-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extended model by payment and charge id. Nested api to make it more readable.
- Loading branch information
1 parent
56699d7
commit f94ec1b
Showing
8 changed files
with
132 additions
and
7 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import type { GuestStayAccountEvent } from './guestStayAccount'; | ||
|
||
export type NotExisting = { status: 'NotExisting' }; | ||
|
||
export type Opened = { | ||
id: string; | ||
guestId: string; | ||
roomId: string; | ||
status: 'Opened' | 'CheckedOut'; | ||
balance: number; | ||
transactionsCount: number; | ||
transactions: { id: string; amount: number }[]; | ||
checkedInAt: Date; | ||
checkedOutAt?: Date; | ||
}; | ||
|
||
export type GuestStayDetails = NotExisting | Opened; | ||
|
||
export const initialState = (): GuestStayDetails => ({ | ||
status: 'NotExisting', | ||
}); | ||
|
||
export const evolve = ( | ||
state: GuestStayDetails, | ||
{ type, data: event }: GuestStayAccountEvent, | ||
): GuestStayDetails => { | ||
switch (type) { | ||
case 'GuestCheckedIn': { | ||
return state.status === 'NotExisting' | ||
? { | ||
id: event.guestStayAccountId, | ||
guestId: event.guestId, | ||
roomId: event.roomId, | ||
status: 'Opened', | ||
balance: 0, | ||
transactionsCount: 0, | ||
transactions: [], | ||
checkedInAt: event.checkedInAt, | ||
} | ||
: state; | ||
} | ||
case 'ChargeRecorded': { | ||
return state.status === 'Opened' | ||
? { | ||
...state, | ||
balance: state.balance - event.amount, | ||
transactionsCount: state.transactionsCount + 1, | ||
transactions: [ | ||
...state.transactions, | ||
{ id: event.chargeId, amount: event.amount }, | ||
], | ||
} | ||
: state; | ||
} | ||
case 'PaymentRecorded': { | ||
return state.status === 'Opened' | ||
? { | ||
...state, | ||
balance: state.balance + event.amount, | ||
transactionsCount: state.transactionsCount + 1, | ||
transactions: [ | ||
...state.transactions, | ||
{ id: event.paymentId, amount: event.amount }, | ||
], | ||
} | ||
: state; | ||
} | ||
case 'GuestCheckedOut': { | ||
return state.status === 'Opened' | ||
? { | ||
...state, | ||
status: 'CheckedOut', | ||
checkedOutAt: event.checkedOutAt, | ||
} | ||
: state; | ||
} | ||
case 'GuestCheckoutFailed': { | ||
return state; | ||
} | ||
default: { | ||
const _notExistingEventType: never = type; | ||
return state; | ||
} | ||
} | ||
}; |
Oops, something went wrong.