Back: add episode service for calendar

This commit is contained in:
X4Lo committed 2024-09-10 19:07:12 +01:00
1 parent 9b8ff730c3
commit 5008940c59
1 file changed
+51
@@ -0,0 +1,51 @@
const { Op } = require("sequelize")
const Episode = require("../../models/Episode");
const moment = require("../../utils/moment");
const Season = require("../../models/Season");
const Entry = require("../../models/Entry");
const EntryId = require("../../models/EntryId");
const Media = require("../../models/Media");
async function getEpisodesBetweenDates(startDate, endDate) {
// Get the first and last day of the month based on the provided date
const formatedStartDate = moment(startDate).format('YYYY-MM-DD');
const formatedEndDate = moment(endDate).format('YYYY-MM-DD');
// Fetch episodes that aired within the current month
const episodes = await Episode.findAll({
where: {
air_date: {
[Op.between]: [formatedStartDate, formatedEndDate]
}
},
include: [
{
model: Season,
as: 'season',
include: {
model: Entry, as: 'entry', include: [
{ model: EntryId, as: 'entryId' },
{ model: Media, as: 'medias' },
]
}
}
]
});
const modifiedEpisodes = episodes.map(episode => {
const episodeJSON = episode.toJSON();
if (episodeJSON.season && episodeJSON.season.entry) {
episodeJSON.entry = episodeJSON.season.entry;
episodeJSON.season.entry = null;
}
return episodeJSON;
});
return modifiedEpisodes;
}
module.exports = {
getEpisodesBetweenDates,
}