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 aggregate #1054

Merged
merged 6 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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,10 @@
import { ResourceId } from '@marxan/cloning/domain';
import { IEvent } from '@nestjs/cqrs';
import { LegacyProjectImportId } from '../legacy-project-import/legacy-project-import.id';

export class AllLegacyProjectPiecesImported implements IEvent {
constructor(
public readonly legacyProjectImportId: LegacyProjectImportId,
public readonly projectId: ResourceId,
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IEvent } from '@nestjs/cqrs';
import { LegacyProjectImportId } from '../legacy-project-import/legacy-project-import.id';

export class LegacyProjectImportBatchFailed implements IEvent {
constructor(
public readonly legacyProjectImportId: LegacyProjectImportId,
public readonly batchNumber: number,
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IEvent } from '@nestjs/cqrs';
import { LegacyProjectImportComponentId } from '../legacy-project-import/legacy-project-import-component.id';
import { LegacyProjectImportId } from '../legacy-project-import/legacy-project-import.id';

export class LegacyProjectImportPieceImported implements IEvent {
constructor(
public readonly legacyProjectImportId: LegacyProjectImportId,
public readonly componentId: LegacyProjectImportComponentId,
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IEvent } from '@nestjs/cqrs';
import { LegacyProjectImportComponentId } from '../legacy-project-import/legacy-project-import-component.id';
import { LegacyProjectImportId } from '../legacy-project-import/legacy-project-import.id';

export class LegacyProjectImportPieceRequested implements IEvent {
constructor(
public readonly legacyProjectImportId: LegacyProjectImportId,
public readonly componentId: LegacyProjectImportComponentId,
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ResourceId } from '@marxan/cloning/domain';
import { IEvent } from '@nestjs/cqrs';
import { LegacyProjectImportId } from '../legacy-project-import/legacy-project-import.id';

export class LegacyProjectImportRequested implements IEvent {
constructor(
public readonly legacyProjectImportId: LegacyProjectImportId,
public readonly projectId: ResourceId,
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export enum LegacyProjectImportComponentStatuses {
Submitted = 'submitted',
Completed = 'completed',
Failed = 'failed',
}

export class LegacyProjectImportComponentStatus {
static create(): LegacyProjectImportComponentStatus {
return new LegacyProjectImportComponentStatus(
LegacyProjectImportComponentStatuses.Submitted,
);
}

constructor(private readonly status: LegacyProjectImportComponentStatuses) {}

markAsCompleted(): LegacyProjectImportComponentStatus {
if (this.status === LegacyProjectImportComponentStatuses.Failed)
throw new Error('Import component has already failed');

return new LegacyProjectImportComponentStatus(
LegacyProjectImportComponentStatuses.Completed,
);
}

markAsFailed(): LegacyProjectImportComponentStatus {
if (this.status === LegacyProjectImportComponentStatuses.Completed)
throw new Error('Import component has already been completed');

return new LegacyProjectImportComponentStatus(
LegacyProjectImportComponentStatuses.Failed,
);
}

isReady() {
return this.status === LegacyProjectImportComponentStatuses.Completed;
}

hasFailed() {
return this.status === LegacyProjectImportComponentStatuses.Failed;
}

toSnapshot(): LegacyProjectImportComponentStatuses {
return this.status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { v4 } from 'uuid';

export class LegacyProjectImportComponentId {
private readonly _token = 'legacy-project-import-component-id';
constructor(public readonly value: string) {}

static create(): LegacyProjectImportComponentId {
return new LegacyProjectImportComponentId(v4());
}

equals(other: LegacyProjectImportComponentId): boolean {
return this.value === other.value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { LegacyProjectImportPiece } from '@marxan/legacy-project-import';
import { LegacyProjectImportComponentStatuses } from './legacy-project-import-component-status';

export interface LegacyProjectImportComponentSnapshot {
readonly id: string;
readonly kind: LegacyProjectImportPiece;
readonly order: number;
readonly archiveLocation?: string;
readonly status: LegacyProjectImportComponentStatuses;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { ArchiveLocation } from '@marxan/cloning/domain';
import {
LegacyProjectImportPiece,
LegacyProjectImportPieceOrderResolver,
} from '@marxan/legacy-project-import';
import { LegacyProjectImportComponentStatus } from './legacy-project-import-component-status';
import { LegacyProjectImportComponentId } from './legacy-project-import-component.id';
import { LegacyProjectImportComponentSnapshot } from './legacy-project-import-component.snapshot';

export class LegacyProjectImportComponent {
private constructor(
readonly id: LegacyProjectImportComponentId,
readonly kind: LegacyProjectImportPiece,
readonly order: number,
readonly archiveLocation?: ArchiveLocation,
private status: LegacyProjectImportComponentStatus = LegacyProjectImportComponentStatus.create(),
) {}

static fromSnapshot(snapshot: LegacyProjectImportComponentSnapshot) {
return new LegacyProjectImportComponent(
new LegacyProjectImportComponentId(snapshot.id),
snapshot.kind,
snapshot.order,
snapshot.archiveLocation
? new ArchiveLocation(snapshot.archiveLocation)
: undefined,
new LegacyProjectImportComponentStatus(snapshot.status),
);
}

static newOne(
kind: LegacyProjectImportPiece,
archiveLocation?: ArchiveLocation,
): LegacyProjectImportComponent {
return new LegacyProjectImportComponent(
LegacyProjectImportComponentId.create(),
kind,
LegacyProjectImportPieceOrderResolver.resolveFor(kind),
archiveLocation,
);
}

isReady() {
return this.status.isReady();
}

hasFailed() {
return this.status.hasFailed();
}

complete() {
this.status = this.status.markAsCompleted();
}

markAsFailed() {
this.status = this.status.markAsFailed();
}

toSnapshot(): LegacyProjectImportComponentSnapshot {
return {
id: this.id.value,
order: this.order,
status: this.status.toSnapshot(),
kind: this.kind,
archiveLocation: this.archiveLocation?.value,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { v4 } from 'uuid';

export class LegacyProjectImportId {
private readonly _token = 'legacy-project-import-id';
constructor(public readonly value: string) {}

static create(): LegacyProjectImportId {
return new LegacyProjectImportId(v4());
}

equals(other: LegacyProjectImportId): boolean {
return this.value === other.value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { LegacyProjectImportFileSnapshot } from '@marxan/legacy-project-import';
import { LegacyProjectImportComponentSnapshot } from './legacy-project-import-component.snapshot';

export interface LegacyProjectImportSnapshot {
readonly id: string;
readonly projectId: string;
readonly scenarioId: string;
readonly ownerId: string;
readonly isAcceptingFiles: boolean;
readonly pieces: LegacyProjectImportComponentSnapshot[];
readonly files: LegacyProjectImportFileSnapshot[];
}
Loading