Skip to content

Commit

Permalink
feat(clone): domain snapshots and intentions return values
Browse files Browse the repository at this point in the history
  • Loading branch information
kgajowy committed Oct 11, 2021
1 parent 70da55e commit 99777f0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ClonePiece } from '../../../shared-kernel/clone-piece';

export interface ClonePartSnapshot {
readonly id: string;
readonly piece: ClonePiece;
readonly resourceId: string;
readonly finished: boolean;
readonly uri?: string;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { v4 } from 'uuid';
import { ClonePiece } from '../../../../shared-kernel/clone-piece';

import { ResourceId } from '../resource.id';
import { ClonePartSnapshot } from '../clone-part.snapshot';

import { PieceId } from './piece.id';
import { PieceLocation } from './piece-location';

Expand All @@ -26,11 +29,23 @@ export class ClonePart {
return this.finished;
}

// TODO
toSnapshot() {
return {};
toSnapshot(): ClonePartSnapshot {
return {
id: this.id.value,
piece: this.piece,
resourceId: this.resourceId.value,
finished: this.finished,
uri: this.uri?.value,
};
}

// TODO
static fromSnapshot() {}
static fromSnapshot(snapshot: ClonePartSnapshot) {
return new ClonePart(
new PieceId(snapshot.id),
snapshot.piece,
new ResourceId(snapshot.resourceId),
snapshot.finished,
snapshot.uri ? new PieceLocation(snapshot.uri) : undefined,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ResourceKind } from './resource.kind';
import { ClonePartSnapshot } from './clone-part.snapshot';

export interface ExportSnapshot {
id: string;
resourceId: string;
resourceKind: ResourceKind;
archiveLocation?: string;
exportPieces: ClonePartSnapshot[];
}
47 changes: 38 additions & 9 deletions api/apps/api/src/modules/clone/export/domain/export/export.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { v4 } from 'uuid';
import { AggregateRoot } from '@nestjs/cqrs';
import { Either, left, right } from 'fp-ts/Either';

import { ResourceKind } from './resource.kind';
import { ExportId } from './export.id';
Expand All @@ -13,6 +14,10 @@ import { ArchiveReady } from '../events/archive-ready.event';
import { PieceLocation } from './clone-part/piece-location';
import { ClonePart } from './clone-part/clone-part';
import { PieceId } from './clone-part/piece.id';
import { ExportSnapshot } from './export.snapshot';

export const pieceNotFound = Symbol('export piece not found');
export const notReady = Symbol('some pieces of export are not yet ready');

export class Export extends AggregateRoot {
private constructor(
Expand Down Expand Up @@ -50,28 +55,52 @@ export class Export extends AggregateRoot {
return exportRequest;
}

completePiece(id: PieceId, pieceLocation: PieceLocation) {
completePiece(
id: PieceId,
pieceLocation: PieceLocation,
): Either<typeof pieceNotFound, true> {
const piece = this.pieces.find((piece) => piece.id.equals(id));

// TODO throw
piece?.finish(pieceLocation);
if (!piece) {
return left(pieceNotFound);
}
piece.finish(pieceLocation);

if (this.#allPiecesReady()) {
this.apply(new ClonePartsFinished(this.id));
}

return right(true);
}

complete(archiveLocation: ArchiveLocation) {
complete(archiveLocation: ArchiveLocation): Either<typeof notReady, true> {
if (!this.#allPiecesReady()) {
return left(notReady);
}
this.archiveLocation = archiveLocation;
this.apply(new ArchiveReady(this.id, this.archiveLocation));
return right(true);
}

toSnapshot() {
//
toSnapshot(): ExportSnapshot {
return {
id: this.id.value,
resourceId: this.resourceId.value,
resourceKind: this.resourceKind,
exportPieces: this.pieces.map((piece) => piece.toSnapshot()),
archiveLocation: this.archiveLocation?.value,
};
}

static fromSnapshot() {
//
static fromSnapshot(snapshot: ExportSnapshot): Export {
return new Export(
new ExportId(snapshot.id),
new ResourceId(snapshot.resourceId),
snapshot.resourceKind,
snapshot.exportPieces.map((piece) => ClonePart.fromSnapshot(piece)),
snapshot.archiveLocation
? new ArchiveLocation(snapshot.archiveLocation)
: undefined,
);
}

#allPiecesReady = () => this.pieces.every((piece) => piece.isReady());
Expand Down

0 comments on commit 99777f0

Please sign in to comment.