MySQLAuthenticatorChallengeRepository.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { MapperInterface, Uuid } from '@standardnotes/domain-core'
  2. import { Repository } from 'typeorm'
  3. import { AuthenticatorChallenge } from '../../Domain/Authenticator/AuthenticatorChallenge'
  4. import { AuthenticatorChallengeRepositoryInterface } from '../../Domain/Authenticator/AuthenticatorChallengeRepositoryInterface'
  5. import { TypeORMAuthenticatorChallenge } from '../TypeORM/TypeORMAuthenticatorChallenge'
  6. export class MySQLAuthenticatorChallengeRepository implements AuthenticatorChallengeRepositoryInterface {
  7. constructor(
  8. private ormRepository: Repository<TypeORMAuthenticatorChallenge>,
  9. private mapper: MapperInterface<AuthenticatorChallenge, TypeORMAuthenticatorChallenge>,
  10. ) {}
  11. async save(authenticatorChallenge: AuthenticatorChallenge): Promise<void> {
  12. let persistence = this.mapper.toProjection(authenticatorChallenge)
  13. const existing = await this.findByUserUuid(authenticatorChallenge.props.userUuid)
  14. if (existing !== null) {
  15. existing.props.challenge = authenticatorChallenge.props.challenge
  16. existing.props.createdAt = authenticatorChallenge.props.createdAt
  17. persistence = this.mapper.toProjection(existing)
  18. }
  19. await this.ormRepository.save(persistence)
  20. }
  21. async findByUserUuid(userUuid: Uuid): Promise<AuthenticatorChallenge | null> {
  22. const persistence = await this.ormRepository
  23. .createQueryBuilder('challenge')
  24. .where('challenge.user_uuid = :userUuid', {
  25. userUuid: userUuid.value,
  26. })
  27. .getOne()
  28. if (persistence === null) {
  29. return null
  30. }
  31. return this.mapper.toDomain(persistence)
  32. }
  33. }