|
const { runSelectQuery } = require('../utils/queries'); |
|
|
|
const generalSlots = [ |
|
`10:00 - 10:30`, `10:45 - 11:15`, `11:30 - 12:00`, `12:15 - 12:45`, `13:00 - 13:30`, |
|
`13:45 - 14:15`, `14:30 - 15:00`, `15:15 - 15:45`, `16:00 - 16:30`, `16:45 - 17:15`, `17:30 - 18:00` |
|
]; |
|
|
|
|
|
async function getBookedSlots(date) { |
|
if (!date) { |
|
throw new Error('Date is required'); |
|
} |
|
|
|
|
|
const isValidDate = /^\d{4}-\d{2}-\d{2}$/.test(date); |
|
if (!isValidDate) { |
|
throw new Error('Invalid date format. Expected YYYY-MM-DD'); |
|
} |
|
|
|
try { |
|
const query = ` |
|
SELECT slot FROM demo_requests WHERE demo_date = ?; |
|
`; |
|
const results = await runSelectQuery(query, date); |
|
|
|
console.log(`Fetched ${results.length} slots for date: ${date}`); |
|
|
|
|
|
if (!results || results.length === 0) { |
|
console.log(`No slots found for date: ${date}`); |
|
return []; |
|
} |
|
|
|
|
|
const slots = results.map(result => { |
|
if (!result.slot) { |
|
console.warn(`No slot found for record: ${JSON.stringify(result)}`); |
|
return null; |
|
} |
|
return result.slot; |
|
}).filter(slot => slot !== null); |
|
|
|
return slots; |
|
} catch (error) { |
|
console.error('Error fetching booked slots:', error.message); |
|
throw error; |
|
} |
|
} |
|
|
|
|
|
async function getAvailableSlots(date) { |
|
if (!date) { |
|
throw new Error('Date is required'); |
|
} |
|
|
|
|
|
const isValidDate = /^\d{4}-\d{2}-\d{2}$/.test(date); |
|
if (!isValidDate) { |
|
throw new Error('Invalid date format. Expected YYYY-MM-DD'); |
|
} |
|
|
|
try { |
|
|
|
const bookedSlots = await getBookedSlots(date); |
|
console.log('Booked slots:', bookedSlots); |
|
|
|
|
|
if (bookedSlots.length === 0) { |
|
return generalSlots; |
|
} |
|
|
|
|
|
if (!Array.isArray(generalSlots) || generalSlots.length === 0) { |
|
throw new Error('General slots are not properly defined'); |
|
} |
|
|
|
|
|
const availableSlots = generalSlots.filter(slot => !bookedSlots.includes(slot)); |
|
|
|
|
|
return availableSlots; |
|
} catch (error) { |
|
console.error('Error fetching available slots:', error.message); |
|
throw error; |
|
} |
|
} |
|
|
|
module.exports = { getAvailableSlots, getBookedSlots}; |
|
|