Skip to content

Commit 4a49d05

Browse files
committed
--update: search rooms for booking
1 parent d6cb0ba commit 4a49d05

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

apps/room/src/room.service.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

libs/shared/src/schemas/room.schema.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ export class Room extends BaseEntity {
3131
booking: boolean;
3232
@Prop({ type: String, required: true })
3333
price: string;
34-
@Prop({ type: String, required: true })
35-
max_adults: string;
36-
@Prop({ type: String, required: true })
37-
max_children: string;
34+
@Prop({ type: Number, required: true })
35+
max_adults: number;
36+
@Prop({ type: Number, required: true })
37+
max_children: number;
3838
}
3939
export type RoomDocument = HydratedDocument<Room>;
4040
export const RoomSchema = SchemaFactory.createForClass(Room);

0 commit comments

Comments
 (0)