From 54232a544a2d51ffdd29d53fa89bc31e9c0b6a89 Mon Sep 17 00:00:00 2001 From: Aaron Plahn Date: Mon, 29 Jan 2024 09:17:41 -0800 Subject: [PATCH] fix tests for ADD_PHOTOGRAPH_TO_DIGITAL_TEXT_PAGE --- .../resourceDescriptions.e2e.spec.ts.snap | 7 +++ .../controllers/__tests__/createTestModule.ts | 2 + .../command-payload-schemas.spec.ts.snap | 53 +++++++++++++++++++ .../aggregate-schemas.spec.ts.snap | 7 +++ ...ph-to-digital-text-page.command-handler.ts | 27 ++++++++-- ...ital-text-page.command.integration.spec.ts | 13 ++++- 6 files changed, 103 insertions(+), 6 deletions(-) diff --git a/apps/api/src/app/controllers/__tests__/__snapshots__/resourceDescriptions.e2e.spec.ts.snap b/apps/api/src/app/controllers/__tests__/__snapshots__/resourceDescriptions.e2e.spec.ts.snap index 6ff3ba389..e7349fb23 100644 --- a/apps/api/src/app/controllers/__tests__/__snapshots__/resourceDescriptions.e2e.spec.ts.snap +++ b/apps/api/src/app/controllers/__tests__/__snapshots__/resourceDescriptions.e2e.spec.ts.snap @@ -1751,6 +1751,13 @@ exports[`GET /resources should return the expected result 1`] = ` "isOptional": false, "label": "identifier", }, + "photographId": { + "coscradDataType": "UUID", + "description": "a reference to the main photograph for this page", + "isArray": false, + "isOptional": true, + "label": "photograph ID", + }, }, }, "tags": { diff --git a/apps/api/src/app/controllers/__tests__/createTestModule.ts b/apps/api/src/app/controllers/__tests__/createTestModule.ts index b54994124..0443bd9f4 100644 --- a/apps/api/src/app/controllers/__tests__/createTestModule.ts +++ b/apps/api/src/app/controllers/__tests__/createTestModule.ts @@ -90,6 +90,7 @@ import { AddPageToDigitalText } from '../../../domain/models/digital-text/comman import { AddPhotographToDigitalTextPage, AddPhotographToDigitalTextPageCommandHandler, + PhotographAddedToDigitalTextPage, } from '../../../domain/models/digital-text/commands/add-photograph-to-digital-text-page'; import { DigitalText } from '../../../domain/models/digital-text/entities/digital-text.entity'; import { CreateMediaItem } from '../../../domain/models/media-item/commands/create-media-item/create-media-item.command'; @@ -288,6 +289,7 @@ export const buildAllDataClassProviders = () => DigitalTextPageContentTranslated, DigitalTextTitleTranslated, AudioAddedForDigitalTextPage, + PhotographAddedToDigitalTextPage, SongCreated, SongTitleTranslated, LyricsAddedForSong, diff --git a/apps/api/src/app/controllers/command/__snapshots__/command-payload-schemas.spec.ts.snap b/apps/api/src/app/controllers/command/__snapshots__/command-payload-schemas.spec.ts.snap index 853afbbfd..a313ea567 100644 --- a/apps/api/src/app/controllers/command/__snapshots__/command-payload-schemas.spec.ts.snap +++ b/apps/api/src/app/controllers/command/__snapshots__/command-payload-schemas.spec.ts.snap @@ -870,6 +870,59 @@ exports[`command payload schemas Command payload schema should match the snapsho }, "type": "ADD_PAGE_TO_DIGITAL_TEXT", }, + { + "description": "Add a photograph to a digital text page", + "label": "Add Photograph to Page", + "schema": { + "aggregateCompositeIdentifier": { + "complexDataType": "NESTED_TYPE", + "description": "system-wide unique identifier", + "isArray": false, + "isOptional": false, + "label": "Composite Identifier", + "schema": { + "id": { + "coscradDataType": "UUID", + "description": "unique identifier", + "isArray": false, + "isOptional": false, + "label": "ID", + }, + "type": { + "complexDataType": "ENUM", + "description": "must be: digitalText", + "enumLabel": "type", + "enumName": "type", + "isArray": false, + "isOptional": false, + "label": "type", + "labelsAndValues": [ + { + "label": "digitalText", + "value": "digitalText", + }, + ], + }, + }, + }, + "pageIdentifier": { + "coscradDataType": "NON_EMPTY_STRING", + "description": "the page to which you are adding the photograph", + "isArray": false, + "isOptional": false, + "label": "page identifier", + }, + "photographId": { + "coscradDataType": "UUID", + "description": "system reference to the photograph", + "isArray": false, + "isOptional": false, + "label": "photograph ID", + "referenceTo": "photograph", + }, + }, + "type": "ADD_PHOTOGRAPH_TO_DIGITAL_TEXT_PAGE", + }, { "description": "Tranlate the digital text page content to another language", "label": "Translate Digital Text Page Content", diff --git a/apps/api/src/domain/models/__tests__/__snapshots__/aggregate-schemas.spec.ts.snap b/apps/api/src/domain/models/__tests__/__snapshots__/aggregate-schemas.spec.ts.snap index 45a89466a..8ce6c4860 100644 --- a/apps/api/src/domain/models/__tests__/__snapshots__/aggregate-schemas.spec.ts.snap +++ b/apps/api/src/domain/models/__tests__/__snapshots__/aggregate-schemas.spec.ts.snap @@ -569,6 +569,13 @@ exports[`Coscrad Data Schemas for aggregate root domain models the COSCRAD data "isOptional": false, "label": "identifier", }, + "photographId": { + "coscradDataType": "UUID", + "description": "a reference to the main photograph for this page", + "isArray": false, + "isOptional": true, + "label": "photograph ID", + }, }, }, "title": { diff --git a/apps/api/src/domain/models/digital-text/commands/add-photograph-to-digital-text-page/add-photograph-to-digital-text-page.command-handler.ts b/apps/api/src/domain/models/digital-text/commands/add-photograph-to-digital-text-page/add-photograph-to-digital-text-page.command-handler.ts index bd8087e94..649030e7c 100644 --- a/apps/api/src/domain/models/digital-text/commands/add-photograph-to-digital-text-page/add-photograph-to-digital-text-page.command-handler.ts +++ b/apps/api/src/domain/models/digital-text/commands/add-photograph-to-digital-text-page/add-photograph-to-digital-text-page.command-handler.ts @@ -1,8 +1,10 @@ -import { CommandHandler, ICommand } from '@coscrad/commands'; +import { AggregateType } from '@coscrad/api-interfaces'; +import { CommandHandler } from '@coscrad/commands'; import { Valid } from '../../../../../domain/domainModelValidators/Valid'; import { DeluxeInMemoryStore } from '../../../../../domain/types/DeluxeInMemoryStore'; import { InMemorySnapshot } from '../../../../../domain/types/ResourceType'; -import { InternalError } from '../../../../../lib/errors/InternalError'; +import { InternalError, isInternalError } from '../../../../../lib/errors/InternalError'; +import { isNotFound } from '../../../../../lib/types/not-found'; import { ResultOrError } from '../../../../../types/ResultOrError'; import { BaseUpdateCommandHandler } from '../../../shared/command-handlers/base-update-command-handler'; import { BaseEvent, IEventPayload } from '../../../shared/events/base-event.entity'; @@ -13,8 +15,25 @@ import { PhotographAddedToDigitalTextPage } from './photograph-added-to-digital- @CommandHandler(AddPhotographToDigitalTextPage) export class AddPhotographToDigitalTextPageCommandHandler extends BaseUpdateCommandHandler { - protected fetchRequiredExternalState(_command?: ICommand): Promise { - return Promise.resolve(new DeluxeInMemoryStore({}).fetchFullSnapshotInLegacyFormat()); + protected async fetchRequiredExternalState({ + photographId, + }: AddPhotographToDigitalTextPage): Promise { + const photographSearchResult = await this.repositoryProvider + .forResource(AggregateType.photograph) + .fetchById(photographId); + + if (isInternalError(photographSearchResult)) { + throw new InternalError( + `Failed to ADD_PHOTOGRAPH_TO_DIGITAL_TEXT due to invalid existing state`, + [photographSearchResult] + ); + } + + return new DeluxeInMemoryStore({ + [AggregateType.photograph]: isNotFound(photographSearchResult) + ? [] + : [photographSearchResult], + }).fetchFullSnapshotInLegacyFormat(); } protected actOnInstance( diff --git a/apps/api/src/domain/models/digital-text/commands/add-photograph-to-digital-text-page/add-photograph-to-digital-text-page.command.integration.spec.ts b/apps/api/src/domain/models/digital-text/commands/add-photograph-to-digital-text-page/add-photograph-to-digital-text-page.command.integration.spec.ts index a45d64c27..facf63f29 100644 --- a/apps/api/src/domain/models/digital-text/commands/add-photograph-to-digital-text-page/add-photograph-to-digital-text-page.command.integration.spec.ts +++ b/apps/api/src/domain/models/digital-text/commands/add-photograph-to-digital-text-page/add-photograph-to-digital-text-page.command.integration.spec.ts @@ -19,7 +19,9 @@ import { DummyCommandFsaFactory } from '../../../__tests__/command-helpers/dummy import { CommandAssertionDependencies } from '../../../__tests__/command-helpers/types/CommandAssertionDependencies'; import buildDummyUuid from '../../../__tests__/utilities/buildDummyUuid'; import { dummySystemUserId } from '../../../__tests__/utilities/dummySystemUserId'; +import InvalidExternalReferenceByAggregateError from '../../../categories/errors/InvalidExternalReferenceByAggregateError'; import AggregateNotFoundError from '../../../shared/common-command-errors/AggregateNotFoundError'; +import CommandExecutionError from '../../../shared/common-command-errors/CommandExecutionError'; import { PageAddedToDigitalText } from '../add-page-to-digital-text/page-added-to-digital-text.event'; import { DigitalTextCreated } from '../digital-text-created.event'; import { AddPhotographToDigitalTextPage } from './add-photograph-to-digital-text-page.command'; @@ -138,7 +140,9 @@ describe(commandType, () => { checkError: (result) => { assertErrorAsExpected( result, - new AggregateNotFoundError(existingPhotograph.getCompositeIdentifier()) + new CommandExecutionError([ + new AggregateNotFoundError(digitalTextCompositeIdentifier), + ]) ); }, }); @@ -162,7 +166,12 @@ describe(commandType, () => { checkError: (result) => { assertErrorAsExpected( result, - new AggregateNotFoundError(existingPhotograph.getCompositeIdentifier()) + new CommandExecutionError([ + new InvalidExternalReferenceByAggregateError( + digitalTextCompositeIdentifier, + [existingPhotograph.getCompositeIdentifier()] + ), + ]) ); }, });