SharedVaultFileRemovedEventHandler.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { DomainEventHandlerInterface, SharedVaultFileRemovedEvent } from '@standardnotes/domain-events'
  2. import { NotificationPayload, NotificationType, Uuid } from '@standardnotes/domain-core'
  3. import { Logger } from 'winston'
  4. import { UpdateStorageQuotaUsedInSharedVault } from '../UseCase/SharedVaults/UpdateStorageQuotaUsedInSharedVault/UpdateStorageQuotaUsedInSharedVault'
  5. import { AddNotificationsForUsers } from '../UseCase/Messaging/AddNotificationsForUsers/AddNotificationsForUsers'
  6. export class SharedVaultFileRemovedEventHandler implements DomainEventHandlerInterface {
  7. constructor(
  8. private updateStorageQuotaUsedInSharedVaultUseCase: UpdateStorageQuotaUsedInSharedVault,
  9. private addNotificationsForUsers: AddNotificationsForUsers,
  10. private logger: Logger,
  11. ) {}
  12. async handle(event: SharedVaultFileRemovedEvent): Promise<void> {
  13. const sharedVaultUuidOrError = Uuid.create(event.payload.sharedVaultUuid)
  14. if (sharedVaultUuidOrError.isFailed()) {
  15. this.logger.error(sharedVaultUuidOrError.getError())
  16. return
  17. }
  18. const sharedVaultUuid = sharedVaultUuidOrError.getValue()
  19. const result = await this.updateStorageQuotaUsedInSharedVaultUseCase.execute({
  20. sharedVaultUuid: event.payload.sharedVaultUuid,
  21. bytesUsed: -event.payload.fileByteSize,
  22. })
  23. if (result.isFailed()) {
  24. this.logger.error(`Failed to update storage quota used in shared vault: ${result.getError()}`)
  25. return
  26. }
  27. const notificationPayload = NotificationPayload.create({
  28. sharedVaultUuid,
  29. type: NotificationType.create(NotificationType.TYPES.SharedVaultFileRemoved).getValue(),
  30. version: '1.0',
  31. }).getValue()
  32. const notificationResult = await this.addNotificationsForUsers.execute({
  33. sharedVaultUuid: event.payload.sharedVaultUuid,
  34. type: NotificationType.TYPES.SharedVaultFileRemoved,
  35. payload: notificationPayload,
  36. version: '1.0',
  37. })
  38. if (notificationResult.isFailed()) {
  39. this.logger.error(`Failed to add notification for users: ${notificationResult.getError()}`)
  40. }
  41. }
  42. }