Major Backend Single Responsibility Update part 1

This commit is contained in:
X4Lo committed 2025-01-25 04:29:42 +01:00
1 parent 65177c7553
commit cbdffb21e3
41 files changed
+1723 -888

No files matched your search

@@ -1,142 +0,0 @@
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,
}
@@ -326,6 +326,7 @@ async function updateEntry(oldEntry, data) {
return savedEntry;
}
// ! this will soon be removed to the new EntryService
async function refrechEntry(entryId) {
// ? checking if the provied id is valid
// ! note: the includes are required for the update
@@ -795,24 +796,10 @@ async function SyncSerieData(entryId) {
return oldEntry;
}
async function getEntry(id) {
return Entry.findByPk(id);
}
async function deleteEntry(id) {
await Entry.destroy({
where: {
id: id
}
});
}
module.exports = {
saveEntry,
updateEntry,
refrechEntry,
getEntry,
deleteEntry,
SyncSeriesData,
SyncSerieData,
}