1
1
import {
2
2
formatDateToUtcYYYYMMDD ,
3
+ projections ,
3
4
type EventStore ,
4
5
} from '@event-driven-io/emmett' ;
5
6
import {
@@ -13,12 +14,18 @@ import {
13
14
getPostgreSQLEventStore ,
14
15
type PostgresEventStore ,
15
16
} from '@event-driven-io/emmett-postgresql' ;
17
+ import { pongoClient , type PongoClient } from '@event-driven-io/pongo' ;
16
18
import {
17
19
PostgreSqlContainer ,
18
20
type StartedPostgreSqlContainer ,
19
21
} from '@testcontainers/postgresql' ;
20
22
import { randomUUID } from 'node:crypto' ;
21
23
import { after , before , beforeEach , describe , it } from 'node:test' ;
24
+ import { toGuestStayAccountId } from '../guestStayAccount' ;
25
+ import {
26
+ guestStayDetailsProjection ,
27
+ type GuestStayDetails ,
28
+ } from '../guestStayDetails' ;
22
29
import { guestStayAccountsApi } from './api' ;
23
30
24
31
const doesGuestStayExist = ( _guestId : string , _roomId : string , _day : Date ) =>
@@ -35,11 +42,18 @@ void describe('guestStayAccount E2E', () => {
35
42
36
43
let postgres : StartedPostgreSqlContainer ;
37
44
let eventStore : PostgresEventStore ;
45
+ let readStore : PongoClient ;
38
46
let given : ApiE2ESpecification ;
39
47
40
48
before ( async ( ) => {
41
49
postgres = await new PostgreSqlContainer ( ) . start ( ) ;
42
- eventStore = getPostgreSQLEventStore ( postgres . getConnectionUri ( ) ) ;
50
+
51
+ const connectionString = postgres . getConnectionUri ( ) ;
52
+
53
+ eventStore = getPostgreSQLEventStore ( connectionString , {
54
+ projections : projections . inline ( [ guestStayDetailsProjection ] ) ,
55
+ } ) ;
56
+ readStore = pongoClient ( connectionString ) ;
43
57
44
58
given = ApiE2ESpecification . for (
45
59
( ) : EventStore => eventStore ,
@@ -48,6 +62,7 @@ void describe('guestStayAccount E2E', () => {
48
62
apis : [
49
63
guestStayAccountsApi (
50
64
eventStore ,
65
+ readStore . db ( ) ,
51
66
doesGuestStayExist ,
52
67
( prefix ) => `${ prefix } -${ transactionId } ` ,
53
68
( ) => now ,
@@ -59,6 +74,7 @@ void describe('guestStayAccount E2E', () => {
59
74
60
75
after ( async ( ) => {
61
76
await eventStore . close ( ) ;
77
+ await readStore . close ( ) ;
62
78
await postgres . stop ( ) ;
63
79
} ) ;
64
80
@@ -88,6 +104,8 @@ void describe('guestStayAccount E2E', () => {
88
104
request . delete (
89
105
`/guests/${ guestId } /stays/${ roomId } /periods/${ formattedNow } ` ,
90
106
) ;
107
+ const getDetails : TestRequest = ( request ) =>
108
+ request . get ( `/guests/${ guestId } /stays/${ roomId } /periods/${ formattedNow } ` ) ;
91
109
92
110
void describe ( 'When not existing' , ( ) => {
93
111
const notExistingAccount : TestRequest [ ] = [ ] ;
@@ -119,6 +137,11 @@ void describe('guestStayAccount E2E', () => {
119
137
given ( ...notExistingAccount )
120
138
. when ( checkOut )
121
139
. then ( [ expectError ( 403 ) ] ) ) ;
140
+
141
+ void it ( `details return 404` , ( ) =>
142
+ given ( ...notExistingAccount )
143
+ . when ( getDetails )
144
+ . then ( [ expectError ( 404 ) ] ) ) ;
122
145
} ) ;
123
146
124
147
void describe ( 'When checked in' , ( ) => {
@@ -144,6 +167,24 @@ void describe('guestStayAccount E2E', () => {
144
167
. when ( checkOut )
145
168
. then ( [ expectResponse ( 204 ) ] ) ) ;
146
169
170
+ void it ( `details return checked in stay` , ( ) =>
171
+ given ( checkedInAccount )
172
+ . when ( getDetails )
173
+ . then ( [
174
+ expectResponse < GuestStayDetails > ( 200 , {
175
+ body : {
176
+ _id : toGuestStayAccountId ( guestId , roomId , now ) ,
177
+ status : 'CheckedIn' ,
178
+ balance : 0 ,
179
+ roomId,
180
+ guestId,
181
+ transactions : [ ] ,
182
+ transactionsCount : 0 ,
183
+ checkedInAt : now ,
184
+ } ,
185
+ } ) ,
186
+ ] ) ) ;
187
+
147
188
void describe ( 'with unsettled balance' , ( ) => {
148
189
const unsettledAccount : TestRequest [ ] = [ checkIn , recordCharge ] ;
149
190
@@ -167,6 +208,24 @@ void describe('guestStayAccount E2E', () => {
167
208
given ( ...unsettledAccount )
168
209
. when ( checkOut )
169
210
. then ( [ expectError ( 403 ) ] ) ) ;
211
+
212
+ void it ( `details return checked in stay with charge` , ( ) =>
213
+ given ( ...unsettledAccount )
214
+ . when ( getDetails )
215
+ . then ( [
216
+ expectResponse ( 200 , {
217
+ body : {
218
+ _id : toGuestStayAccountId ( guestId , roomId , now ) ,
219
+ status : 'CheckedIn' ,
220
+ balance : - amount ,
221
+ roomId,
222
+ guestId,
223
+ transactions : [ { amount } ] ,
224
+ transactionsCount : 1 ,
225
+ checkedInAt : now ,
226
+ } ,
227
+ } ) ,
228
+ ] ) ) ;
170
229
} ) ;
171
230
172
231
void describe ( 'with settled balance' , ( ) => {
@@ -190,6 +249,24 @@ void describe('guestStayAccount E2E', () => {
190
249
given ( ...settledAccount )
191
250
. when ( checkOut )
192
251
. then ( [ expectResponse ( 204 ) ] ) ) ;
252
+
253
+ void it ( `details return checked in stay with charge` , ( ) =>
254
+ given ( ...settledAccount )
255
+ . when ( getDetails )
256
+ . then ( [
257
+ expectResponse ( 200 , {
258
+ body : {
259
+ _id : toGuestStayAccountId ( guestId , roomId , now ) ,
260
+ status : 'CheckedIn' ,
261
+ balance : 0 ,
262
+ roomId,
263
+ guestId,
264
+ transactions : [ { amount } , { amount } ] ,
265
+ transactionsCount : 2 ,
266
+ checkedInAt : now ,
267
+ } ,
268
+ } ) ,
269
+ ] ) ) ;
193
270
} ) ;
194
271
} ) ;
195
272
@@ -226,5 +303,10 @@ void describe('guestStayAccount E2E', () => {
226
303
given ( ...checkedOutAccount )
227
304
. when ( checkOut )
228
305
. then ( [ expectError ( 403 , { detail : `NotCheckedIn` } ) ] ) ) ;
306
+
307
+ void it ( `details return 404` , ( ) =>
308
+ given ( ...checkedOutAccount )
309
+ . when ( getDetails )
310
+ . then ( [ expectResponse ( 404 ) ] ) ) ;
229
311
} ) ;
230
312
} ) ;
0 commit comments