Files
Showy/backend/services/models-services/episodeService.js
T

51 lines
1.4 KiB
JavaScript

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,
}