diff --git a/api/src/service/domain/workflow/workflowitem_eventsourcing.ts b/api/src/service/domain/workflow/workflowitem_eventsourcing.ts index 98d171a3d..573d76cf7 100644 --- a/api/src/service/domain/workflow/workflowitem_eventsourcing.ts +++ b/api/src/service/domain/workflow/workflowitem_eventsourcing.ts @@ -18,7 +18,7 @@ import * as WorkflowitemUpdated from "./workflowitem_updated"; export function sourceWorkflowitemFromSnapshot( ctx: Ctx, events: BusinessEvent[], - withLog: boolean, + _withLog: boolean, workflowitemJson?, ): Result.Type { let workflowitem; @@ -36,9 +36,7 @@ export function sourceWorkflowitemFromSnapshot( } const workflowitemResult = sourceEventFromSnapshot(ctx, event, workflowitem); if (Result.isOk(workflowitemResult)) { - if (withLog) { - workflowitemResult.log.push(newTraceEvent(workflowitemResult, event)); - } + workflowitemResult.log.push(newTraceEvent(workflowitemResult, event)); workflowitem = workflowitemResult; } } @@ -108,35 +106,6 @@ export function parseWorkflowitemFromSnapshot(workflowitemJson): Workflowitem.Wo }; } -export function sourceWorkflowitems( - ctx: Ctx, - events: BusinessEvent[], - origin?: Map, -): { workflowitems: Workflowitem.Workflowitem[]; errors: Error[] } { - const items = - origin === undefined - ? new Map() - : new Map(origin); - const errors: Error[] = []; - - for (const event of events) { - logger.trace({ event }, "Validating workflowitem event by applying it"); - if (!event.type.startsWith("workflowitem_")) { - continue; - } - - const workflowitem = sourceEvent(ctx, event, items); - if (Result.isErr(workflowitem)) { - errors.push(workflowitem); - } else { - workflowitem.log.push(newTraceEvent(workflowitem, event)); - items.set(workflowitem.id, workflowitem); - } - } - - return { workflowitems: [...items.values()], errors }; -} - function newTraceEvent( workflowitem: Workflowitem.Workflowitem, event: BusinessEvent, @@ -154,46 +123,6 @@ function newTraceEvent( }; } -function sourceEvent( - ctx: Ctx, - event: BusinessEvent, - workflowitems: Map, -): Result.Type { - const workflowitemId = getWorkflowitemId(event); - let workflowitem: Result.Type; - if (Result.isOk(workflowitemId)) { - // The event refers to an existing workflowitem, so - // the workflowitem should have been initialized already. - - workflowitem = get(workflowitems, workflowitemId); - if (Result.isErr(workflowitem)) { - return new VError( - `workflowitem ID ${workflowitemId} found in event ${event.type} is invalid`, - ); - } - - workflowitem = newWorkflowitemFromEvent(ctx, workflowitem, event); - if (Result.isErr(workflowitem)) { - return workflowitem; // <- event-sourcing error - } - } else { - // The event does not refer to a workflowitem ID, so it must be a creation event: - if (event.type !== "workflowitem_created") { - return new VError( - `event ${event.type} is not of type "workflowitem_created" but also ` + - "does not include a workflowitem ID", - ); - } - - workflowitem = WorkflowitemCreated.createFrom(ctx, event); - if (Result.isErr(workflowitem)) { - return new VError(workflowitem, "could not create workflowitem from event"); - } - } - - return workflowitem; -} - function get( workflowitems: Map, workflowitemId: Workflowitem.Id, @@ -264,6 +193,7 @@ export function newWorkflowitemFromEvent( type EventModule = { mutate: (workflowitem: Workflowitem.Workflowitem, event: BusinessEvent) => Result.Type; }; + function getEventModule(event: BusinessEvent): Result.Type { switch (event.type) { case "workflowitem_document_validated": diff --git a/api/src/service/domain/workflow/workflowitem_ordering.ts b/api/src/service/domain/workflow/workflowitem_ordering.ts index e67ef7326..3449aba7b 100644 --- a/api/src/service/domain/workflow/workflowitem_ordering.ts +++ b/api/src/service/domain/workflow/workflowitem_ordering.ts @@ -1,5 +1,6 @@ import { AssertionError } from "assert"; import * as Workflowitem from "./workflowitem"; +import logger from "../../../lib/logger"; export type WorkflowitemOrdering = Workflowitem.Id[]; @@ -73,7 +74,11 @@ function closedAt(item: Workflowitem.ScrubbedWorkflowitem): string | AssertionEr const traceEvent = item.log.find((e) => e.businessEvent.type === "workflowitem_closed"); if (traceEvent === undefined || traceEvent.businessEvent.type !== "workflowitem_closed") { - return new AssertionError({ message: `Expected close event for workflowitem ${item.id}` }); + const error = new AssertionError({ + message: `Expected close event for workflowitem ${item.id}`, + }); + logger.error(error, error.message); + return error; } const closeEvent = traceEvent.businessEvent;