Skip to content

Commit

Permalink
fix tests for ADD_PHOTOGRAPH_TO_DIGITAL_TEXT_PAGE
Browse files Browse the repository at this point in the history
  • Loading branch information
aaron-plahn committed Jan 31, 2024
1 parent b2c6b1a commit 54232a5
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/app/controllers/__tests__/createTestModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -288,6 +289,7 @@ export const buildAllDataClassProviders = () =>
DigitalTextPageContentTranslated,
DigitalTextTitleTranslated,
AudioAddedForDigitalTextPage,
PhotographAddedToDigitalTextPage,
SongCreated,
SongTitleTranslated,
LyricsAddedForSong,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -13,8 +15,25 @@ import { PhotographAddedToDigitalTextPage } from './photograph-added-to-digital-

@CommandHandler(AddPhotographToDigitalTextPage)
export class AddPhotographToDigitalTextPageCommandHandler extends BaseUpdateCommandHandler<DigitalText> {
protected fetchRequiredExternalState(_command?: ICommand): Promise<InMemorySnapshot> {
return Promise.resolve(new DeluxeInMemoryStore({}).fetchFullSnapshotInLegacyFormat());
protected async fetchRequiredExternalState({
photographId,
}: AddPhotographToDigitalTextPage): Promise<InMemorySnapshot> {
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -138,7 +140,9 @@ describe(commandType, () => {
checkError: (result) => {
assertErrorAsExpected(
result,
new AggregateNotFoundError(existingPhotograph.getCompositeIdentifier())
new CommandExecutionError([
new AggregateNotFoundError(digitalTextCompositeIdentifier),
])
);
},
});
Expand All @@ -162,7 +166,12 @@ describe(commandType, () => {
checkError: (result) => {
assertErrorAsExpected(
result,
new AggregateNotFoundError(existingPhotograph.getCompositeIdentifier())
new CommandExecutionError([
new InvalidExternalReferenceByAggregateError(
digitalTextCompositeIdentifier,
[existingPhotograph.getCompositeIdentifier()]
),
])
);
},
});
Expand Down

0 comments on commit 54232a5

Please sign in to comment.