UpdateUser.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { TimerInterface } from '@standardnotes/time'
  2. import { inject, injectable } from 'inversify'
  3. import TYPES from '../../Bootstrap/Types'
  4. import { AuthResponseFactoryResolverInterface } from '../Auth/AuthResponseFactoryResolverInterface'
  5. import { UserRepositoryInterface } from '../User/UserRepositoryInterface'
  6. import { UpdateUserDTO } from './UpdateUserDTO'
  7. import { UpdateUserResponse } from './UpdateUserResponse'
  8. import { UseCaseInterface } from './UseCaseInterface'
  9. @injectable()
  10. export class UpdateUser implements UseCaseInterface {
  11. constructor(
  12. @inject(TYPES.Auth_UserRepository) private userRepository: UserRepositoryInterface,
  13. @inject(TYPES.Auth_AuthResponseFactoryResolver)
  14. private authResponseFactoryResolver: AuthResponseFactoryResolverInterface,
  15. @inject(TYPES.Auth_Timer) private timer: TimerInterface,
  16. ) {}
  17. async execute(dto: UpdateUserDTO): Promise<UpdateUserResponse> {
  18. dto.user.updatedAt = this.timer.getUTCDate()
  19. const updatedUser = await this.userRepository.save(dto.user)
  20. const authResponseFactory = this.authResponseFactoryResolver.resolveAuthResponseFactoryVersion(dto.apiVersion)
  21. const result = await authResponseFactory.createResponse({
  22. user: updatedUser,
  23. apiVersion: dto.apiVersion,
  24. userAgent: dto.updatedWithUserAgent,
  25. ephemeralSession: false,
  26. readonlyAccess: false,
  27. })
  28. return {
  29. success: true,
  30. authResponse: result.response,
  31. }
  32. }
  33. }