Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: legacy project import requested saga #1093

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Injectable } from '@nestjs/common';
import { ICommand, ofType, Saga } from '@nestjs/cqrs';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { LegacyProjectImportRequested } from '../domain/events/legacy-project-import-requested.event';
import { MarkLegacyProjectImportAsSubmitted } from './mark-legacy-project-as-submitted.command';

@Injectable()
export class LegacyProjectImportRequestedSaga {
@Saga()
emitApiEvents = (events$: Observable<any>): Observable<ICommand> =>
events$.pipe(
ofType(LegacyProjectImportRequested),
map((event) => new MarkLegacyProjectImportAsSubmitted(event.projectId)),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { LegacyProjectImportEntity } from '../infra/entities/legacy-project-impo
import { LegacyProjectImportTypeormRepository } from '../infra/legacy-project-import-typeorm.repository';
import { AddFileToLegacyProjectImportHandler } from './add-file-to-legacy-project-import.handler';
import { CompleteLegacyProjectImportPieceHandler } from './complete-legacy-project-import-piece.handler';
import { LegacyProjectImportRequestedSaga } from './legacy-project-import-requested.saga';
import { MarkLegacyProjectImportAsSubmittedHandler } from './mark-legacy-project-as-submitted.handler';
import { MarkLegacyProjectImportAsFailedHandler } from './mark-legacy-project-import-as-failed.handler';
import { MarkLegacyProjectImportPieceAsFailedHandler } from './mark-legacy-project-import-piece-as-failed.handler';
import { StartLegacyProjectImportHandler } from './start-legacy-project-import.handler';
Expand Down Expand Up @@ -54,6 +56,8 @@ import { StartLegacyProjectImportHandler } from './start-legacy-project-import.h
return path.endsWith('/') ? path.substring(0, path.length - 1) : path;
},
},
LegacyProjectImportRequestedSaga,
MarkLegacyProjectImportAsSubmittedHandler,
MarkLegacyProjectImportAsFailedHandler,
MarkLegacyProjectImportPieceAsFailedHandler,
StartLegacyProjectImportHandler,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ResourceId } from '@marxan/cloning/domain';
import { Command } from '@nestjs-architects/typed-cqrs';

export class MarkLegacyProjectImportAsSubmitted extends Command<void> {
constructor(public readonly projectId: ResourceId) {
super();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { API_EVENT_KINDS } from '@marxan/api-events';
import { ResourceId } from '@marxan/cloning/domain';
import { Logger } from '@nestjs/common';
import {
CommandBus,
CommandHandler,
IInferredCommandHandler,
} from '@nestjs/cqrs';
import { isLeft } from 'fp-ts/lib/Either';
import { ApiEventsService } from '../../api-events';
import { LegacyProjectImportRepository } from '../domain/legacy-project-import/legacy-project-import.repository';
import { MarkLegacyProjectImportAsSubmitted } from './mark-legacy-project-as-submitted.command';
import { MarkLegacyProjectImportAsFailed } from './mark-legacy-project-import-as-failed.command';

@CommandHandler(MarkLegacyProjectImportAsSubmitted)
export class MarkLegacyProjectImportAsSubmittedHandler
implements IInferredCommandHandler<MarkLegacyProjectImportAsSubmitted> {
constructor(
private readonly legacyProjectImportRepository: LegacyProjectImportRepository,
private readonly apiEvents: ApiEventsService,
private readonly commandBus: CommandBus,
private readonly logger: Logger,
) {}

private async markLegacyProjectImportAsSubmitted(
projectId: ResourceId,
reason: string,
): Promise<void> {
this.logger.error(reason);
await this.commandBus.execute(
new MarkLegacyProjectImportAsFailed(projectId, reason),
);
}

async execute({
projectId,
}: MarkLegacyProjectImportAsSubmitted): Promise<void> {
const legacyProjectImportOrError = await this.legacyProjectImportRepository.find(
projectId,
);

if (isLeft(legacyProjectImportOrError)) {
await this.markLegacyProjectImportAsSubmitted(
projectId,
`Could not find legacy project import with project ID ${projectId.value}`,
);
return;
}

const {
ownerId,
scenarioId,
} = legacyProjectImportOrError.right.toSnapshot();
const kind = API_EVENT_KINDS.project__legacy__import__submitted__v1__alpha;
const topic = projectId.value;

await this.apiEvents.createIfNotExists({
kind,
topic,
data: {
projectId: topic,
ownerId,
scenarioId,
},
});
}
}