import { MapperInterface, Uuid } from '@standardnotes/domain-core' import { Repository } from 'typeorm' import { AuthenticatorChallenge } from '../../Domain/Authenticator/AuthenticatorChallenge' import { AuthenticatorChallengeRepositoryInterface } from '../../Domain/Authenticator/AuthenticatorChallengeRepositoryInterface' import { TypeORMAuthenticatorChallenge } from '../TypeORM/TypeORMAuthenticatorChallenge' export class MySQLAuthenticatorChallengeRepository implements AuthenticatorChallengeRepositoryInterface { constructor( private ormRepository: Repository, private mapper: MapperInterface, ) {} async save(authenticatorChallenge: AuthenticatorChallenge): Promise { let persistence = this.mapper.toProjection(authenticatorChallenge) const existing = await this.findByUserUuid(authenticatorChallenge.props.userUuid) if (existing !== null) { existing.props.challenge = authenticatorChallenge.props.challenge existing.props.createdAt = authenticatorChallenge.props.createdAt persistence = this.mapper.toProjection(existing) } await this.ormRepository.save(persistence) } async findByUserUuid(userUuid: Uuid): Promise { const persistence = await this.ormRepository .createQueryBuilder('challenge') .where('challenge.user_uuid = :userUuid', { userUuid: userUuid.value, }) .getOne() if (persistence === null) { return null } return this.mapper.toDomain(persistence) } }