user_email_backup.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import 'reflect-metadata'
  2. import 'newrelic'
  3. import { Logger } from 'winston'
  4. import * as dayjs from 'dayjs'
  5. import * as utc from 'dayjs/plugin/utc'
  6. import { ContainerConfigLoader } from '../src/Bootstrap/Container'
  7. import TYPES from '../src/Bootstrap/Types'
  8. import { Env } from '../src/Bootstrap/Env'
  9. import { DomainEventPublisherInterface } from '@standardnotes/domain-events'
  10. import { DomainEventFactoryInterface } from '../src/Domain/Event/DomainEventFactoryInterface'
  11. import { SettingRepositoryInterface } from '../src/Domain/Setting/SettingRepositoryInterface'
  12. import { MuteFailedBackupsEmailsOption, SettingName } from '@standardnotes/settings'
  13. import { RoleServiceInterface } from '../src/Domain/Role/RoleServiceInterface'
  14. import { PermissionName } from '@standardnotes/features'
  15. import { UserRepositoryInterface } from '../src/Domain/User/UserRepositoryInterface'
  16. import { Email } from '@standardnotes/domain-core'
  17. const inputArgs = process.argv.slice(2)
  18. const backupEmail = inputArgs[0]
  19. const requestBackups = async (
  20. userRepository: UserRepositoryInterface,
  21. settingRepository: SettingRepositoryInterface,
  22. roleService: RoleServiceInterface,
  23. domainEventFactory: DomainEventFactoryInterface,
  24. domainEventPublisher: DomainEventPublisherInterface,
  25. ): Promise<void> => {
  26. const permissionName = PermissionName.DailyEmailBackup
  27. const muteEmailsSettingName = SettingName.NAMES.MuteFailedBackupsEmails
  28. const muteEmailsSettingValue = MuteFailedBackupsEmailsOption.Muted
  29. const emailOrError = Email.create(backupEmail)
  30. if (emailOrError.isFailed()) {
  31. throw new Error('Could not trigger email backup for user - missing email parameter')
  32. }
  33. const email = emailOrError.getValue()
  34. const user = await userRepository.findOneByUsernameOrEmail(email)
  35. if (user === null) {
  36. throw new Error(`Could not find user with email: ${backupEmail}`)
  37. }
  38. const userIsPermittedForEmailBackups = await roleService.userHasPermission(user.uuid, permissionName)
  39. if (!userIsPermittedForEmailBackups) {
  40. throw new Error(`User ${backupEmail} is not permitted for email backups`)
  41. }
  42. let userHasEmailsMuted = false
  43. const emailsMutedSetting = await settingRepository.findOneByNameAndUserUuid(muteEmailsSettingName, user.uuid)
  44. if (emailsMutedSetting !== null && emailsMutedSetting.value !== null) {
  45. userHasEmailsMuted = emailsMutedSetting.value === muteEmailsSettingValue
  46. }
  47. await domainEventPublisher.publish(
  48. domainEventFactory.createEmailBackupRequestedEvent(
  49. user.uuid,
  50. emailsMutedSetting?.uuid as string,
  51. userHasEmailsMuted,
  52. ),
  53. )
  54. return
  55. }
  56. const container = new ContainerConfigLoader()
  57. void container.load().then((container) => {
  58. dayjs.extend(utc)
  59. const env: Env = new Env()
  60. env.load()
  61. const logger: Logger = container.get(TYPES.Auth_Logger)
  62. logger.info(`Starting email backup requesting for ${backupEmail} ...`)
  63. const settingRepository: SettingRepositoryInterface = container.get(TYPES.Auth_SettingRepository)
  64. const userRepository: UserRepositoryInterface = container.get(TYPES.Auth_UserRepository)
  65. const roleService: RoleServiceInterface = container.get(TYPES.Auth_RoleService)
  66. const domainEventFactory: DomainEventFactoryInterface = container.get(TYPES.Auth_DomainEventFactory)
  67. const domainEventPublisher: DomainEventPublisherInterface = container.get(TYPES.Auth_DomainEventPublisher)
  68. Promise.resolve(
  69. requestBackups(userRepository, settingRepository, roleService, domainEventFactory, domainEventPublisher),
  70. )
  71. .then(() => {
  72. logger.info(`Email backup requesting complete for ${backupEmail}`)
  73. process.exit(0)
  74. })
  75. .catch((error) => {
  76. logger.error(`Could not finish email backup requesting for ${backupEmail}: ${error.message}`)
  77. process.exit(1)
  78. })
  79. })