Files
Showy/backend/services/episodeService.js
T
Mohammed Abad 11eab03987 Backend:
- Enhance PeopleController with person credits and TMDB filmography endpoints;
- update episodeService to filter entries by showInCalendar;
- add fetchPersonCombinedCredits function in tmdbService
2026-04-01 00:07:41 +01:00

75 lines
1.9 KiB
JavaScript

const moment = require("../utils/moment");
const { Op } = require("sequelize")
const Episode = require("../models/Episode");
const Season = require("../models/Season");
const Entry = require("../models/Entry");
const EntryId = require("../models/EntryId");
const Media = require("../models/Media");
const BadRequestError = require("../errors/BadRequestError");
class EpisodeService {
constructor() {
if (!EpisodeService.instance) {
EpisodeService.instance = this;
}
return EpisodeService.instance;
}
async getEpisodesBetweenDates(startDate, endDate) {
if (!startDate) {
throw new BadRequestError("Start Date not provided");
}
if (!endDate) {
throw new BadRequestError("end Date not provided");
}
const formatedStartDate = moment(startDate).format('YYYY-MM-DD');
const formatedEndDate = moment(endDate).format('YYYY-MM-DD');
// fetch episodes that aired within the current month, only from entries with showInCalendar enabled
const episodes = await Episode.findAll({
where: {
air_date: {
[Op.between]: [formatedStartDate, formatedEndDate]
},
},
include: [
{
model: Season,
as: 'season',
include: {
model: Entry,
as: 'entry',
where: { showInCalendar: true },
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 = new EpisodeService();