11import {
22 formatDateToUtcYYYYMMDD ,
3+ projections ,
34 type EventStore ,
45} from '@event-driven-io/emmett' ;
56import {
@@ -13,12 +14,18 @@ import {
1314 getPostgreSQLEventStore ,
1415 type PostgresEventStore ,
1516} from '@event-driven-io/emmett-postgresql' ;
17+ import { pongoClient , type PongoClient } from '@event-driven-io/pongo' ;
1618import {
1719 PostgreSqlContainer ,
1820 type StartedPostgreSqlContainer ,
1921} from '@testcontainers/postgresql' ;
2022import { randomUUID } from 'node:crypto' ;
2123import { after , before , beforeEach , describe , it } from 'node:test' ;
24+ import { toGuestStayAccountId } from '../guestStayAccount' ;
25+ import {
26+ guestStayDetailsProjection ,
27+ type GuestStayDetails ,
28+ } from '../guestStayDetails' ;
2229import { guestStayAccountsApi } from './api' ;
2330
2431const doesGuestStayExist = ( _guestId : string , _roomId : string , _day : Date ) =>
@@ -35,11 +42,18 @@ void describe('guestStayAccount E2E', () => {
3542
3643 let postgres : StartedPostgreSqlContainer ;
3744 let eventStore : PostgresEventStore ;
45+ let readStore : PongoClient ;
3846 let given : ApiE2ESpecification ;
3947
4048 before ( async ( ) => {
4149 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 ) ;
4357
4458 given = ApiE2ESpecification . for (
4559 ( ) : EventStore => eventStore ,
@@ -48,6 +62,7 @@ void describe('guestStayAccount E2E', () => {
4862 apis : [
4963 guestStayAccountsApi (
5064 eventStore ,
65+ readStore . db ( ) ,
5166 doesGuestStayExist ,
5267 ( prefix ) => `${ prefix } -${ transactionId } ` ,
5368 ( ) => now ,
@@ -59,6 +74,7 @@ void describe('guestStayAccount E2E', () => {
5974
6075 after ( async ( ) => {
6176 await eventStore . close ( ) ;
77+ await readStore . close ( ) ;
6278 await postgres . stop ( ) ;
6379 } ) ;
6480
@@ -88,6 +104,8 @@ void describe('guestStayAccount E2E', () => {
88104 request . delete (
89105 `/guests/${ guestId } /stays/${ roomId } /periods/${ formattedNow } ` ,
90106 ) ;
107+ const getDetails : TestRequest = ( request ) =>
108+ request . get ( `/guests/${ guestId } /stays/${ roomId } /periods/${ formattedNow } ` ) ;
91109
92110 void describe ( 'When not existing' , ( ) => {
93111 const notExistingAccount : TestRequest [ ] = [ ] ;
@@ -119,6 +137,11 @@ void describe('guestStayAccount E2E', () => {
119137 given ( ...notExistingAccount )
120138 . when ( checkOut )
121139 . then ( [ expectError ( 403 ) ] ) ) ;
140+
141+ void it ( `details return 404` , ( ) =>
142+ given ( ...notExistingAccount )
143+ . when ( getDetails )
144+ . then ( [ expectError ( 404 ) ] ) ) ;
122145 } ) ;
123146
124147 void describe ( 'When checked in' , ( ) => {
@@ -144,6 +167,24 @@ void describe('guestStayAccount E2E', () => {
144167 . when ( checkOut )
145168 . then ( [ expectResponse ( 204 ) ] ) ) ;
146169
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+
147188 void describe ( 'with unsettled balance' , ( ) => {
148189 const unsettledAccount : TestRequest [ ] = [ checkIn , recordCharge ] ;
149190
@@ -167,6 +208,24 @@ void describe('guestStayAccount E2E', () => {
167208 given ( ...unsettledAccount )
168209 . when ( checkOut )
169210 . 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+ ] ) ) ;
170229 } ) ;
171230
172231 void describe ( 'with settled balance' , ( ) => {
@@ -190,6 +249,24 @@ void describe('guestStayAccount E2E', () => {
190249 given ( ...settledAccount )
191250 . when ( checkOut )
192251 . 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+ ] ) ) ;
193270 } ) ;
194271 } ) ;
195272
@@ -226,5 +303,10 @@ void describe('guestStayAccount E2E', () => {
226303 given ( ...checkedOutAccount )
227304 . when ( checkOut )
228305 . then ( [ expectError ( 403 , { detail : `NotCheckedIn` } ) ] ) ) ;
306+
307+ void it ( `details return 404` , ( ) =>
308+ given ( ...checkedOutAccount )
309+ . when ( getDetails )
310+ . then ( [ expectResponse ( 404 ) ] ) ) ;
229311 } ) ;
230312} ) ;
0 commit comments