import { ContentType } from '@standardnotes/common' ; import { AccountDeletionRequestedEvent , DomainEventHandlerInterface , DomainEventPublisherInterface } from '@standardnotes/domain-events' ; import { inject , injectable } from 'inversify' ; import { Logger } from 'winston' ; import TYPES from '../../Bootstrap/Types' ; import { DomainEventFactoryInterface } from '../Event/DomainEventFactoryInterface' ; import { Frequency } from '../Extension/Frequency' ; import { ContentDecoderInterface } from '../Item/ContentDecoderInterface' ; import { ItemRepositoryInterface } from '../Item/ItemRepositoryInterface' ; @ injectable ( ) export class AccountDeletionRequestedEventHandler implements DomainEventHandlerInterface { constructor ( @ inject ( TYPES . ItemRepository ) private itemRepository , @ inject ( TYPES . ContentDecoder ) private contentDecoder , @ inject ( TYPES . DomainEventPublisher ) private domainEventPublisher , @ inject ( TYPES . DomainEventFactory ) private domainEventFactory , @ inject ( TYPES . Logger ) private logger ) { } async handle ( event ) { await this . syncExtensionsRemoval ( event . payload . userUuid ) ; await this . itemRepository . deleteByUserUuid ( event . payload . userUuid ) ; this . logger . info ( `Finished account cleanup for user: ${ event . payload . userUuid }` ) ; } private async syncExtensionsRemoval ( userUuid ) { const extensions = await this . itemRepository . findAll ( { deleted : false , contentType : ContentType . ServerExtension , userUuid , sortBy : 'updated_at_timestamp' , sortOrder : 'DESC' , } ) ; for ( const extension of extensions ) { if ( ! extension . content ) { continue ; } const decodedContent = await this . contentDecoder . decode ( extension . content ) ; if ( ! ( 'frequency' in decodedContent ) || decodedContent . frequency !== Frequency . Realtime ) { continue ; } await this . domainEventPublisher . publish ( await this . domainEventFactory . createItemsSyncedEvent ( { userUuid , extensionUrl : `${ decodedContent . url }&directive=delete-account` , extensionId : extension . uuid , itemUuids : [ ] , forceMute : true , skipFileBackup : true , source : 'account-deletion' , } ) ) ; } } }