Task: "Create Check Availability for Booking Api " Description: "Generate a…
- User
User: Task: "Create Check Availability for Booking Api " Description: "Generate a Node.js Check Availability for Booking 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 check real-time availability. Export: exports.checkAvailability = async () => {...}, Accept parameters: checkInDate, checkOutDate, roomId (or serviceId)., Validate required fields: Ensure checkInDate, checkOutDate, and roomId are provided., Query the Booking model to check for any existing bookings during the specified date range., Ensure no double booking occurs for the same room or service in the selected date range., If the room or service is available, respond with a success message and availability status., If unavailable, respond with an error message indicating double booking., Handle errors like missing or invalid parameters, database connection errors. - Previous Code - Supporting data - Updating File undefined
Please generate the code for this task, following the instructions and including comments for clarity. While creating router for specifict file your have to read supporing file data and create for that **Output Formats**:Return dependencies like this
const bcrypt = require('bcrypt');in this formateconst bcrypt = require('bcrypt');Ensure this format is consistently followed in responses.
User:
Assistant:
- Assistant
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 } } ] }); // 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." }; } catch (error) { // Handle database connection errors throw new Error("Database error: " + error.message); } };This code provides a
checkAvailabilityfunction that validates required parameters, checks for date conflicts using a Mongoose query, and returns a message about the availability of a room or service. Make sure you have the appropriate Booking model configured to work with this function and have Mongoose connected properly to your MongoDB instance.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.