Task: "Create Implement Waitlist Feature Api" Description: "Generate a Node.js…
- User
User: Task: "Create Implement Waitlist Feature Api" Description: "Generate a Node.js Waitlist Feature Api with given dependenceis" Requirements: - Database: mongoose - Authentication: None specified - Output needed: Return only the function code as specified in dependencies - Dependencies: Create a function to add a user to the waitlist for a fully booked date. Export: exports.addToWaitlist = async () => {...}, Accept parameters: userId, checkInDate, checkOutDate, roomId., Validate required fields: Ensure userId, checkInDate, checkOutDate, and roomId are provided., If the requested date is fully booked, add the user to the waitlist for that date., Store waitlist data with date, roomId, userId, and priority level., When a booking is canceled, notify users on the waitlist and offer the available slot., Respond with a success message confirming the user has been added to the waitlist., Handle errors like invalid dates, conflicting bookings, or database issues. - Previous Code const mongoose = require('mongoose'); const Booking = require('./models/Booking'); // Assuming Booking model is in models/Booking.js
exports.checkAvailability = async (checkInDate, checkOutDate, roomId) => { // Validate required fields if (!checkInDate || !checkOutDate || !roomId) { throw new Error("Missing required parameters: checkInDate, checkOutDate, roomId are required."); }
// Convert dates to proper Date objects const checkIn = new Date(checkInDate); const checkOut = new Date(checkOutDate);
// Validate date formats if (isNaN(checkIn.getTime()) || isNaN(checkOut.getTime())) { throw new Error("Invalid date formats provided."); }
try { // Query the Booking model to find existing bookings within date range const existingBookings = await Booking.find({ roomId: roomId, or: [ { checkInDate: { lt: checkOut, gte: checkIn } }, { checkOutDate: { gt: checkIn, lte: checkOut } }, { checkInDate: { lte: checkIn }, checkOutDate: { $gte: checkOut } } ] });
code// Ensure no double booking if (existingBookings.length > 0) { return { available: false, message: "Room/service is already booked for these dates." }; } // If no conflicting bookings found return { available: true, message: "Room/service is available for booking." }; - Assistant