参照実装はデータベースの応答を待つため ResultAsync を使います。ResultAsync は、データベースの応答を待ってから Ok か Err になる Result です。連結の方法は、記録して返す工程の andThrough を除いて、S04 の Result と同じです。createEvent は Err を返しませんが、ResultAsync を返すため、map ではなく andThen で連結します。
-
このコマのコード
cancelAppointmentUseCase.tsdependencies.userResolver .resolveById(input.actorUserId) .andThen(ensureUserFound(input.actorUserId)) .andThen(ensureCanManageClinic) .andThen(() => dependencies.appointmentResolver.resolveById(input.appointmentId), ) .andThen(ensureAppointmentFound(input.appointmentId)) .andThen(ensureCancellable) .andThen(createEvent(dependencies, input)) .andThrough(dependencies.appointmentCanceledStore.store) .map((event) => ({ appointment: event.aggregateState }));手前の境界: 入力を検査する レールには乗らない
成功失敗- 実行者の確認
resolveById -
ensureUserFound -
ensureCanManageClinic - 予約の確認
resolveById -
ensureAppointmentFound -
ensureCancellable - 結果を作る
createEvent - 記録して返す
andThrough(store) -
map - 終点
UseCaseOutput
useCase 全体は、
andThen、andThrough、mapを連結した1本の連鎖です。実行者の確認、予約の確認、結果を作る、記録して返す、の順に進みます。次のコマから、値の流れを1行ずつ追跡します。 - 実行者の確認
-
このコマのコード
cancellationReason.tsconst CancellationReasonSchema = z .string() .trim() .min(1) .max(500) .brand<typeof CancellationReasonBrand>() .transform(Sensitive.of);受け取る値
{ reason: "owner request" }返す値
{ …, reason: "owner request" }手前の境界: 入力を検査する レールには乗らない
成功失敗- 実行者の確認
resolveById -
ensureUserFound -
ensureCanManageClinic - 予約の確認
resolveById -
ensureAppointmentFound -
ensureCancellable - 結果を作る
createEvent - 記録して返す
andThrough(store) -
map - 終点
UseCaseOutput
useCase の手前の境界が、リクエストを検査します。ここはレールの外です。理由は空ではないため、
.min(1)を通過します。 - 実行者の確認
-
このコマのコード
userResolver.tsデータベースから実行者を取得します。実装は省略します。
受け取る値
"(受付担当者の ID)"返す値
Ok({ kind: "Receptionist", … })手前の境界: 入力を検査する レールには乗らない
成功失敗- Ok 実行者の確認
resolveById -
ensureUserFound -
ensureCanManageClinic - 予約の確認
resolveById -
ensureAppointmentFound -
ensureCancellable - 結果を作る
createEvent - 記録して返す
andThrough(store) -
map - 終点
UseCaseOutput
resolveByIdが、データベースから実行者を取得します。 - Ok 実行者の確認
-
このコマのコード
errors.tsexport const ensureUserFound = ( actorUserId: UserId, ) => (user: User | undefined): Result<User, UnauthorizedError> => user === undefined ? err({ kind: "Unauthorized", actorUserId }) : ok(user);受け取る値
{ kind: "Receptionist", … }返す値
Ok({ kind: "Receptionist", … })手前の境界: 入力を検査する レールには乗らない
成功失敗- 実行者の確認
resolveById - Ok
ensureUserFound -
ensureCanManageClinic - 予約の確認
resolveById -
ensureAppointmentFound -
ensureCancellable - 結果を作る
createEvent - 記録して返す
andThrough(store) -
map - 終点
UseCaseOutput
ensureUserFoundは、実行者を取得できたためokを返します。 - 実行者の確認
-
このコマのコード
authorization.tsexport const ensureCanManageClinic = ( user: User, ): Result<User, UnauthorizedError> => user.kind === "Admin" || user.kind === "Receptionist" ? ok(user) : err({ kind: "Unauthorized", actorUserId: user.userId });受け取る値
{ kind: "Receptionist", … }返す値
Ok({ kind: "Receptionist", … })手前の境界: 入力を検査する レールには乗らない
成功失敗- 実行者の確認
resolveById -
ensureUserFound - Ok
ensureCanManageClinic - 予約の確認
resolveById -
ensureAppointmentFound -
ensureCancellable - 結果を作る
createEvent - 記録して返す
andThrough(store) -
map - 終点
UseCaseOutput
ensureCanManageClinicが、権限を検証します。Receptionistには権限があります。 - 実行者の確認
-
このコマのコード
appointmentResolver.tsデータベースから予約を取得します。実装は省略します。
受け取る値
"11111111-…"返す値
Ok({ kind: "Scheduled", … })手前の境界: 入力を検査する レールには乗らない
成功失敗- 実行者の確認
resolveById -
ensureUserFound -
ensureCanManageClinic - Ok 予約の確認
resolveById -
ensureAppointmentFound -
ensureCancellable - 結果を作る
createEvent - 記録して返す
andThrough(store) -
map - 終点
UseCaseOutput
resolveByIdが、予約を取得します。値は実行者から予約へ変わります。 - 実行者の確認
-
このコマのコード
errors.tsexport const ensureAppointmentFound = ( appointmentId: AppointmentId, ) => (appointment: Appointment | undefined): Result<Appointment, AppointmentNotFound> => appointment === undefined ? err({ kind: "AppointmentNotFound", appointmentId }) : ok(appointment);受け取る値
{ kind: "Scheduled", … }返す値
Ok({ kind: "Scheduled", … })手前の境界: 入力を検査する レールには乗らない
成功失敗- 実行者の確認
resolveById -
ensureUserFound -
ensureCanManageClinic - 予約の確認
resolveById - Ok
ensureAppointmentFound -
ensureCancellable - 結果を作る
createEvent - 記録して返す
andThrough(store) -
map - 終点
UseCaseOutput
ensureAppointmentFoundは、予約を取得できたためokを返します。 - 実行者の確認
-
このコマのコード
cancelAppointmentUseCase.tsappointment.kind === "Scheduled" || appointment.kind === "CheckedIn" ? ok(appointment) : err({ kind: "InvalidAppointmentState", appointmentId: appointment.appointmentId, expectedKind: "ScheduledOrCheckedIn", actualKind: appointment.kind, });受け取る値
{ kind: "Scheduled", … }返す値
Ok({ kind: "Scheduled", … })手前の境界: 入力を検査する レールには乗らない
成功失敗- 実行者の確認
resolveById -
ensureUserFound -
ensureCanManageClinic - 予約の確認
resolveById -
ensureAppointmentFound - Ok
ensureCancellable - 結果を作る
createEvent - 記録して返す
andThrough(store) -
map - 終点
UseCaseOutput
ensureCancellableが、状態を検証します。Scheduledは診察開始前であるため、okを返します。err のactualKindは S04 のactualと同じ現在の状態で、expectedKindは通過できる状態です。 - 実行者の確認
-
このコマのコード
cancelAppointmentUseCase.tsconst createEvent = (dependencies: Dependencies, input: UseCaseInput) => (appointment: Scheduled | CheckedIn) => ResultAsync.fromSafePromise( Promise.resolve().then(() => Appointment.cancel({ eventId: dependencies.eventIdGenerator.generate(), occurredAt: dependencies.clock.now(), actorUserId: input.actorUserId, })(appointment, input.reason),受け取る値
{ kind: "Scheduled", … }返す値
Ok({ kind: "AppointmentCanceled" })手前の境界: 入力を検査する レールには乗らない
成功失敗- 実行者の確認
resolveById -
ensureUserFound -
ensureCanManageClinic - 予約の確認
resolveById -
ensureAppointmentFound -
ensureCancellable - Ok 結果を作る
createEvent - 記録して返す
andThrough(store) -
map - 終点
UseCaseOutput
createEventが、時計から時刻を、ID の生成器から ID を取得し、入力のreasonとともにAppointment.cancelへ渡します。前後の工程と同じ ResultAsync の連鎖につなぐために、ResultAsync.fromSafePromiseが、処理の結果を、Err にならない ResultAsync に包みます。 - 実行者の確認
-
このコマのコード
appointment.tsconst aggregateState = { kind: "Canceled", appointmentId: appointment.appointmentId, petId: appointment.petId, ownerId: appointment.ownerId, scheduledAt: appointment.scheduledAt, reason: appointment.reason, cancellationReason, canceledAt: context.occurredAt, } as const satisfies Canceled;受け取る値
予約、"owner request"、context返す値
{ kind: "AppointmentCanceled" }手前の境界: 入力を検査する レールには乗らない
成功失敗- 実行者の確認
resolveById -
ensureUserFound -
ensureCanManageClinic - 予約の確認
resolveById -
ensureAppointmentFound -
ensureCancellable - Ok 結果を作る
createEvent - 記録して返す
andThrough(store) -
map - 終点
UseCaseOutput
Appointment.cancelは、渡された値だけでAppointmentCanceledを作ります。背景で見た3項目が変わり、時刻はcontext.occurredAtから取ります。 - 実行者の確認
-
このコマのコード
appointmentEventStore.ts予約の状態の更新と、イベントの追記を、1回の保存で行います。実装は省略します。
受け取る値
{ kind: "AppointmentCanceled" }返す値
Ok({ kind: "AppointmentCanceled" })手前の境界: 入力を検査する レールには乗らない
成功失敗- 実行者の確認
resolveById -
ensureUserFound -
ensureCanManageClinic - 予約の確認
resolveById -
ensureAppointmentFound -
ensureCancellable - 結果を作る
createEvent - Ok 記録して返す
andThrough(store) -
map - 終点
UseCaseOutput
andThroughは、Ok の場合だけ store を呼び、値をそのまま次へ渡します。予約の更新とイベントの追記を、1回の保存で行います。 - 実行者の確認
-
このコマのコード
cancelAppointmentUseCase.ts.map((event) => ({ appointment: event.aggregateState }));受け取る値
{ kind: "AppointmentCanceled" }返す値
Ok({ appointment: { kind: "Canceled" } })手前の境界: 入力を検査する レールには乗らない
成功失敗- 実行者の確認
resolveById -
ensureUserFound -
ensureCanManageClinic - 予約の確認
resolveById -
ensureAppointmentFound -
ensureCancellable - 結果を作る
createEvent - 記録して返す
andThrough(store) - Ok
map - 終点
UseCaseOutput
mapが、イベントからキャンセル後の予約を取り出します。 - 実行者の確認
-
このコマのコード
cancelAppointmentUseCase.tsexport type UseCaseError = | UnauthorizedError | AppointmentNotFound | InvalidAppointmentState | AppointmentConflict; export type UseCaseOutput = UseResultAsync<UseCaseOk, UseCaseError>;受け取る値
{ appointment: { kind: "Canceled" } }返す値
Ok({ appointment: { kind: "Canceled" } })手前の境界: 入力を検査する レールには乗らない
成功失敗- 実行者の確認
resolveById -
ensureUserFound -
ensureCanManageClinic - 予約の確認
resolveById -
ensureAppointmentFound -
ensureCancellable - 結果を作る
createEvent - 記録して返す
andThrough(store) -
map - Ok 終点
UseCaseOutput
値は成功レールの終点に到達します。戻り値の型
UseCaseOutputは、成功の型UseCaseOkと失敗の型UseCaseErrorを持つResultAsyncです(UseResultAsyncは別名)。UseCaseErrorは、工程の失敗のユニオン型です。 - 実行者の確認
確認の問い
問い: キャンセル前とキャンセル後の予約のデータを比較してください。変わる項目と変わらない項目は、それぞれどれですか。
答え:
appointmentId、scheduledAt、reasonは変わりません。kindはScheduledからCanceledへ変わり、cancellationReasonとcanceledAtが追加されます。来院理由はreasonに保持され、キャンセル理由はcancellationReasonに格納されます。