diff --git a/backend/services/models-services/episodeService.js b/backend/services/models-services/episodeService.js new file mode 100644 index 0000000..b042f51 --- /dev/null +++ b/backend/services/models-services/episodeService.js @@ -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, +} \ No newline at end of file