会計済みの予約に対する「診察を開始する」は、本来は拒否されるべき操作です。課題1の操作を1回たどり、処理を中断すべきだった箇所を確認します。コードの内容を理解する必要はありません。ハイライトされた行とデータの変化に注目します。
会計が完了した Mugi の予約です。画面の「会計済み」と「Paid」、データベースの
paidは、いずれも同じ状態の表記です。獣医師は別の予約と誤認して、この画面を開いています。獣医師が「診察を開始する」を押します。入口は、予約の ID を受け取り、担当の獣医師の ID と合わせて次の処理へ渡します。受け取る値は ID のみであるため、サーバーはこの時点で予約の状態を把握していません。
ユースケース(1つの業務の処理順序をまとめた関数)が、データベースから Mugi の予約を取得します。状態は
paidであり、この時点でサーバーは会計済みであることを把握します。repositoryは、データベースを読み書きするオブジェクトです。本来はここで処理を中断すべきです。しかし、業務ロジック(操作の可否などを判定する処理)を置く
domain/のupdateStatusは、受け取った値でstatusを上書きするだけで、現在の状態を検証しません。省略した行にも、状態を検証する処理はありません。データベースの予約を、新しい内容で上書きします。
paidはin-examinationに変わり、examinationStartedAtも押した時刻で上書きされます。画面の右の欄で、変わった行が強調表示されます。amountとpaidAtは残っているため、会計済みの予約が診察中の状態になりました。監査記録に、診察を開始した操作を1行追加します。監査記録の
payload(操作後の予約内容を保存する項目)には予約全体が含まれるため、調査に不要な飼い主の氏名、メールアドレス、電話番号も保存されます。これが課題2です。サーバーの応答によって画面が更新され、表示は「診察中」に変わります。診察中の表示を見た受付が会計担当へ未精算と伝え、会計担当が飼い主へ同じ4,800円を再度請求しました。これが課題1です。
画面
Mugi
会計済み- 状態
- Paid
操作
- 受付する
- 診察を開始する
- 診察結果を記録する
- 会計を記録する
コード(見出しは実行中のファイル、ハイライトは実行中の行)
サーバーの処理はまだ開始していません
ここまでに表示した4つのファイルのいずれにも、会計済みの予約を拒否する処理はありませんでした
app.post("/appointments/:appointmentId/start-examination", (context) => {
startExamination(repository)({
appointmentId: context.req.param("appointmentId"),
veterinarianId: clinicFixture.veterinarianId,
});
return redirectToRoot(context);
}); const current = repository.find(input.appointmentId);
if (current === undefined) {
throw new Error(`Appointment not found: ${input.appointmentId}`);
}
const occurredAt = new Date().toISOString();
const updated = updateStatus(current, "in-examination", {
veterinarianId: input.veterinarianId,
examinationStartedAt: occurredAt,
});
repository.save(updated);
repository.appendAudit({
eventId: randomUUID(),
eventName: "examination.started",
occurredAt,
appointment: updated,
}); .where(sql`${appointmentsTable.appointmentId} = ${appointmentId}`)
.get();
return row === undefined ? undefined : (row.state as Appointment); export const updateStatus = (
appointment: Appointment,
status: string,
extra?: AppointmentExtra,
): Appointment => ({ ...appointment, ...extra, status }); .values(row)
.onConflictDoUpdate({
target: appointmentsTable.appointmentId,
set: row,
})
.run(); db.insert(auditLogsTable)
.values({
...event,
appointmentId: appointment.appointmentId,
payload: appointment,
}) 入力
{
appointmentId: "11111111-1111-4111-8111-111111111111",
veterinarianId: "44444444-4444-4444-8444-444444444444",
} 入力
appointmentId: "11111111-1111-4111-8111-111111111111" 出力
current: { petName: "Mugi", status: "paid", amount: 4800, ... } 入力
current: { status: "paid", ... }
status: "in-examination" 状態を検証する処理がない
出力
updated: { status: "in-examination",
amount: 4800, ... } 入力
updated: { status: "in-examination", amount: 4800, ... } 入力
{ eventName: "examination.started", occurredAt, appointment: updated, ... } データベース
現在の予約内容
status: "paid"status: "in-examination"amount: 4800paidAt: "2026-08-30T07:10:00.000Z"examinationStartedAt: 1回目の時刻examinationStartedAt: 押した時刻
監査記録
"appointment.seeded""appointment.checked-in""examination.started""examination.result-recorded""payment.recorded"
- 前の5行は省略
"examination.started"- payload
-
petName: "Mugi" -
status: "in-examination" -
ownerName: "Owner A" -
ownerEmail: "owner@example.test" -
ownerPhone: "090-0000-0000"
押す前
Mugi
会計済み- 状態
- Paid
操作
- 受付する
- 診察を開始する
- 診察結果を記録する
- 会計を記録する
現在の予約内容
-
status: "paid" amount: 4800paidAt: "2026-08-30T07:10:00.000Z"examinationStartedAt: 1回目の時刻
監査記録
"appointment.seeded""appointment.checked-in""examination.started""examination.result-recorded""payment.recorded"
押した後
Mugi
診察中- 状態
- InExamination
操作
- 受付する
- 診察を開始する
- 診察結果を記録する
- 会計を記録する
現在の予約内容
-
status: "paid" → "in-examination" amount: 4800paidAt: "2026-08-30T07:10:00.000Z"examinationStartedAt: 押した時刻
監査記録
"appointment.seeded""appointment.checked-in""examination.started""examination.result-recorded""payment.recorded""examination.started"-
petName: "Mugi" -
status: "in-examination" -
ownerName: "Owner A" -
ownerEmail: "owner@example.test" -
ownerPhone: "090-0000-0000"
app.post("/appointments/:appointmentId/start-examination", (context) => {
startExamination(repository)({
appointmentId: context.req.param("appointmentId"),
veterinarianId: clinicFixture.veterinarianId,
});
return redirectToRoot(context);
}); const current = repository.find(input.appointmentId);
if (current === undefined) {
throw new Error(`Appointment not found: ${input.appointmentId}`);
}
const occurredAt = new Date().toISOString();
const updated = updateStatus(current, "in-examination", {
veterinarianId: input.veterinarianId,
examinationStartedAt: occurredAt,
});
repository.save(updated);
repository.appendAudit({
eventId: randomUUID(),
eventName: "examination.started",
occurredAt,
appointment: updated,
}); -
会計が完了した Mugi の予約です。画面の「会計済み」と「Paid」、データベースの
paidは、いずれも同じ状態の表記です。獣医師は別の予約と誤認して、この画面を開いています。 -
獣医師が「診察を開始する」を押します。入口は、予約の ID を受け取り、担当の獣医師の ID と合わせて次の処理へ渡します。受け取る値は ID のみであるため、サーバーはこの時点で予約の状態を把握していません。
入力
{ appointmentId: "11111111-1111-4111-8111-111111111111", veterinarianId: "44444444-4444-4444-8444-444444444444", } -
ユースケース(1つの業務の処理順序をまとめた関数)が、データベースから Mugi の予約を取得します。状態は
paidであり、この時点でサーバーは会計済みであることを把握します。repositoryは、データベースを読み書きするオブジェクトです。この行が呼ぶ関数
adaptor/secondary/sqlite/appointmentRepository.ts .where(sql`${appointmentsTable.appointmentId} = ${appointmentId}`) .get(); return row === undefined ? undefined : (row.state as Appointment);入力
appointmentId: "11111111-1111-4111-8111-111111111111"出力
current: { petName: "Mugi", status: "paid", amount: 4800, ... } -
本来はここで処理を中断すべきです。しかし、業務ロジック(操作の可否などを判定する処理)を置く
domain/のupdateStatusは、受け取った値でstatusを上書きするだけで、現在の状態を検証しません。省略した行にも、状態を検証する処理はありません。この行が呼ぶ関数
domain/appointment/appointment.ts export const updateStatus = ( appointment: Appointment, status: string, extra?: AppointmentExtra, ): Appointment => ({ ...appointment, ...extra, status });入力
current: { status: "paid", ... } status: "in-examination"状態を検証する処理がない
出力
updated: { status: "in-examination", amount: 4800, ... } -
データベースの予約を、新しい内容で上書きします。
paidはin-examinationに変わり、examinationStartedAtも押した時刻で上書きされます。画面の右の欄で、変わった行が強調表示されます。amountとpaidAtは残っているため、会計済みの予約が診察中の状態になりました。この行が呼ぶ関数
adaptor/secondary/sqlite/appointmentRepository.ts .values(row) .onConflictDoUpdate({ target: appointmentsTable.appointmentId, set: row, }) .run();入力
updated: { status: "in-examination", amount: 4800, ... } -
監査記録に、診察を開始した操作を1行追加します。監査記録の
payload(操作後の予約内容を保存する項目)には予約全体が含まれるため、調査に不要な飼い主の氏名、メールアドレス、電話番号も保存されます。これが課題2です。この行が呼ぶ関数
adaptor/secondary/sqlite/appointmentRepository.ts db.insert(auditLogsTable) .values({ ...event, appointmentId: appointment.appointmentId, payload: appointment, })入力
{ eventName: "examination.started", occurredAt, appointment: updated, ... } -
サーバーの応答によって画面が更新され、表示は「診察中」に変わります。診察中の表示を見た受付が会計担当へ未精算と伝え、会計担当が飼い主へ同じ4,800円を再度請求しました。これが課題1です。