|
@@ -6,9 +6,12 @@ import {
|
|
|
import { Logger } from 'winston'
|
|
|
import { DomainEventFactoryInterface } from '../Event/DomainEventFactoryInterface'
|
|
|
import { TransitionRevisionsFromPrimaryToSecondaryDatabaseForUser } from '../UseCase/Transition/TransitionRevisionsFromPrimaryToSecondaryDatabaseForUser/TransitionRevisionsFromPrimaryToSecondaryDatabaseForUser'
|
|
|
+import { Uuid } from '@standardnotes/domain-core'
|
|
|
+import { RevisionRepositoryInterface } from '../Revision/RevisionRepositoryInterface'
|
|
|
|
|
|
export class TransitionStatusUpdatedEventHandler implements DomainEventHandlerInterface {
|
|
|
constructor(
|
|
|
+ private primaryRevisionsRepository: RevisionRepositoryInterface,
|
|
|
private transitionRevisionsFromPrimaryToSecondaryDatabaseForUser: TransitionRevisionsFromPrimaryToSecondaryDatabaseForUser,
|
|
|
private domainEventPublisher: DomainEventPublisherInterface,
|
|
|
private domainEventFactory: DomainEventFactoryInterface,
|
|
@@ -29,6 +32,36 @@ export class TransitionStatusUpdatedEventHandler implements DomainEventHandlerIn
|
|
|
}
|
|
|
|
|
|
if (event.payload.status === 'STARTED' && event.payload.transitionType === 'revisions') {
|
|
|
+ const userUuidOrError = Uuid.create(event.payload.userUuid)
|
|
|
+ if (userUuidOrError.isFailed()) {
|
|
|
+ this.logger.error(
|
|
|
+ `Failed to transition revisions for user ${event.payload.userUuid}: ${userUuidOrError.getError()}`,
|
|
|
+ )
|
|
|
+ await this.domainEventPublisher.publish(
|
|
|
+ this.domainEventFactory.createTransitionStatusUpdatedEvent({
|
|
|
+ userUuid: event.payload.userUuid,
|
|
|
+ status: 'FAILED',
|
|
|
+ transitionType: 'revisions',
|
|
|
+ }),
|
|
|
+ )
|
|
|
+
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if (await this.isAlreadyMigrated(userUuidOrError.getValue())) {
|
|
|
+ this.logger.info(`Revisions for user ${event.payload.userUuid} are already migrated`)
|
|
|
+
|
|
|
+ await this.domainEventPublisher.publish(
|
|
|
+ this.domainEventFactory.createTransitionStatusUpdatedEvent({
|
|
|
+ userUuid: event.payload.userUuid,
|
|
|
+ status: 'FINISHED',
|
|
|
+ transitionType: 'revisions',
|
|
|
+ }),
|
|
|
+ )
|
|
|
+
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
await this.domainEventPublisher.publish(
|
|
|
this.domainEventFactory.createTransitionStatusUpdatedEvent({
|
|
|
userUuid: event.payload.userUuid,
|
|
@@ -66,4 +99,16 @@ export class TransitionStatusUpdatedEventHandler implements DomainEventHandlerIn
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private async isAlreadyMigrated(userUuid: Uuid): Promise<boolean> {
|
|
|
+ const totalRevisionsCountForUserInPrimary = await this.primaryRevisionsRepository.countByUserUuid(userUuid)
|
|
|
+
|
|
|
+ if (totalRevisionsCountForUserInPrimary > 0) {
|
|
|
+ this.logger.info(
|
|
|
+ `User ${userUuid.value} has ${totalRevisionsCountForUserInPrimary} revisions in primary database.`,
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ return totalRevisionsCountForUserInPrimary === 0
|
|
|
+ }
|
|
|
}
|