Skip to content

Adjusted sample to showcase idempotence handling #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/guestStayAccounts/api/api.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -147,10 +147,10 @@ void describe('guestStayAccount E2E', () => {
void describe('When checked in', () => {
const checkedInAccount: TestRequest = checkIn;

void it(`doesn't check in`, () =>
void it(`ignores check in`, () =>
given(checkedInAccount)
.when(checkIn)
.then([expectError(403, { detail: `Guest is already checked-in!` })]));
.then([expectResponse(201)]));

void it('records charge', () =>
given(checkedInAccount)
@@ -299,10 +299,10 @@ void describe('guestStayAccount E2E', () => {
expectError(403, { detail: `Guest account is already checked out` }),
]));

void it(`doesn't checkout`, () =>
void it(`ignores checkout`, () =>
given(...checkedOutAccount)
.when(checkOut)
.then([expectError(403, { detail: `NotCheckedIn` })]));
.then([expectResponse(204)]));

void it(`details return 404`, () =>
given(...checkedOutAccount)
23 changes: 4 additions & 19 deletions src/guestStayAccounts/api/api.int.spec.ts
Original file line number Diff line number Diff line change
@@ -126,10 +126,8 @@ void describe('Guest stay account', () => {
])),
);

void it(`doesn't check in`, () =>
given(checkedInAccount)
.when(checkIn)
.then(expectError(403, { detail: `Guest is already checked-in!` })));
void it(`ingores check in`, () =>
given(checkedInAccount).when(checkIn).then(expectResponse(201)));

void it('records charge', () =>
given(checkedInAccount)
@@ -422,23 +420,10 @@ void describe('Guest stay account', () => {
expectError(403, { detail: `Guest account is already checked out` }),
));

void it(`doesn't checkout`, () =>
void it(`ignores check out`, () =>
given(checkedOutAccount)
.when(checkOut)
.then([
expectError(403, { detail: `NotCheckedIn` }),
expectNewEvents(guestStayAccountId, [
{
type: 'GuestCheckoutFailed',
data: {
guestStayAccountId,
groupCheckoutId: undefined,
reason: 'NotCheckedIn',
failedAt: now,
},
},
]),
]));
.then([expectResponse(204)]));
});

const given = ApiSpecification.for<GuestStayAccountEvent>(
13 changes: 7 additions & 6 deletions src/guestStayAccounts/api/api.ts
Original file line number Diff line number Diff line change
@@ -168,16 +168,17 @@ export const guestStayAccountsApi =
metadata: { now: getCurrentTime() },
};

const {
newEvents: [event],
} = await handle(eventStore, guestStayAccountId, (state) =>
checkOut(command, state),
const { newEvents } = await handle(
eventStore,
guestStayAccountId,
(state) => checkOut(command, state),
);

return event.type !== 'GuestCheckoutFailed'
return newEvents.length === 0 ||
newEvents[0].type !== 'GuestCheckoutFailed'
? NoContent()
: Forbidden({
problemDetails: event.data.reason,
problemDetails: newEvents[0].data.reason,
});
}),
);
23 changes: 9 additions & 14 deletions src/guestStayAccounts/businessLogic.ts
Original file line number Diff line number Diff line change
@@ -54,8 +54,11 @@ export type GuestStayCommand =
export const checkIn = (
{ data: { guestId, roomId }, metadata }: CheckIn,
state: GuestStayAccount,
): GuestCheckedIn => {
assertDoesNotExist(state);
): GuestCheckedIn | [] => {
if (state.status === 'CheckedIn') [];

if (state.status === 'CheckedOut')
throw new IllegalStateError(`Guest account is already checked out`);

const now = metadata?.now ?? new Date();

@@ -107,7 +110,9 @@ export const recordPayment = (
export const checkOut = (
{ data: { guestStayAccountId, groupCheckoutId }, metadata }: CheckOut,
state: GuestStayAccount,
): GuestCheckedOut | GuestCheckoutFailed => {
): GuestCheckedOut | GuestCheckoutFailed | [] => {
if (state.status === 'CheckedOut') return [];

const now = metadata?.now ?? new Date();

if (state.status !== 'CheckedIn')
@@ -146,7 +151,7 @@ export const checkOut = (
export const decide = (
command: GuestStayCommand,
state: GuestStayAccount,
): GuestStayAccountEvent => {
): GuestStayAccountEvent | GuestStayAccountEvent[] => {
const { type } = command;

switch (type) {
@@ -165,16 +170,6 @@ export const decide = (
}
};

const assertDoesNotExist = (state: GuestStayAccount): state is CheckedIn => {
if (state.status === 'CheckedIn')
throw new IllegalStateError(`Guest is already checked-in!`);

if (state.status === 'CheckedOut')
throw new IllegalStateError(`Guest account is already checked out`);

return true;
};

const assertIsCheckedIn = (state: GuestStayAccount): state is CheckedIn => {
if (state.status === 'NotExisting')
throw new IllegalStateError(`Guest account doesn't exist!`);
20 changes: 4 additions & 16 deletions src/guestStayAccounts/businessLogic.unit.spec.ts
Original file line number Diff line number Diff line change
@@ -121,7 +121,7 @@ void describe('Guest Stay Account', () => {
},
];

void it(`doesn't check in`, () =>
void it(`ignores check in`, () =>
given(checkedInAccount)
.when({
type: 'CheckIn',
@@ -131,9 +131,7 @@ void describe('Guest Stay Account', () => {
},
metadata: { now },
})
.thenThrows<IllegalStateError>(
(error) => error.message === `Guest is already checked-in!`,
));
.then([]));

void it('records charge', () => {
given(checkedInAccount)
@@ -476,7 +474,7 @@ void describe('Guest Stay Account', () => {
(error) => error.message === `Guest account is already checked out`,
));

void it(`doesn't checkout`, () =>
void it(`ignores check out`, () =>
given(checkedOutAccount)
.when({
type: 'CheckOut',
@@ -485,16 +483,6 @@ void describe('Guest Stay Account', () => {
},
metadata: { now },
})
.then([
{
type: 'GuestCheckoutFailed',
data: {
guestStayAccountId,
groupCheckoutId: undefined,
reason: 'NotCheckedIn',
failedAt: now,
},
},
]));
.then([]));
});
});