| @@ -118,86 +118,87 @@ const makeOrchestrationEngine = Effect.gen(function* () { · 117 unmodified lines |
| 118 | 118 | yield* Deferred.fail(envelope.result, error); |
| 119 | 119 | }); |
| 120 | 120 | |
| 121 | | − const existingReceipt = yield* commandReceiptRepository.getByCommandId({ |
| 122 | | − commandId: envelope.command.commandId, |
| 123 | | − }); |
| 124 | | − if (Option.isSome(existingReceipt)) { |
| 125 | | − if (existingReceipt.value.status === "accepted") { |
| 126 | | − yield* Deferred.succeed(envelope.result, { |
| 127 | | − sequence: existingReceipt.value.resultSequence, |
| 128 | | − }); |
| 121 | + return yield* Effect.gen(function* () { |
| 122 | + const existingReceipt = yield* commandReceiptRepository.getByCommandId({ |
| 123 | + commandId: envelope.command.commandId, |
| 124 | + }); |
| 125 | + if (Option.isSome(existingReceipt)) { |
| 126 | + if (existingReceipt.value.status === "accepted") { |
| 127 | + yield* Deferred.succeed(envelope.result, { |
| 128 | + sequence: existingReceipt.value.resultSequence, |
| 129 | + }); |
| 130 | + return; |
| 131 | + } |
| 132 | + yield* Deferred.fail( |
| 133 | + envelope.result, |
| 134 | + new OrchestrationCommandPreviouslyRejectedError({ |
| 135 | + commandId: envelope.command.commandId, |
| 136 | + detail: existingReceipt.value.error ?? "Previously rejected.", |
| 137 | + }), |
| 138 | + ); |
| 129 | 139 | return; |
| 130 | 140 | } |
| 131 | | − yield* Deferred.fail( |
| 132 | | − envelope.result, |
| 133 | | − new OrchestrationCommandPreviouslyRejectedError({ |
| 134 | | − commandId: envelope.command.commandId, |
| 135 | | − detail: existingReceipt.value.error ?? "Previously rejected.", |
| 136 | | − }), |
| 137 | | − ); |
| 138 | | − return; |
| 139 | | − } |
| 140 | 141 | |
| 141 | | − const eventBase = yield* decideOrchestrationCommand({ |
| 142 | | − command: envelope.command, |
| 143 | | − readModel, |
| 144 | | − }); |
| 145 | | − const eventBases = Array.isArray(eventBase) ? eventBase : [eventBase]; |
| 146 | | − const committedCommand = yield* sql |
| 147 | | − .withTransaction( |
| 148 | | − Effect.gen(function* () { |
| 149 | | − const committedEvents: OrchestrationEvent[] = []; |
| 150 | | − let nextReadModel = readModel; |
| 151 | | − |
| 152 | | − for (const nextEvent of eventBases) { |
| 153 | | − const savedEvent = yield* eventStore.append(nextEvent); |
| 154 | | − nextReadModel = yield* projectEvent(nextReadModel, savedEvent); |
| 155 | | − yield* projectionPipeline.projectEvent(savedEvent); |
| 156 | | − committedEvents.push(savedEvent); |
| 157 | | − } |
| 158 | | − |
| 159 | | − const lastSavedEvent = committedEvents.at(-1) ?? null; |
| 160 | | − if (lastSavedEvent === null) { |
| 161 | | − return yield* new OrchestrationCommandInvariantError({ |
| 162 | | − commandType: envelope.command.type, |
| 163 | | − detail: "Command produced no events.", |
| 142 | + const eventBase = yield* decideOrchestrationCommand({ |
| 143 | + command: envelope.command, |
| 144 | + readModel, |
| 145 | + }); |
| 146 | + const eventBases = Array.isArray(eventBase) ? eventBase : [eventBase]; |
| 147 | + const committedCommand = yield* sql |
| 148 | + .withTransaction( |
| 149 | + Effect.gen(function* () { |
| 150 | + const committedEvents: OrchestrationEvent[] = []; |
| 151 | + let nextReadModel = readModel; |
| 152 | + |
| 153 | + for (const nextEvent of eventBases) { |
| 154 | + const savedEvent = yield* eventStore.append(nextEvent); |
| 155 | + nextReadModel = yield* projectEvent(nextReadModel, savedEvent); |
| 156 | + yield* projectionPipeline.projectEvent(savedEvent); |
| 157 | + committedEvents.push(savedEvent); |
| 158 | + } |
| 159 | + |
| 160 | + const lastSavedEvent = committedEvents.at(-1) ?? null; |
| 161 | + if (lastSavedEvent === null) { |
| 162 | + return yield* new OrchestrationCommandInvariantError({ |
| 163 | + commandType: envelope.command.type, |
| 164 | + detail: "Command produced no events.", |
| 165 | + }); |
| 166 | + } |
| 167 | + |
| 168 | + yield* commandReceiptRepository.upsert({ |
| 169 | + commandId: envelope.command.commandId, |
| 170 | + aggregateKind: lastSavedEvent.aggregateKind, |
| 171 | + aggregateId: lastSavedEvent.aggregateId, |
| 172 | + acceptedAt: lastSavedEvent.occurredAt, |
| 173 | + resultSequence: lastSavedEvent.sequence, |
| 174 | + status: "accepted", |
| 175 | + error: null, |
| 164 | 176 | }); |
| 165 | | − } |
| 166 | 177 | |
| 167 | | − yield* commandReceiptRepository.upsert({ |
| 168 | | − commandId: envelope.command.commandId, |
| 169 | | − aggregateKind: lastSavedEvent.aggregateKind, |
| 170 | | − aggregateId: lastSavedEvent.aggregateId, |
| 171 | | − acceptedAt: lastSavedEvent.occurredAt, |
| 172 | | − resultSequence: lastSavedEvent.sequence, |
| 173 | | − status: "accepted", |
| 174 | | − error: null, |
| 175 | | − }); |
| 176 | | − |
| 177 | | − return { |
| 178 | | − committedEvents, |
| 179 | | − lastSequence: lastSavedEvent.sequence, |
| 180 | | − nextReadModel, |
| 181 | | − } as const; |
| 182 | | − }), |
| 183 | | − ) |
| 184 | | − .pipe( |
| 185 | | − Effect.catchTag("SqlError", (sqlError) => |
| 186 | | − Effect.fail( |
| 187 | | − toPersistenceSqlError("OrchestrationEngine.processEnvelope:transaction")(sqlError), |
| 178 | + return { |
| 179 | + committedEvents, |
| 180 | + lastSequence: lastSavedEvent.sequence, |
| 181 | + nextReadModel, |
| 182 | + } as const; |
| 183 | + }), |
| 184 | + ) |
| 185 | + .pipe( |
| 186 | + Effect.catchTag("SqlError", (sqlError) => |
| 187 | + Effect.fail( |
| 188 | + toPersistenceSqlError("OrchestrationEngine.processEnvelope:transaction")(sqlError), |
| 189 | + ), |
| 188 | 190 | ), |
| 189 | | − ), |
| 190 | | − Effect.catch(handleError), |
| 191 | | − ); |
| 192 | | − if (!committedCommand) { |
| 193 | | − return; |
| 194 | | − } |
| 195 | | − |
| 196 | | − readModel = committedCommand.nextReadModel; |
| 197 | | − for (const event of committedCommand.committedEvents) { |
| 198 | | − yield* PubSub.publish(eventPubSub, event); |
| 199 | | − } |
| 200 | | − yield* Deferred.succeed(envelope.result, { sequence: committedCommand.lastSequence }); |
| 191 | + ); |
| 192 | + if (!committedCommand) { |
| 193 | + return; |
| 194 | + } |
| 195 | + |
| 196 | + readModel = committedCommand.nextReadModel; |
| 197 | + for (const event of committedCommand.committedEvents) { |
| 198 | + yield* PubSub.publish(eventPubSub, event); |
| 199 | + } |
| 200 | + yield* Deferred.succeed(envelope.result, { sequence: committedCommand.lastSequence }); |
| 201 | + }).pipe(Effect.catch(handleError), Effect.asVoid); |
| 201 | 202 | }); |
| 202 | 203 | |
| 203 | 204 | yield* projectionPipeline.bootstrap; |