Я пытаюсь создать приложение календаря в стеке MERN со следующей схемой событий: -
Модель - Событие. js
calendar: {
type: Schema.Types.ObjectId,
ref: 'calendars'
},
name: {
type: String,
required: true,
},
description: {
type: String,
},
startTime: {
type: Date,
required:true,
},
endTime: {
type: Date,
required:true
},
dates: [{
type: Date
}]
Как Я создаю массив дат?
var dates=[]
var eventDate = new Date(newEvent.startTime);
var dateAfter1Year = new Date(newEvent.startTime);
dateAfter1Year.setFullYear(dateAfter1Year.getFullYear()+1);
while(eventDate<dateAfter1Year){
dates.push(eventDate.toJSON());
eventDate.setDate(eventDate.getDate()+recurringIn)
}
Как мне проверить, существует ли уже событие в любой из этих дней в базе данных в заданное время дня ??? Если бы массив дат не существовал, я мог бы очень легко реализовать это следующим образом: -
findTimeOverlap = async function (newEvent) {
// Takes care if the new Event lies exactly or in between any old event in the same calendar
const timeOverlap = await Event.findOne({
calendar:newEvent.calendar,
startTime: {$gte :newEvent.startTime},
endTime: {$lte: newEvent.endTime}
})
if(timeOverlap) return timeOverlap;
// Takes care if any old event starts before the new event but ends in-between the new event
const timeOverlap2 = await Event.findOne({
calendar:newEvent.calendar,
startTime: {$lte: newEvent.startTime},
endTime: {$lte: newEvent.endTime},
endTime: {$gt: newEvent.startTime}
})
if(timeOverlap2) return timeOverlap2;
// Takes care if any old event starts in-between the new event and ends after it
const timeOverlap3 = await Event.findOne({
calendar:newEvent.calendar,
startTime: {$gte: newEvent.startTime},
startTime: {$lt: newEvent.endTime},
endTime: {$gte: newEvent.endTime}
})
if(timeOverlap3) return timeOverlap3;
// Takes care if the new event lies in between any old event
const timeOverlap4 = await Event.findOne({
calendar:newEvent.calendar,
startTime: {$lt: newEvent.startTime},
endTime: {$gt: newEvent.endTime}
})
if(timeOverlap4) return timeOverlap4;
// If control reaches here, the slot is free
return null;
}