GetUserAnalyticsId.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. import { inject, injectable } from 'inversify'
  2. import { Result, UseCaseInterface, Username, Uuid } from '@standardnotes/domain-core'
  3. import TYPES from '../../../Bootstrap/Types'
  4. import { AnalyticsEntityRepositoryInterface } from '../../Entity/AnalyticsEntityRepositoryInterface'
  5. import { GetUserAnalyticsIdDTO } from './GetUserAnalyticsIdDTO'
  6. import { GetUserAnalyticsIdResponse } from './GetUserAnalyticsIdResponse'
  7. @injectable()
  8. export class GetUserAnalyticsId implements UseCaseInterface<GetUserAnalyticsIdResponse> {
  9. constructor(
  10. @inject(TYPES.AnalyticsEntityRepository) private analyticsEntityRepository: AnalyticsEntityRepositoryInterface,
  11. ) {}
  12. async execute(dto: GetUserAnalyticsIdDTO): Promise<Result<GetUserAnalyticsIdResponse>> {
  13. let analyticsEntity = null
  14. if (dto.userUuid) {
  15. analyticsEntity = await this.analyticsEntityRepository.findOneByUserUuid(dto.userUuid)
  16. } else if (dto.userEmail) {
  17. analyticsEntity = await this.analyticsEntityRepository.findOneByUserEmail(dto.userEmail)
  18. }
  19. if (analyticsEntity === null) {
  20. return Result.fail(`Could not find analytics entity ${dto.userUuid}`)
  21. }
  22. return Result.ok({
  23. analyticsId: analyticsEntity.id,
  24. userUuid: Uuid.create(analyticsEntity.userUuid).getValue(),
  25. username: Username.create(analyticsEntity.username).getValue(),
  26. })
  27. }
  28. }