@@ -221,25 +221,46 @@ export class RoomService {
221221 }
222222 }
223223 async SearchRoomForBooking ( payload ) {
224- const { start, end, adults, children } = payload ;
224+ const { start, end, adults = 0 , children = 0 } = payload ;
225+ if ( ! payload ) {
226+ throw new Error ( 'Payload is required' ) ;
227+ }
228+
229+ if ( ! start || ! end ) {
230+ throw new Error ( 'Start and end date are required' ) ;
231+ }
232+ if ( adults < 0 ) {
233+ throw new Error ( 'Adults must be a positive number' ) ;
234+ }
235+
236+ if ( children < 0 ) {
237+ throw new Error ( 'Children must be a positive number' ) ;
238+ }
239+
225240 const formatStartToDate = moment ( start , 'DD-MM-YYYY' ) . toDate ( ) ;
226241 const formatEndToDate = moment ( end , 'DD-MM-YYYY' ) . toDate ( ) ;
242+ if ( formatStartToDate >= formatEndToDate ) {
243+ throw new Error ( 'Start date must be before end date' ) ;
244+ }
227245
228- try {
229- const unavailableRooms = await this . BookingModel . find ( {
230- $or : [
231- {
232- $and : [
233- { start : { $lt : formatStartToDate } } ,
234- { end : { $gte : formatEndToDate } } ,
235- ] ,
236- isValidDateRange : true ,
237- } ,
238- ] ,
239- } ) . populate ( 'rooms' ) ;
240- return unavailableRooms ;
241- } catch ( error ) {
242- throw new Error ( 'Error finding unavailable rooms' ) ;
246+ const unavailableRooms : any = await this . BookingModel . find ( {
247+ start : { $gte : formatStartToDate , $lte : formatEndToDate } ,
248+ } ) . lean ( ) ;
249+ const listRoomReject = unavailableRooms [ 0 ] . rooms ;
250+ const roomForBooking = await this . RoomRepository . findAllWithPopulate ( {
251+ _id : { $nin : listRoomReject } ,
252+ max_adults : { $gte : parseInt ( adults ) } ,
253+ max_children : { $gte : parseInt ( children ) } ,
254+ } ) ;
255+ if ( ! roomForBooking ) {
256+ return {
257+ status : HttpStatus . OK ,
258+ data : [ ] ,
259+ } ;
243260 }
261+ return {
262+ code : HttpStatus . OK ,
263+ data : roomForBooking ,
264+ } ;
244265 }
245266}
0 commit comments