Skip to content

Commit

Permalink
Bumped Emmett to 0.11.0 and adjusted
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Jun 13, 2024
1 parent a79b2ae commit 7be3fef
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 56 deletions.
36 changes: 18 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
},
"homepage": "https://github.com/oskardudycz/EventSourcing.NodeJS#readme",
"dependencies": {
"@event-driven-io/emmett-esdb": "0.10.0",
"@event-driven-io/emmett-expressjs": "0.10.0",
"@event-driven-io/emmett-testcontainers": "0.10.0"
"@event-driven-io/emmett-esdb": "0.11.0",
"@event-driven-io/emmett-expressjs": "0.11.0",
"@event-driven-io/emmett-testcontainers": "0.11.0"
},
"devDependencies": {
"@types/node": "20.11.30",
Expand Down
2 changes: 1 addition & 1 deletion src/guestStayAccounts/api/api.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,6 @@ describe('guestStayAccount E2E', () => {
void it(`doesn't checkout`, () =>
given(...checkedOutAccount)
.when(checkOut)
.then([expectError(403, { detail: `NotOpened` })]));
.then([expectError(403, { detail: `NotCheckedIn` })]));
});
});
6 changes: 3 additions & 3 deletions src/guestStayAccounts/api/api.int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
formatDateToUtcYYYYMMDD,
getInMemoryEventStore,
type EventStore,
type TestEventStream,
} from '@event-driven-io/emmett';
import {
ApiSpecification,
Expand All @@ -12,7 +13,6 @@ import {
getApplication,
type TestRequest,
} from '@event-driven-io/emmett-expressjs';
import type { TestEventStream } from '@event-driven-io/emmett-expressjs/dist/testing/utils';
import { randomUUID } from 'node:crypto';
import { beforeEach, describe, it } from 'node:test';
import {
Expand Down Expand Up @@ -426,14 +426,14 @@ void describe('Guest stay account', () => {
given(checkedOutAccount)
.when(checkOut)
.then([
expectError(403, { detail: `NotOpened` }),
expectError(403, { detail: `NotCheckedIn` }),
expectNewEvents(guestStayAccountId, [
{
type: 'GuestCheckoutFailed',
data: {
guestStayAccountId,
groupCheckoutId: undefined,
reason: 'NotOpened',
reason: 'NotCheckedIn',
failedAt: now,
},
},
Expand Down
10 changes: 5 additions & 5 deletions src/guestStayAccounts/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type CheckOutRequest = Request<
unknown
>;

type GetShoppingCartRequest = Request<
type GetGuestStayAccountDetailsRequest = Request<
Partial<{ guestId: string; roomId: string; checkInDate: string }>,
unknown,
unknown
Expand Down Expand Up @@ -153,7 +153,7 @@ export const guestStayAccountsApi =
}),
);

// CheckOut Shopping Cart
// CheckOut Guest
router.delete(
'/guests/:guestId/stays/:roomId/periods/:checkInDate',
on(async (request: CheckOutRequest) => {
Expand All @@ -179,10 +179,10 @@ export const guestStayAccountsApi =
}),
);

// Get Shopping Cart
// Get Guest Stay Account Details
router.get(
'/guests/:guestId/stays/:roomId/periods/:checkInDate',
on(async (request: GetShoppingCartRequest) => {
on(async (request: GetGuestStayAccountDetailsRequest) => {
const guestStayAccountId = parseGuestStayAccountId(request.params);

const result = await getGuestStayDetails(
Expand All @@ -192,7 +192,7 @@ export const guestStayAccountsApi =

if (result === null) return NotFound();

if (result.state.status !== 'Opened') return NotFound();
if (result.state.status !== 'CheckedIn') return NotFound();

return OK({
body: result.state,
Expand Down
16 changes: 8 additions & 8 deletions src/guestStayAccounts/businessLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { IllegalStateError, type Command } from '@event-driven-io/emmett';
import {
toGuestStayAccountId,
type ChargeRecorded,
type CheckedIn,
type GuestCheckedIn,
type GuestCheckedOut,
type GuestCheckoutFailed,
type GuestStayAccount,
type GuestStayAccountEvent,
type Opened,
type PaymentRecorded,
} from './guestStayAccount';

Expand Down Expand Up @@ -74,7 +74,7 @@ export const recordCharge = (
{ data: { guestStayAccountId, chargeId, amount }, metadata }: RecordCharge,
state: GuestStayAccount,
): ChargeRecorded => {
assertIsOpened(state);
assertIsCheckedIn(state);

return {
type: 'ChargeRecorded',
Expand All @@ -91,7 +91,7 @@ export const recordPayment = (
{ data: { guestStayAccountId, paymentId, amount }, metadata }: RecordPayment,
state: GuestStayAccount,
): PaymentRecorded => {
assertIsOpened(state);
assertIsCheckedIn(state);

return {
type: 'PaymentRecorded',
Expand All @@ -110,13 +110,13 @@ export const checkOut = (
): GuestCheckedOut | GuestCheckoutFailed => {
const now = metadata?.now ?? new Date();

if (state.status !== 'Opened')
if (state.status !== 'CheckedIn')
return {
type: 'GuestCheckoutFailed',
data: {
guestStayAccountId,
groupCheckoutId,
reason: 'NotOpened',
reason: 'NotCheckedIn',
failedAt: now,
},
};
Expand Down Expand Up @@ -165,8 +165,8 @@ export const decide = (
}
};

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

if (state.status === 'CheckedOut')
Expand All @@ -175,7 +175,7 @@ const assertDoesNotExist = (state: GuestStayAccount): state is Opened => {
return true;
};

const assertIsOpened = (state: GuestStayAccount): state is Opened => {
const assertIsCheckedIn = (state: GuestStayAccount): state is CheckedIn => {
if (state.status === 'NotExisting')
throw new IllegalStateError(`Guest account doesn't exist!`);

Expand Down
4 changes: 2 additions & 2 deletions src/guestStayAccounts/businessLogic.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void describe('Guest Stay Account', () => {
data: {
guestStayAccountId,
groupCheckoutId: undefined,
reason: 'NotOpened',
reason: 'NotCheckedIn',
failedAt: now,
},
},
Expand Down Expand Up @@ -491,7 +491,7 @@ void describe('Guest Stay Account', () => {
data: {
guestStayAccountId,
groupCheckoutId: undefined,
reason: 'NotOpened',
reason: 'NotCheckedIn',
failedAt: now,
},
},
Expand Down
14 changes: 7 additions & 7 deletions src/guestStayAccounts/guestStayAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export type GuestCheckoutFailed = Event<
'GuestCheckoutFailed',
{
guestStayAccountId: string;
reason: 'NotOpened' | 'BalanceNotSettled';
reason: 'NotCheckedIn' | 'BalanceNotSettled';
failedAt: Date;
groupCheckoutId?: string;
}
Expand All @@ -66,11 +66,11 @@ export type GuestStayAccountEvent =

export type NotExisting = { status: 'NotExisting' };

export type Opened = { status: 'Opened'; balance: number };
export type CheckedIn = { status: 'CheckedIn'; balance: number };

export type CheckedOut = { status: 'CheckedOut' };

export type GuestStayAccount = NotExisting | Opened | CheckedOut;
export type GuestStayAccount = NotExisting | CheckedIn | CheckedOut;

export const initialState = (): GuestStayAccount => ({
status: 'NotExisting',
Expand All @@ -93,27 +93,27 @@ export const evolve = (
switch (type) {
case 'GuestCheckedIn': {
return state.status === 'NotExisting'
? { status: 'Opened', balance: 0 }
? { status: 'CheckedIn', balance: 0 }
: state;
}
case 'ChargeRecorded': {
return state.status === 'Opened'
return state.status === 'CheckedIn'
? {
...state,
balance: state.balance - event.amount,
}
: state;
}
case 'PaymentRecorded': {
return state.status === 'Opened'
return state.status === 'CheckedIn'
? {
...state,
balance: state.balance + event.amount,
}
: state;
}
case 'GuestCheckedOut': {
return state.status === 'Opened' ? { status: 'CheckedOut' } : state;
return state.status === 'CheckedIn' ? { status: 'CheckedOut' } : state;
}
case 'GuestCheckoutFailed': {
return state;
Expand Down
14 changes: 7 additions & 7 deletions src/guestStayAccounts/guestStayDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import type { GuestStayAccountEvent } from './guestStayAccount';

export type NotExisting = { status: 'NotExisting' };

export type Opened = {
export type CheckedIn = {
id: string;
guestId: string;
roomId: string;
status: 'Opened' | 'CheckedOut';
status: 'CheckedIn' | 'CheckedOut';
balance: number;
transactionsCount: number;
transactions: { id: string; amount: number }[];
checkedInAt: Date;
checkedOutAt?: Date;
};

export type GuestStayDetails = NotExisting | Opened;
export type GuestStayDetails = NotExisting | CheckedIn;

export const initialState = (): GuestStayDetails => ({
status: 'NotExisting',
Expand All @@ -32,7 +32,7 @@ export const evolve = (
id: event.guestStayAccountId,
guestId: event.guestId,
roomId: event.roomId,
status: 'Opened',
status: 'CheckedIn',
balance: 0,
transactionsCount: 0,
transactions: [],
Expand All @@ -41,7 +41,7 @@ export const evolve = (
: state;
}
case 'ChargeRecorded': {
return state.status === 'Opened'
return state.status === 'CheckedIn'
? {
...state,
balance: state.balance - event.amount,
Expand All @@ -54,7 +54,7 @@ export const evolve = (
: state;
}
case 'PaymentRecorded': {
return state.status === 'Opened'
return state.status === 'CheckedIn'
? {
...state,
balance: state.balance + event.amount,
Expand All @@ -67,7 +67,7 @@ export const evolve = (
: state;
}
case 'GuestCheckedOut': {
return state.status === 'Opened'
return state.status === 'CheckedIn'
? {
...state,
status: 'CheckedOut',
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ const eventStore = getEventStoreDBEventStore(eventStoreDBClient);
const doesGuestStayExist = (_guestId: string, _roomId: string, _day: Date) =>
Promise.resolve(true);

const shoppingCarts = guestStayAccountsApi(
const guestStayAccounts = guestStayAccountsApi(
eventStore,
doesGuestStayExist,
(prefix) => `${prefix}-${randomUUID()}`,
() => new Date(),
);

const application: Application = getApplication({
apis: [shoppingCarts],
apis: [guestStayAccounts],
});

startAPI(application);

0 comments on commit 7be3fef

Please sign in to comment.