142 lines
4.3 KiB
JavaScript
142 lines
4.3 KiB
JavaScript
const { Sequelize } = require("sequelize");
|
|
const Entry = require("../../models/Entry");
|
|
const EntryId = require("../../models/EntryId");
|
|
const UserCollection = require("../../models/Collection");
|
|
const { fetchCollectionData, fetchEntryData } = require("../tmdbService");
|
|
const { downloadCollectionMedia, doGeneratePreview } = require("../mediaService");
|
|
const Media = require("../../models/Media");
|
|
const Collection = require("../../models/Collection");
|
|
const { saveEntry } = require("./entryService");
|
|
const { logToFile } = require("../logService");
|
|
const EntryRating = require("../../models/EntryRating");
|
|
const Season = require("../../models/Season");
|
|
const Genre = require("../../models/Genre");
|
|
const UserCollectionEntry = require("../../models/UserCollectionEntry");
|
|
|
|
/**
|
|
* Creates or updates an official collection,
|
|
* it also create an association to all related library entries
|
|
* @collection_tmdbId tmdb id of the collection
|
|
*/
|
|
async function upsertOfficialCollection(collection_tmdbId) {
|
|
let collectionData = await fetchCollectionData(collection_tmdbId);
|
|
|
|
// * 1 - collection info
|
|
const [oldCollection, createdCollection] = await UserCollection.findOrCreate({
|
|
where: { tmdbId: collectionData.id },
|
|
defaults: {
|
|
name: collectionData.name,
|
|
posterPath: collectionData.poster_path,
|
|
backdropPath: collectionData.backdrop_path,
|
|
description: collectionData.overview
|
|
},
|
|
include: [{ model: Media, as: 'medias' }]
|
|
});
|
|
|
|
// * 2 - collection entries
|
|
let collection = oldCollection || createdCollection;
|
|
if (collection) {
|
|
const partIds = collectionData.parts.map(part => part.id);
|
|
|
|
const entries = await Entry.findAll({
|
|
include: [{
|
|
model: EntryId,
|
|
as: 'entryId',
|
|
where: {
|
|
tmdb_id: {
|
|
[Sequelize.Op.in]: partIds
|
|
}
|
|
}
|
|
}]
|
|
});
|
|
|
|
await collection.setEntries(entries);
|
|
|
|
// * 3 - collection media
|
|
// ? downloading
|
|
const downloadedImages = await downloadCollectionMedia(collection);
|
|
|
|
// ? generating preview & inserting data
|
|
for (const image of downloadedImages) {
|
|
// ? generating preview
|
|
doGeneratePreview('collections', image.type + 's', image.path).then((result) => {
|
|
const msg = `Collection Service | Generated Collection Preview`;
|
|
console.log(msg);
|
|
logToFile(msg);
|
|
}).catch((err) => {
|
|
const msg = `Collection Service | Error Generating Collection Preview`;
|
|
console.log(msg);
|
|
console.log(err);
|
|
logToFile(msg);
|
|
logToFile(err);
|
|
});
|
|
|
|
// ? inserting data
|
|
await Media.create({
|
|
mediaable: 'collection',
|
|
mediaableId: collection.id,
|
|
type: image.type,
|
|
path: image.path,
|
|
isLocal: true,
|
|
isSelected: true
|
|
})
|
|
}
|
|
}
|
|
|
|
return await Collection.findByPk(collection.id, {
|
|
include: [{
|
|
model: Entry,
|
|
as: 'entries',
|
|
include: [
|
|
{ model: EntryId, as: 'entryId' },
|
|
{ model: EntryRating, as: 'entryRating' },
|
|
{ model: Season, as: 'seasons' },
|
|
{ model: Media, as: 'medias' },
|
|
{ model: Genre, as: 'genres' },
|
|
],
|
|
through: {
|
|
model: UserCollectionEntry,
|
|
attributes: ['order'], // attributes (to include) from the join model
|
|
},
|
|
}],
|
|
order: [[Sequelize.literal("`entries->UserCollectionEntry`.`order`"), 'ASC']], // sort by 'order' in ascending order
|
|
});;
|
|
}
|
|
|
|
/**
|
|
* Adds the other parts of the collection to the library
|
|
* @collectionId id of the collection
|
|
*/
|
|
async function getCollectionParts(collectionId) {
|
|
const collection = await Collection.findByPk(collectionId);
|
|
const collectionData = await fetchCollectionData(collection.tmdbId);
|
|
|
|
const partIds = collectionData.parts.map(part => part.id);
|
|
|
|
const entries = await Entry.findAll({
|
|
include: [{
|
|
model: EntryId,
|
|
as: 'entryId',
|
|
where: {
|
|
tmdb_id: {
|
|
[Sequelize.Op.in]: partIds
|
|
}
|
|
}
|
|
}]
|
|
});
|
|
|
|
collectionData.parts.forEach(async (part) => {
|
|
if (entries.filter((e) => e.entryId.tmdb_id == part.id).length == 0) {
|
|
const isTvSeries = false;
|
|
const entryData = await fetchEntryData(part.id, isTvSeries);
|
|
const entry = await saveEntry(entryData);
|
|
|
|
await collection.addEntries([entry.id]);
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
upsertOfficialCollection,
|
|
getCollectionParts,
|
|
} |