ホーム 全セッション S06

全体の流れ

14 / 20 目安 1.5分

ページ一覧

解決策: ここまでの処理をつないだ、診察開始の全体の流れを確認します。

課題1〜3を解決する処理を、1本の流れにつなぎます。図の関数は、startExamination.ts の startExaminationWithEffects です(同じファイルにある S04 の startExamination とは別の関数です)。診察開始は、予約を取得し、確認し、イベントを生成し、保存します。保存の工程だけ andThrough でつなぎます。保存の結果を捨てて、event を次の工程へ渡すためです。

値がワークフローを流れる

成功

  1. resolveAppointment が予約を取得します。値は CheckedIn の予約です。
  2. ensureFound の中で ensureAppointmentFound が予約の有無を検証します。予約があるため ok を返し、値をそのまま次へ渡します。
  3. ensureCheckedIn は kind を検証します。ここから値の型は CheckedIn に絞られます。
  4. 時刻と ID はイベントを作る時点で必要なため、startExaminationEvent の中で createEventContext が port から取得します。取得は1回だけで、この1回の値を状態と監査記録の両方が使用します。
  5. startExaminationEvent の中で Appointment.startExamination が ExaminationStarted を生成します。Err を返さない処理であるため map でつなぎます。
  6. andThrough が store を呼びます。結果の ok は破棄し、保存後も必要な event をそのまま次の行へ渡します。
  7. 最後の map が event.aggregateState を取り出します。呼び出し元には InExamination が返ります。

examples/session-06-solution/src/useCase/startExamination.ts:85-90

resolveAppointment(deps)(input.appointmentId)

実行されない

Ok AppointmentState | undefined { kind: "CheckedIn", … }

  .andThen(ensureFound(input.appointmentId))

実行されない

Ok Appointment { kind: "CheckedIn", … }

  .andThen(ensureCheckedIn)

実行されない

Ok CheckedIn { kind: "CheckedIn", … }

  .map(startExaminationEvent(deps, input.veterinarianId))

実行されない

Ok ExaminationStarted { kind: "ExaminationStarted", … }

  .andThrough(deps.examinationStartedStore.store)

実行されない

Ok ExaminationStarted { kind: "ExaminationStarted", … }

Err AppointmentConflict { kind: "AppointmentConflict", … }

reject AppointmentPersistenceError append-audit の書き込みが失敗

examinationStartedStore.ts:186-188

ResultAsync.fromSafePromise(
  Promise.resolve().then(() => storeAtomically(event)),
).andThen((result) => result),
  .map((event) => event.aggregateState);

実行されない

Ok InExamination { kind: "InExamination", … }

examples/session-06-solution/src/adaptor/primary/web/routes.ts:53-55

error.kind === "AppointmentConflict"
  ? "conflict"
  : toStartExaminationNoticeCode(error);

実行されない

応答 Response 303 /?notice=conflict

獣医師の画面

操作のお知らせ

予約がほかの操作によって更新されました 閉じる

examples/session-06-solution/src/app.ts:59

app.onError((_error, context) => context.text("Internal Server Error", 500));

実行されない

応答 Response 500 Internal Server Error

右の札は、その行から次の行へ渡される値です。札「Ok」は成功の Result です。