Major Backend SI Update part 2: added controllers
This commit is contained in:
1 parent
a51e207707
commit
dbb00fdb61
38 files changed
+2129
-1231
No files matched your search
@@ -18,8 +18,23 @@ class CollectionService {
|
||||
return CollectionService.instance;
|
||||
}
|
||||
|
||||
async getCollections() {
|
||||
const collections = await Collection.findAll({
|
||||
include: [
|
||||
{
|
||||
model: Entry, as: 'entries',
|
||||
include: { model: Media, as: 'medias' }
|
||||
},
|
||||
{ model: Media, as: 'medias' }
|
||||
],
|
||||
order: [['name', 'ASC']]
|
||||
});
|
||||
|
||||
return collections;
|
||||
}
|
||||
|
||||
async getCollectionById(id) {
|
||||
return await Collection.findByPk(id, {
|
||||
const collection = await Collection.findByPk(id, {
|
||||
include: [{
|
||||
model: Entry,
|
||||
as: 'entries',
|
||||
@@ -37,6 +52,8 @@ class CollectionService {
|
||||
}],
|
||||
order: [[Sequelize.literal("`entries->UserCollectionEntry`.`order`"), 'ASC']], // sort by 'order' in ascending order
|
||||
});
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
async createOrUpdateCollection(collectionData) {
|
||||
|
||||
@@ -20,11 +20,14 @@ const { logToFile } = require("./logService.js");
|
||||
const collectionManager = require('./collectionManager.js');
|
||||
const entryService = require("./entryService");
|
||||
const mediaManager = require("./mediaManager.js");
|
||||
const EntryNetwork = require("../models/EntryNetwork.js");
|
||||
const networkService = require("./networkService.js");
|
||||
const Network = require("../models/Network.js");
|
||||
|
||||
const _collectionManager = collectionManager;
|
||||
const _entryService = entryService;
|
||||
const _mediaManager = mediaManager;
|
||||
|
||||
const _networkService = networkService;
|
||||
class EntryManager {
|
||||
|
||||
constructor() {
|
||||
@@ -128,6 +131,25 @@ class EntryManager {
|
||||
})
|
||||
});
|
||||
|
||||
// * networks
|
||||
if (entryData.networks) {
|
||||
const oldNetworksIds = oldEntry.networks.map(network => network.id);
|
||||
for (const networkData of entryData.networks) {
|
||||
// If the entry already has this network, skip it
|
||||
if (oldNetworksIds.includes(networkData.id)) continue;
|
||||
|
||||
// Create or update the network
|
||||
const [oldNetwork, newNetwork] = await _networkService.createOrUpdateNetwork(networkData);
|
||||
let network = oldNetwork || newNetwork;
|
||||
if (!network) {
|
||||
throw new NotFoundError("Network not found");
|
||||
}
|
||||
|
||||
// Associate the new network with the entry
|
||||
await oldEntry.addNetwork(network);
|
||||
}
|
||||
}
|
||||
|
||||
// * For TV-Series (Seasons & Episodes)
|
||||
if (oldEntry.is_tvseries) {
|
||||
// ? Only refrech if the series's status is not 'Ended'
|
||||
|
||||
@@ -15,12 +15,15 @@ const NotFoundError = require("../errors/NotFoundError");
|
||||
const mediaOptimizerService = require("./mediaOptimizerService.js");
|
||||
const mediaManager = require("./mediaManager.js");
|
||||
const { fetchSeasonData } = require("./tmdbService.js");
|
||||
const Network = require("../models/Network.js");
|
||||
const networkService = require("./networkService.js");
|
||||
|
||||
const _mediaManager = mediaManager;
|
||||
const _mediaOptimizerService = mediaOptimizerService;
|
||||
const _networkService = networkService;
|
||||
|
||||
class EntryService {
|
||||
|
||||
|
||||
constructor() {
|
||||
if (!EntryService.instance) {
|
||||
EntryService.instance = this;
|
||||
@@ -35,6 +38,7 @@ class EntryService {
|
||||
{ model: EntryRating, as: 'entryRating' },
|
||||
{ model: Media, as: 'medias' },
|
||||
{ model: Season, as: 'seasons' },
|
||||
{ model: Network, as: 'networks' },
|
||||
{
|
||||
model: Genre,
|
||||
as: 'genres',
|
||||
@@ -67,6 +71,7 @@ class EntryService {
|
||||
{ model: EntrySeen, as: 'entrySeen' },
|
||||
{ model: Season, as: 'seasons' },
|
||||
{ model: Media, as: 'medias' },
|
||||
{ model: Network, as: 'networks' },
|
||||
{
|
||||
model: Collection, as: 'collections',
|
||||
include: [
|
||||
@@ -133,7 +138,7 @@ class EntryService {
|
||||
let savedEntry = await movie.save();
|
||||
|
||||
// ? ids
|
||||
const ids = await EntryId.create({
|
||||
await EntryId.create({
|
||||
entry_id: savedEntry.id,
|
||||
tmdb_id: data.id,
|
||||
imdb_id: data.imdb_id,
|
||||
@@ -147,8 +152,23 @@ class EntryService {
|
||||
})
|
||||
});
|
||||
|
||||
// ? networks
|
||||
if (data.networks) {
|
||||
for (const networkData of data.networks) {
|
||||
// Create or update the network
|
||||
const [oldNetwork, newNetwork] = await _networkService.createOrUpdateNetwork(networkData);
|
||||
let network = oldNetwork || newNetwork;
|
||||
if (!network) {
|
||||
throw new NotFoundError("Network not found");
|
||||
}
|
||||
|
||||
// Associate the new network with the entry
|
||||
await savedEntry.addNetwork(network);
|
||||
}
|
||||
}
|
||||
|
||||
// ? ratings
|
||||
const ratings = await EntryRating.create({
|
||||
await EntryRating.create({
|
||||
entry_id: savedEntry.id,
|
||||
user_rating: 0,
|
||||
tmdb_rating: data.vote_average,
|
||||
@@ -260,6 +280,59 @@ class EntryService {
|
||||
return updatedEntry;
|
||||
}
|
||||
|
||||
async updateEntryRatings(id, entryRatingsData) {
|
||||
if (!id) {
|
||||
throw new BadRequestError("Entry id not provided");
|
||||
}
|
||||
|
||||
let entry = await Entry.findByPk(id, {
|
||||
include: [
|
||||
{ model: EntryRating, as: 'entryRating' },
|
||||
]
|
||||
});
|
||||
|
||||
if (!entry) {
|
||||
throw new NotFoundError("Entry with the provided id was not found")
|
||||
}
|
||||
|
||||
if (entry.entryRating.id != entryRatingsData.id) {
|
||||
throw new BadRequestError("EntryRatings id does not match");
|
||||
}
|
||||
|
||||
entry.entryRating.user_rating = entryRatingsData.user_rating ?? entry.entryRating.user_rating;
|
||||
entry.entryRating.tmdb_rating = entryRatingsData.tmdb_rating ?? entry.entryRating.tmdb_rating;
|
||||
entry.entryRating.imdb_rating = entryRatingsData.imdb_rating ?? entry.entryRating.imdb_rating;
|
||||
entry.entryRating.rotten_tomatoes_rating = entryRatingsData.rotten_tomatoes_rating ?? entry.entryRating.rotten_tomatoes_rating;
|
||||
|
||||
return await entry.entryRating.save();
|
||||
}
|
||||
|
||||
async updateEntryIds(id, entryIdsData) {
|
||||
if (!id) {
|
||||
throw new BadRequestError("Entry id not provided");
|
||||
}
|
||||
|
||||
let entry = await Entry.findByPk(id, {
|
||||
include: [
|
||||
{ model: EntryId, as: 'entryId' },
|
||||
]
|
||||
});
|
||||
|
||||
if (!entry) {
|
||||
throw new NotFoundError("Entry with the provided id was not found")
|
||||
}
|
||||
|
||||
if (entry.entryRating.id != entryRatingsData.id) {
|
||||
throw new BadRequestError("EntryIds id does not match");
|
||||
}
|
||||
|
||||
entry.entryId.tmdb_id = entryIdsData.tmdb_id ?? entry.entryId.tmdb_id;
|
||||
entry.entryId.imdb_id = entryIdsData.imdb_id ?? entry.entryId.imdb_id;
|
||||
entry.entryId.tvdb_id = entryIdsData.tvdb_id ?? entry.entryId.tvdb_id;
|
||||
|
||||
await entry.entryId.save();
|
||||
}
|
||||
|
||||
async deleteEntryById(id) {
|
||||
if (!id) {
|
||||
throw new BadRequestError("Entry id not provided");
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
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
|
||||
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 = new EpisodeService();
|
||||
@@ -9,134 +9,149 @@ const configService = require('./configService');
|
||||
|
||||
const _configService = configService;
|
||||
|
||||
async function deleteMissingFilesEntries(dry = false) {
|
||||
const allMedia = await Media.findAll();
|
||||
class MaintenanceService {
|
||||
|
||||
// Getting the userDataPath to store the images
|
||||
const dataPath = await _configService.getMediaFolder();
|
||||
|
||||
// Handling different media types
|
||||
const mediaTypeFolder = {
|
||||
'entryposter': path.join(dataPath, 'images', 'entries', 'posters'),
|
||||
'entrylogo': path.join(dataPath, 'images', 'entries', 'logos'),
|
||||
'entrybackdrop': path.join(dataPath, 'images', 'entries', 'backdrops'),
|
||||
|
||||
'collectionposter': path.join(dataPath, 'images', 'collections', 'posters'),
|
||||
'collectionlogo': path.join(dataPath, 'images', 'collections', 'logos'),
|
||||
'collectionbackdrop': path.join(dataPath, 'images', 'collections', 'backdrops')
|
||||
};
|
||||
|
||||
let missingFiles = [];
|
||||
for (let i = 0; i < allMedia.length; i++) {
|
||||
const folderType = `${allMedia[i].mediaable}${allMedia[i].type.replace("_preview", "")}`;
|
||||
|
||||
const filePath = path.join(mediaTypeFolder[folderType], allMedia[i].path)
|
||||
if (!fs.existsSync(filePath)) {
|
||||
missingFiles.push(allMedia[i]);
|
||||
constructor() {
|
||||
if (!MaintenanceService.instance) {
|
||||
MaintenanceService.instance = this;
|
||||
}
|
||||
return MaintenanceService.instance;
|
||||
}
|
||||
|
||||
let removed = {
|
||||
'posters': 0,
|
||||
'logos': 0,
|
||||
'backdrops': 0
|
||||
};
|
||||
/**
|
||||
* Delete orphan media records from database,
|
||||
* orphan media records are media records that exist in the database
|
||||
* but do not have a file in the `folder data`
|
||||
* @param {boolean} [dry=false] - if true only return the count of the files that are getting deleted, default=`false`
|
||||
*/
|
||||
async deleteOrphanMediaRecords(dry = false) {
|
||||
const allMedia = await Media.findAll();
|
||||
|
||||
// if (missingFiles.length == 0)
|
||||
// return removed;
|
||||
// Getting the userDataPath to store the images
|
||||
const dataPath = await _configService.getMediaFolder();
|
||||
|
||||
for (let i = 0; i < missingFiles.length; i++) {
|
||||
removed[missingFiles[i].type + 's'] += 1;
|
||||
if (!dry) missingFiles[i].destroy();
|
||||
}
|
||||
// Handling different media types
|
||||
const mediaTypeFolder = {
|
||||
'entryposter': path.join(dataPath, 'images', 'entries', 'posters'),
|
||||
'entrylogo': path.join(dataPath, 'images', 'entries', 'logos'),
|
||||
'entrybackdrop': path.join(dataPath, 'images', 'entries', 'backdrops'),
|
||||
|
||||
if (dry) logToFile(`Run a dry missing files check, found: {posters: ${removed.posters}, backdrops: ${removed.backdrops}, logos: ${removed.logos}}`)
|
||||
else logToFile(`Run an missing files cleanup, deleted: {posters: ${removed.posters}, backdrops: ${removed.backdrops}, logos: ${removed.logos}}`)
|
||||
'collectionposter': path.join(dataPath, 'images', 'collections', 'posters'),
|
||||
'collectionlogo': path.join(dataPath, 'images', 'collections', 'logos'),
|
||||
'collectionbackdrop': path.join(dataPath, 'images', 'collections', 'backdrops')
|
||||
};
|
||||
|
||||
return removed;
|
||||
}
|
||||
/**
|
||||
* Delete orphan files from media folder data,
|
||||
* orphan files are files that exist in the `folder data`
|
||||
* but do not have an instance in the database
|
||||
* @param {boolean} [dry=false] - if true only return the count of the files that are getting deleted, default=`false`
|
||||
*/
|
||||
async function deleteOrphanFiles(dry = false) {
|
||||
// Getting the userDataPath to store the images
|
||||
const dataPath = _configService.getMediaFolder();
|
||||
let missingFiles = [];
|
||||
for (let i = 0; i < allMedia.length; i++) {
|
||||
const folderType = `${allMedia[i].mediaable}${allMedia[i].type.replace("_preview", "")}`;
|
||||
|
||||
// ? Getting the path of all media files and putting them in an array
|
||||
// const allMedia = (await Media.findAll()).map(m => {
|
||||
// if (m.mediaable == 'entry')
|
||||
// return path.join(dataPath, 'images', 'entries', m.type + 's', m.path)
|
||||
// else if (m.mediaable == 'collection')
|
||||
// return path.join(dataPath, 'images', 'collections', m.type + 's', m.path)
|
||||
// });
|
||||
|
||||
// ? Getting the name (without extention) of all media in database
|
||||
const allMedia = (await Media.findAll()).map(m => {
|
||||
return m.path.split('.')[0]
|
||||
});
|
||||
|
||||
|
||||
// ? Loop trough the files of the images folder and check
|
||||
// ? if a file doesnt exist in the allMedia array and mark it to delete
|
||||
const mediaTypeFolder = {
|
||||
'entry;poster': path.join(dataPath, 'images', 'entries', 'posters'),
|
||||
'entry;logo': path.join(dataPath, 'images', 'entries', 'logos'),
|
||||
'entry;backdrop': path.join(dataPath, 'images', 'entries', 'backdrops'),
|
||||
|
||||
'collection;poster': path.join(dataPath, 'images', 'collections', 'posters'),
|
||||
'collection;backdrop': path.join(dataPath, 'images', 'collections', 'backdrops')
|
||||
};
|
||||
|
||||
// * counter
|
||||
let removed = {
|
||||
'posters': 0,
|
||||
'logos': 0,
|
||||
'backdrops': 0
|
||||
};
|
||||
|
||||
// Iterate over each media type directory and check for orphan files
|
||||
for (let folderType in mediaTypeFolder) {
|
||||
const directory = mediaTypeFolder[folderType];
|
||||
try {
|
||||
// ? getting all files inside that folder
|
||||
const files = await fsPromises.readdir(directory).then();
|
||||
// console.log(`${directory}, files count: ${files.length}`);
|
||||
|
||||
// Check each file in the directory
|
||||
for (let file of files) {
|
||||
const filePath = path.join(directory, file);
|
||||
// // Check if the constructed file path exists in the allMedia array
|
||||
// // ? this step also takes into account preview files, by removing '_preview'
|
||||
// // ? and verifying if the file path is in database
|
||||
// const hasJpgFormat = allMedia.includes(filePath.replace("_preview.jpg", ".jpg"));
|
||||
// const hasPngFormat = allMedia.includes(filePath.replace("_preview.jpg", ".png"));
|
||||
// const hasGifFormat = allMedia.includes(filePath.replace("_preview.jpg", ".gif"));
|
||||
|
||||
// preparing the file name
|
||||
const fileName = file.split('.')[0].replace("_preview", "");
|
||||
const isInDatabase = allMedia.includes(fileName);
|
||||
|
||||
// if the file isn't in database even on other extentions then delete it
|
||||
if (!isInDatabase) {
|
||||
removed[folderType.split(';')[1] + 's']++; // Increment the count of removed files
|
||||
if (!dry) await fsPromises.unlink(filePath);
|
||||
}
|
||||
const filePath = path.join(mediaTypeFolder[folderType], allMedia[i].path)
|
||||
if (!fs.existsSync(filePath)) {
|
||||
missingFiles.push(allMedia[i]);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`Error handling files in ${directory}:`, err);
|
||||
}
|
||||
|
||||
let removed = {
|
||||
'posters': 0,
|
||||
'logos': 0,
|
||||
'backdrops': 0
|
||||
};
|
||||
|
||||
// if (missingFiles.length == 0)
|
||||
// return removed;
|
||||
|
||||
for (let i = 0; i < missingFiles.length; i++) {
|
||||
removed[missingFiles[i].type + 's'] += 1;
|
||||
if (!dry) missingFiles[i].destroy();
|
||||
}
|
||||
|
||||
if (dry) logToFile(`Run a dry missing files check, found: {posters: ${removed.posters}, backdrops: ${removed.backdrops}, logos: ${removed.logos}}`)
|
||||
else logToFile(`Run an missing files cleanup, deleted: {posters: ${removed.posters}, backdrops: ${removed.backdrops}, logos: ${removed.logos}}`)
|
||||
|
||||
return removed;
|
||||
}
|
||||
|
||||
if (dry) logToFile(`Run a dry orphan files check, found: {posters: ${removed.posters}, backdrops: ${removed.backdrops}, logos: ${removed.logos}}`)
|
||||
else logToFile(`Run an orphan files cleanup, deleted: {posters: ${removed.posters}, backdrops: ${removed.backdrops}, logos: ${removed.logos}}`)
|
||||
/**
|
||||
* Delete orphan files from media folder data,
|
||||
* orphan files are files that exist in the `folder data`
|
||||
* but do not have an instance in the database
|
||||
* @param {boolean} [dry=false] - if true only return the count of the files that are getting deleted, default=`false`
|
||||
*/
|
||||
async deleteOrphanFiles(dry = false) {
|
||||
// Getting the userDataPath to store the images
|
||||
const dataPath = _configService.getMediaFolder();
|
||||
|
||||
// ? Getting the path of all media files and putting them in an array
|
||||
// const allMedia = (await Media.findAll()).map(m => {
|
||||
// if (m.mediaable == 'entry')
|
||||
// return path.join(dataPath, 'images', 'entries', m.type + 's', m.path)
|
||||
// else if (m.mediaable == 'collection')
|
||||
// return path.join(dataPath, 'images', 'collections', m.type + 's', m.path)
|
||||
// });
|
||||
|
||||
// ? Getting the name (without extention) of all media in database
|
||||
const allMedia = (await Media.findAll()).map(m => {
|
||||
return m.path.split('.')[0]
|
||||
});
|
||||
|
||||
|
||||
// ? Loop trough the files of the images folder and check
|
||||
// ? if a file doesnt exist in the allMedia array and mark it to delete
|
||||
const mediaTypeFolder = {
|
||||
'entry;poster': path.join(dataPath, 'images', 'entries', 'posters'),
|
||||
'entry;logo': path.join(dataPath, 'images', 'entries', 'logos'),
|
||||
'entry;backdrop': path.join(dataPath, 'images', 'entries', 'backdrops'),
|
||||
|
||||
'collection;poster': path.join(dataPath, 'images', 'collections', 'posters'),
|
||||
'collection;backdrop': path.join(dataPath, 'images', 'collections', 'backdrops')
|
||||
};
|
||||
|
||||
// * counter
|
||||
let removed = {
|
||||
'posters': 0,
|
||||
'logos': 0,
|
||||
'backdrops': 0
|
||||
};
|
||||
|
||||
// Iterate over each media type directory and check for orphan files
|
||||
for (let folderType in mediaTypeFolder) {
|
||||
const directory = mediaTypeFolder[folderType];
|
||||
try {
|
||||
// ? getting all files inside that folder
|
||||
const files = await fsPromises.readdir(directory).then();
|
||||
// console.log(`${directory}, files count: ${files.length}`);
|
||||
|
||||
// Check each file in the directory
|
||||
for (let file of files) {
|
||||
const filePath = path.join(directory, file);
|
||||
// // Check if the constructed file path exists in the allMedia array
|
||||
// // ? this step also takes into account preview files, by removing '_preview'
|
||||
// // ? and verifying if the file path is in database
|
||||
// const hasJpgFormat = allMedia.includes(filePath.replace("_preview.jpg", ".jpg"));
|
||||
// const hasPngFormat = allMedia.includes(filePath.replace("_preview.jpg", ".png"));
|
||||
// const hasGifFormat = allMedia.includes(filePath.replace("_preview.jpg", ".gif"));
|
||||
|
||||
// preparing the file name
|
||||
const fileName = file.split('.')[0].replace("_preview", "");
|
||||
const isInDatabase = allMedia.includes(fileName);
|
||||
|
||||
// if the file isn't in database even on other extentions then delete it
|
||||
if (!isInDatabase) {
|
||||
removed[folderType.split(';')[1] + 's']++; // Increment the count of removed files
|
||||
if (!dry) await fsPromises.unlink(filePath);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`Error handling files in ${directory}:`, err);
|
||||
}
|
||||
}
|
||||
|
||||
if (dry) logToFile(`Run a dry orphan files check, found: {posters: ${removed.posters}, backdrops: ${removed.backdrops}, logos: ${removed.logos}}`)
|
||||
else logToFile(`Run an orphan files cleanup, deleted: {posters: ${removed.posters}, backdrops: ${removed.backdrops}, logos: ${removed.logos}}`)
|
||||
|
||||
return removed;
|
||||
}
|
||||
|
||||
return removed;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
deleteMissingFilesEntries,
|
||||
deleteOrphanFiles
|
||||
};
|
||||
module.exports = new MaintenanceService();
|
||||
@@ -1,55 +0,0 @@
|
||||
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) {
|
||||
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,
|
||||
}
|
||||
Reference in new issue
Block a user