Skip to content

Commit

Permalink
feat(api): introduce ADD_PHOTOGRAPH_TO_DIGITAL_TEXT_PAGE (#541)
Browse files Browse the repository at this point in the history
* implement DigitalText.addPhotographToPage

* add handler for PHOTOGRAPH_ADDED_TO_DIGITAL_TEXT_PAGE

* add happy path test ADD_PHOTOGRAPH_TO_DIGITAL_TEXT_PAGE

* add invalid test cases

* add type validation test ADD_PHOTOGRAPH_TO_DIGITAL_TEXT_PAGE

* fix tests for ADD_PHOTOGRAPH_TO_DIGITAL_TEXT_PAGE
  • Loading branch information
aaron-plahn authored Jan 31, 2024
1 parent 6629efc commit c66dfa0
Show file tree
Hide file tree
Showing 19 changed files with 808 additions and 4 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
8 changes: 8 additions & 0 deletions apps/api/src/app/controllers/__tests__/createTestModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ import {
ContentAddedToDigitalTextPage,
} from '../../../domain/models/digital-text/commands/add-content-to-digital-text-page';
import { AddPageToDigitalText } from '../../../domain/models/digital-text/commands/add-page-to-digital-text/add-page-to-digital-text.command';
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';
import { CreateMediaItemCommandHandler } from '../../../domain/models/media-item/commands/create-media-item/create-media-item.command-handler';
Expand Down Expand Up @@ -284,6 +289,7 @@ export const buildAllDataClassProviders = () =>
DigitalTextPageContentTranslated,
DigitalTextTitleTranslated,
AudioAddedForDigitalTextPage,
PhotographAddedToDigitalTextPage,
SongCreated,
SongTitleTranslated,
LyricsAddedForSong,
Expand Down Expand Up @@ -578,6 +584,8 @@ export default async (
CreateDigitalTextCommandHandler,
AddPageToDigitalText,
AddPageToDigitalTextCommandHandler,
AddPhotographToDigitalTextPage,
AddPhotographToDigitalTextPageCommandHandler,
TranslateDigitalTextPageContent,
TranslateDigitalTextPageContentCommandHandler,
TranslateDigitalTextPageContent,
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
@@ -0,0 +1,59 @@
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, 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';
import { EventRecordMetadata } from '../../../shared/events/types/EventRecordMetadata';
import { DigitalText } from '../../entities';
import { AddPhotographToDigitalTextPage } from './add-photograph-to-digital-text-page.command';
import { PhotographAddedToDigitalTextPage } from './photograph-added-to-digital-text-page.event';

@CommandHandler(AddPhotographToDigitalTextPage)
export class AddPhotographToDigitalTextPageCommandHandler extends BaseUpdateCommandHandler<DigitalText> {
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(
digitalText: DigitalText,
{ pageIdentifier, photographId }: AddPhotographToDigitalTextPage
): ResultOrError<DigitalText> {
return digitalText.addPhotographToPage(pageIdentifier, photographId);
}

protected validateExternalState(
_state: InMemorySnapshot,
_instance: DigitalText
): InternalError | Valid {
return Valid;
}

protected buildEvent(
payload: AddPhotographToDigitalTextPage,
eventMeta: EventRecordMetadata
): BaseEvent<IEventPayload> {
return new PhotographAddedToDigitalTextPage(payload, eventMeta);
}
}
Loading

0 comments on commit c66dfa0

Please sign in to comment.