Revert "Revert "feat(syncing-server): notify shared vault users upon file uploads or removals (#692)""

This reverts commit d261c81cd0.
This commit is contained in:
Karol Sójko
2023-08-09 18:00:49 +02:00
parent 373767248c
commit 1c3ff526b7
8 changed files with 198 additions and 0 deletions
@@ -1,15 +1,26 @@
import { DomainEventHandlerInterface, SharedVaultFileRemovedEvent } from '@standardnotes/domain-events'
import { NotificationPayload, NotificationType, Uuid } from '@standardnotes/domain-core'
import { Logger } from 'winston'
import { UpdateStorageQuotaUsedInSharedVault } from '../UseCase/SharedVaults/UpdateStorageQuotaUsedInSharedVault/UpdateStorageQuotaUsedInSharedVault'
import { AddNotificationsForUsers } from '../UseCase/Messaging/AddNotificationsForUsers/AddNotificationsForUsers'
export class SharedVaultFileRemovedEventHandler implements DomainEventHandlerInterface {
constructor(
private updateStorageQuotaUsedInSharedVaultUseCase: UpdateStorageQuotaUsedInSharedVault,
private addNotificationsForUsers: AddNotificationsForUsers,
private logger: Logger,
) {}
async handle(event: SharedVaultFileRemovedEvent): Promise<void> {
const sharedVaultUuidOrError = Uuid.create(event.payload.sharedVaultUuid)
if (sharedVaultUuidOrError.isFailed()) {
this.logger.error(sharedVaultUuidOrError.getError())
return
}
const sharedVaultUuid = sharedVaultUuidOrError.getValue()
const result = await this.updateStorageQuotaUsedInSharedVaultUseCase.execute({
sharedVaultUuid: event.payload.sharedVaultUuid,
bytesUsed: -event.payload.fileByteSize,
@@ -17,6 +28,24 @@ export class SharedVaultFileRemovedEventHandler implements DomainEventHandlerInt
if (result.isFailed()) {
this.logger.error(`Failed to update storage quota used in shared vault: ${result.getError()}`)
return
}
const notificationPayload = NotificationPayload.create({
sharedVaultUuid,
type: NotificationType.create(NotificationType.TYPES.SharedVaultFileRemoved).getValue(),
version: '1.0',
}).getValue()
const notificationResult = await this.addNotificationsForUsers.execute({
sharedVaultUuid: event.payload.sharedVaultUuid,
type: NotificationType.TYPES.SharedVaultFileRemoved,
payload: notificationPayload,
version: '1.0',
})
if (notificationResult.isFailed()) {
this.logger.error(`Failed to add notification for users: ${notificationResult.getError()}`)
}
}
}