1
1
/* eslint-disable @typescript-eslint/no-floating-promises */
2
- import { type EventStore } from '@event-driven-io/emmett' ;
2
+ import {
3
+ formatDateToUtcYYYYMMDD ,
4
+ type EventStore ,
5
+ } from '@event-driven-io/emmett' ;
3
6
import { getEventStoreDBEventStore } from '@event-driven-io/emmett-esdb' ;
4
7
import {
5
8
ApiE2ESpecification ,
9
+ expectError ,
6
10
expectResponse ,
7
11
getApplication ,
8
12
type TestRequest ,
@@ -19,14 +23,12 @@ const doesGuestStayExist = (_guestId: string, _roomId: string, _day: Date) =>
19
23
Promise . resolve ( true ) ;
20
24
21
25
describe ( 'guestStayAccount E2E' , ( ) => {
22
- // const oldTime = new Date();
23
26
const now = new Date ( ) ;
24
- // const formattedNow = formatDateToUtcYYYYMMDD(now);
27
+ const formattedNow = formatDateToUtcYYYYMMDD ( now ) ;
25
28
26
29
let guestId : string ;
27
30
let roomId : string ;
28
- // let guestStayAccountId: string;
29
- // const amount = Math.random() * 100;
31
+ const amount = Math . random ( ) * 100 ;
30
32
const transactionId = randomUUID ( ) ;
31
33
32
34
let esdbContainer : StartedEventStoreDBContainer ;
@@ -51,51 +53,173 @@ describe('guestStayAccount E2E', () => {
51
53
) ;
52
54
} ) ;
53
55
56
+ after ( ( ) => {
57
+ return esdbContainer . stop ( ) ;
58
+ } ) ;
59
+
54
60
beforeEach ( ( ) => {
55
61
guestId = randomUUID ( ) ;
56
62
roomId = randomUUID ( ) ;
57
- // guestStayAccountId = toGuestStayAccountId(guestId, roomId, now);
58
63
} ) ;
59
64
60
- after ( ( ) => {
61
- return esdbContainer . stop ( ) ;
62
- } ) ;
65
+ const checkIn : TestRequest = ( request ) =>
66
+ request . post ( `/guests/${ guestId } /stays/${ roomId } ` ) ;
67
+
68
+ const recordCharge : TestRequest = ( request ) =>
69
+ request
70
+ . post (
71
+ `/guests/${ guestId } /stays/${ roomId } /periods/${ formattedNow } /charges` ,
72
+ )
73
+ . send ( { amount } ) ;
63
74
64
- describe ( 'When empty' , ( ) => {
75
+ const recordPayment : TestRequest = ( request ) =>
76
+ request
77
+ . post (
78
+ `/guests/${ guestId } /stays/${ roomId } /periods/${ formattedNow } /payments` ,
79
+ )
80
+ . send ( { amount } ) ;
81
+
82
+ const checkOut : TestRequest = ( request ) =>
83
+ request . delete (
84
+ `/guests/${ guestId } /stays/${ roomId } /periods/${ formattedNow } ` ,
85
+ ) ;
86
+
87
+ void describe ( 'When not existing' , ( ) => {
65
88
const notExistingAccount : TestRequest [ ] = [ ] ;
66
89
67
- it ( 'should add product item' , ( ) => {
68
- return given ( ...notExistingAccount )
69
- . when ( ( request ) => request . post ( `/guests/${ guestId } /stays/${ roomId } ` ) )
70
- . then ( [ expectResponse ( 201 ) ] ) ;
90
+ void it ( 'checks in' , ( ) =>
91
+ given ( ...notExistingAccount )
92
+ . when ( checkIn )
93
+ . then ( [ expectResponse ( 201 ) ] ) ) ;
94
+
95
+ void it ( `doesn't record charge` , ( ) =>
96
+ given ( ...notExistingAccount )
97
+ . when ( recordCharge )
98
+ . then ( [
99
+ expectError ( 403 , {
100
+ detail : `Guest account doesn't exist!` ,
101
+ } ) ,
102
+ ] ) ) ;
103
+
104
+ void it ( `doesn't record payment` , ( ) =>
105
+ given ( ...notExistingAccount )
106
+ . when ( recordPayment )
107
+ . then ( [
108
+ expectError ( 403 , {
109
+ detail : `Guest account doesn't exist!` ,
110
+ } ) ,
111
+ ] ) ) ;
112
+
113
+ void it ( `doesn't checkout` , ( ) =>
114
+ given ( ...notExistingAccount )
115
+ . when ( checkOut )
116
+ . then ( [ expectError ( 403 ) ] ) ) ;
117
+ } ) ;
118
+
119
+ void describe ( 'When checked in' , ( ) => {
120
+ const checkedInAccount : TestRequest = checkIn ;
121
+
122
+ void it ( `doesn't check in` , ( ) =>
123
+ given ( checkedInAccount )
124
+ . when ( checkIn )
125
+ . then ( [ expectError ( 403 , { detail : `Guest is already checked-in!` } ) ] ) ) ;
126
+
127
+ void it ( 'records charge' , ( ) =>
128
+ given ( checkedInAccount )
129
+ . when ( recordCharge )
130
+ . then ( [ expectResponse ( 204 ) ] ) ) ;
131
+
132
+ void it ( 'records payment' , ( ) =>
133
+ given ( checkedInAccount )
134
+ . when ( recordPayment )
135
+ . then ( [ expectResponse ( 204 ) ] ) ) ;
136
+
137
+ void it ( 'checks out' , ( ) =>
138
+ given ( checkedInAccount )
139
+ . when ( checkOut )
140
+ . then ( [ expectResponse ( 204 ) ] ) ) ;
141
+
142
+ void describe ( 'with unsettled balance' , ( ) => {
143
+ const unsettledAccount : TestRequest [ ] = [ checkIn , recordCharge ] ;
144
+
145
+ void it ( 'records charge' , ( ) =>
146
+ given ( ...unsettledAccount )
147
+ . when ( ( request ) =>
148
+ request
149
+ . post (
150
+ `/guests/${ guestId } /stays/${ roomId } /periods/${ formattedNow } /charges` ,
151
+ )
152
+ . send ( { amount } ) ,
153
+ )
154
+ . then ( [ expectResponse ( 204 ) ] ) ) ;
155
+
156
+ void it ( 'records payment' , ( ) =>
157
+ given ( ...unsettledAccount )
158
+ . when ( recordPayment )
159
+ . then ( [ expectResponse ( 204 ) ] ) ) ;
160
+
161
+ void it ( `doesn't check out` , ( ) =>
162
+ given ( ...unsettledAccount )
163
+ . when ( checkOut )
164
+ . then ( [ expectError ( 403 ) ] ) ) ;
165
+ } ) ;
166
+
167
+ void describe ( 'with settled balance' , ( ) => {
168
+ const settledAccount : TestRequest [ ] = [
169
+ checkIn ,
170
+ recordCharge ,
171
+ recordPayment ,
172
+ ] ;
173
+
174
+ void it ( 'records charge' , ( ) =>
175
+ given ( ...settledAccount )
176
+ . when ( recordCharge )
177
+ . then ( [ expectResponse ( 204 ) ] ) ) ;
178
+
179
+ void it ( 'records payment' , ( ) =>
180
+ given ( ...settledAccount )
181
+ . when ( recordPayment )
182
+ . then ( [ expectResponse ( 204 ) ] ) ) ;
183
+
184
+ void it ( `checks out` , ( ) =>
185
+ given ( ...settledAccount )
186
+ . when ( checkOut )
187
+ . then ( [ expectResponse ( 204 ) ] ) ) ;
71
188
} ) ;
72
189
} ) ;
73
190
74
- // describe('When empty', () => {
75
- // it('should add product item', () => {
76
- // return given((request) =>
77
- // request
78
- // .post(`/clients/${clientId}/shopping-carts/current/product-items`)
79
- // .send(productItem),
80
- // )
81
- // .when((request) =>
82
- // request.get(`/clients/${clientId}/shopping-carts/current`).send(),
83
- // )
84
- // .then([
85
- // expectResponse(200, {
86
- // body: {
87
- // clientId,
88
- // id: shoppingCartId,
89
- // productItems: [
90
- // {
91
- // quantity: productItem.quantity,
92
- // productId: productItem.productId,
93
- // },
94
- // ],
95
- // status: 'Opened',
96
- // },
97
- // }),
98
- // ]);
99
- // });
100
- // });
191
+ void describe ( 'When checked out' , ( ) => {
192
+ const checkedOutAccount : TestRequest [ ] = [
193
+ checkIn ,
194
+ recordCharge ,
195
+ recordPayment ,
196
+ checkOut ,
197
+ ] ;
198
+
199
+ void it ( `doesn't check in` , ( ) =>
200
+ given ( ...checkedOutAccount )
201
+ . when ( checkIn )
202
+ . then ( [
203
+ expectError ( 403 , { detail : `Guest account is already checked out` } ) ,
204
+ ] ) ) ;
205
+
206
+ void it ( `doesn't record charge` , ( ) =>
207
+ given ( ...checkedOutAccount )
208
+ . when ( recordCharge )
209
+ . then ( [
210
+ expectError ( 403 , { detail : `Guest account is already checked out` } ) ,
211
+ ] ) ) ;
212
+
213
+ void it ( `doesn't record payment` , ( ) =>
214
+ given ( ...checkedOutAccount )
215
+ . when ( recordPayment )
216
+ . then ( [
217
+ expectError ( 403 , { detail : `Guest account is already checked out` } ) ,
218
+ ] ) ) ;
219
+
220
+ void it ( `doesn't checkout` , ( ) =>
221
+ given ( ...checkedOutAccount )
222
+ . when ( checkOut )
223
+ . then ( [ expectError ( 403 , { detail : `NotOpened` } ) ] ) ) ;
224
+ } ) ;
101
225
} ) ;
0 commit comments