Task: "Create Handle Recurring Bookings Api" Description: "Generate a Node.js…
- User
User: Task: "Create Handle Recurring Bookings Api" Description: "Generate a Node.js Handle Recurring Bookings Api with given dependenceis" Requirements: - Database: mongoose - Authentication: None specified - Output needed: Return only the function code as specified in dependencies - Dependencies: Create a Api to manage recurring bookings. Export: exports.createRecurringBooking = async () => {...}, Accept parameters: startDate, endDate, frequency (e.g., weekly, monthly), roomId., Validate required fields: Ensure startDate, endDate, frequency, and roomId are provided., Use a scheduling library (e.g., node-cron) to generate recurring booking dates based on the frequency., Check availability for each recurring date using the checkAvailability function., Create bookings for each recurring date and link them to the same user or service., Respond to user with a success message containing the booking details for each occurrence., Handle errors like conflicting bookings, invalid parameters, or database issues. - Previous Code const mongoose = require('mongoose'); // Importing the Booking model (assuming it has been defined elsewhere in your code) const Booking = require('./models/Booking');
// Function to check availability of a room or service exports.checkAvailability = async (checkInDate, checkOutDate, roomId) => { // Validate required fields if (!checkInDate || !checkOutDate || !roomId) { throw new Error('Missing required parameters: checkInDate, checkOutDate, and roomId are required.'); }
try { // Convert dates to appropriate format if needed (for example, using new Date()) const checkIn = new Date(checkInDate); const checkOut = new Date(checkOutDate); // Query the Booking model to check for existing bookings during the specified date range const existingBooking = await Booking.findOne({ roomId: roomId, $or: [ { checkInDate: { $lt: checkOut }, checkOutDate: { $gt: checkIn } }, { checkInDate: { $eq: checkIn }, checkOutDate: { $eq: checkOut } } ] }); // If a booking already exists in the date range, indicate double booking if (existingBooking) { return { success: false, message: 'The room or service is unavailable for the selected date range due to a double booking.' }; } // If no booking exists, indicate that the room or service is available return { success: true, message: 'The room or service is available for booking.' }; } catch (error) { // Handle errors such as database connection errors console.error('Error checking availability:', error); throw new Error(`Error checking availability: ${error.message}`); }};
// Function to manage time slots exports.manageTimeSlots = async (date, serviceId, availableSlots) => { // Validate required fields if (!date || !serviceId || !Array.isArray(availableSlots) || availableSlots.length === 0) { throw new Error('Missing required parameters: date, serviceId, and availableSlots are required.'); }
- Assistant