389 lines
13 KiB
JavaScript
389 lines
13 KiB
JavaScript
// Import models
|
|
const Franchise = require('../models/Franchise');
|
|
const Entry = require('../models/Entry')
|
|
const EntryId = require('../models/EntryId')
|
|
const EntryRating = require('../models/EntryRating')
|
|
const EntrySeen = require('../models/EntrySeen');
|
|
const UserCollection = require('../models/Collection');
|
|
const UserCollectionEntry = require('../models/UserCollectionEntry');
|
|
const Season = require('../models/Season');
|
|
const Episode = require('../models/Episode');
|
|
const EpisodeSeen = require('../models/EpisodeSeen');
|
|
const Genre = require('../models/Genre');
|
|
const EntryGenre = require('../models/EntryGenre');
|
|
const sequelize = require('./database');
|
|
|
|
// ! Hooks
|
|
// ? user collections
|
|
// * entry add
|
|
UserCollectionEntry.afterCreate(async (instance, options) => {
|
|
try {
|
|
// ? mimicking a change in UserCollection to trigger updatedAt property update for the UserCollection
|
|
const userCollections = await UserCollection.findByPk(instance.user_collection_id);
|
|
await userCollections.update({
|
|
name: sequelize.literal('name')
|
|
});
|
|
const userCollectionEntriesCount = await UserCollectionEntry.count({
|
|
where: {
|
|
user_collection_id: instance.user_collection_id
|
|
}
|
|
})
|
|
|
|
// ? updating newly create UserCollectionEntry order
|
|
await instance.update({
|
|
order: userCollectionEntriesCount
|
|
})
|
|
} catch (error) {
|
|
console.log(`Sequelize Hook Error: UserCollectionEntry.afterCreate: `, error)
|
|
}
|
|
});
|
|
|
|
// * entry update (order)
|
|
UserCollectionEntry.afterUpdate(async (instance, options) => {
|
|
try {
|
|
// ? mimicking a change in UserCollection to trigger updatedAt property update for the UserCollection
|
|
const userCollections = await UserCollection.findByPk(instance.user_collection_id);
|
|
await userCollections.update({
|
|
name: sequelize.literal('name')
|
|
});
|
|
|
|
// ? updating other entries order
|
|
const collectionEntries = await UserCollectionEntry.findAll({
|
|
where: {
|
|
user_collection_id: instance.user_collection_id
|
|
},
|
|
order: [['order', 'ASC']]
|
|
})
|
|
|
|
// * changing the order of all entries that are now after the updated entry
|
|
let startOrder = instance.order + 1;
|
|
for (let i = 0; i < collectionEntries.length; i++) {
|
|
if (collectionEntries[i].order >= instance.order && collectionEntries[i].entry_id != instance.entry_id) {
|
|
await collectionEntries[i].update({ order: startOrder }, { hooks: false })
|
|
startOrder++;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.log(`Sequelize Hook Error: UserCollectionEntry.afterUpdate: `, error)
|
|
}
|
|
});
|
|
|
|
// * entry remove
|
|
UserCollectionEntry.afterDestroy(async (instance, options) => {
|
|
try {
|
|
const userCollection = await UserCollection.findByPk(instance.user_collection_id);
|
|
const entry = await Entry.findByPk(instance.entry_id);
|
|
|
|
if (!entry || !userCollection) return;
|
|
|
|
// ? mimicking a change to trigger updatedAt property update
|
|
let updates = {
|
|
name: sequelize.literal('name')
|
|
}
|
|
|
|
const collectionsEntries = await UserCollectionEntry.findAll({
|
|
where: {
|
|
user_collection_id: instance.user_collection_id
|
|
},
|
|
include: [
|
|
{ model: Entry, as: 'entry' },
|
|
]
|
|
})
|
|
|
|
// ? updating collection poster
|
|
// ? remove the poster if the collection is empty now
|
|
// if (collectionsEntries.length == 0) {
|
|
// updates.poster = null;
|
|
// } else {
|
|
// // ? replace poster it belonged to the removed entry
|
|
// // if (userCollection.poster_path == entry.poster_path) {
|
|
// // updates.poster_path = collectionsEntries[0].entry.poster_path;
|
|
// // }
|
|
// }
|
|
|
|
// ? updating order, only if the removed entry isn't placed at the end
|
|
let order = instance.order;
|
|
if (instance.order != collectionsEntries.length) {
|
|
for (let i = 0; i < collectionsEntries.length; i++) {
|
|
if (collectionsEntries[i].order > instance.order) {
|
|
collectionsEntries[i].update({ order: order }, { hooks: false })
|
|
order++;
|
|
}
|
|
}
|
|
}
|
|
|
|
// ? updating the collection updatedAt and poster maybe
|
|
await userCollection.update(updates)
|
|
} catch (error) {
|
|
console.log(`Sequelize Hook Error: UserCollectionEntry.afterDestroy: `, error)
|
|
}
|
|
});
|
|
|
|
// ? entry-seen
|
|
// * entry-seen create
|
|
EntrySeen.afterCreate(async (instance, options) => {
|
|
try {
|
|
// ? getting the entry that we just created the entry-seen for
|
|
const entry = await Entry.findByPk(instance.entry_id);
|
|
|
|
// this will ensure that the entry is updated anyways to trigger the updatedAt property
|
|
let update = {
|
|
watchlist: false
|
|
};
|
|
|
|
// ? comparing the instance.date_seen with the entry date_first_seen &
|
|
// ? date_last_seen to check if they should be replaced
|
|
if (!entry.date_first_seen || instance.date_seen < new Date(entry.date_first_seen)) {
|
|
update.date_first_seen = instance.date_seen;
|
|
}
|
|
if (!entry.date_last_seen || instance.date_seen > new Date(entry.date_last_seen)) {
|
|
update.date_last_seen = instance.date_seen;
|
|
}
|
|
|
|
// * updating the entry
|
|
await entry.update(update);
|
|
} catch (error) {
|
|
console.log(`Sequelize Hook Error: EntrySeen.afterCreate: `, error)
|
|
}
|
|
});
|
|
|
|
// * entry-seen udpate
|
|
EntrySeen.afterUpdate(async (instance, options) => {
|
|
try {
|
|
// ? getting the entry that we just created the entry-seen for
|
|
const entry = await Entry.findByPk(instance.entry_id);
|
|
|
|
// ? getting the list of entry-seen for the current entry, sorted by date_seen
|
|
// ? then asigning the first one in the array to: date_first_seen
|
|
// ? and the last one to: date_last_seen
|
|
const entrySeens = await EntrySeen.findAll({
|
|
where: {
|
|
entry_id: instance.entry_id
|
|
},
|
|
order: [
|
|
['date_seen', 'ASC']
|
|
]
|
|
});
|
|
|
|
let update = {};
|
|
update.date_first_seen = entrySeens[0].date_seen;
|
|
update.date_last_seen = entrySeens[entrySeens.length - 1].date_seen;
|
|
|
|
// * updating the entry
|
|
await entry.update(update);
|
|
} catch (error) {
|
|
console.log(`Sequelize Hook Error: EntrySeen.afterUpdate: `, error)
|
|
}
|
|
});
|
|
|
|
// * entry-seen delete
|
|
EntrySeen.afterDestroy(async (instance, options) => {
|
|
try {
|
|
// ? getting the entry that we just created the entry-seen for
|
|
const entry = await Entry.findByPk(instance.entry_id);
|
|
|
|
// ? getting the list of entry-seen for the current entry, sorted by date_seen
|
|
// ? then asigning the first one in the array to: date_first_seen
|
|
// ? and the last one to: date_last_seen
|
|
const entrySeens = await EntrySeen.findAll({
|
|
where: {
|
|
entry_id: instance.entry_id
|
|
},
|
|
order: [
|
|
['date_seen', 'ASC']
|
|
]
|
|
});
|
|
|
|
let update = {};
|
|
|
|
// ? if the deleted entry-seen was the only one available for the current entry
|
|
// ? then set date_first_seen & date_last_seen values to `null`
|
|
if (entrySeens.length == 0) {
|
|
update.date_first_seen = null;
|
|
update.date_last_seen = null;
|
|
} else {
|
|
update.date_first_seen = entrySeens[0].date_seen;
|
|
update.date_last_seen = entrySeens[entrySeens.length - 1].date_seen;
|
|
}
|
|
|
|
// * updating the entry
|
|
await entry.update(update);
|
|
} catch (error) {
|
|
console.log(`Sequelize Hook Error: EntrySeen.afterUpdate: `, error)
|
|
}
|
|
});
|
|
|
|
// ? episode seen
|
|
// * episode-seen create
|
|
EpisodeSeen.afterCreate(async (instance, options) => {
|
|
try {
|
|
// ! updating the entry
|
|
// ? getting the entry that we just created the episode-seen for
|
|
const entry = await Entry.findByPk(instance.entry_id);
|
|
|
|
// this will ensure that the entry is updated anyways to trigger the updatedAt property
|
|
let update = {
|
|
// title: sequelize.literal('title')
|
|
watchlist: false
|
|
};
|
|
|
|
// ? comparing the instance.date_seen with the entry date_first_seen &
|
|
// ? date_last_seen to check if they should be replaced
|
|
if (!entry.date_first_seen || instance.date_seen < new Date(entry.date_first_seen)) {
|
|
update.date_first_seen = instance.date_seen;
|
|
}
|
|
if (!entry.date_last_seen || instance.date_seen > new Date(entry.date_last_seen)) {
|
|
update.date_last_seen = instance.date_seen;
|
|
}
|
|
|
|
// * updating the entry
|
|
await entry.update(update);
|
|
|
|
// ! updating the episode
|
|
// ? getting the episode that we just created the episode-seen for
|
|
const episode = await Episode.findByPk(instance.episode_id);
|
|
|
|
// this will ensure that the episode is updated anyways to trigger the updatedAt property
|
|
let episodeUpdate = {
|
|
name: sequelize.literal('name')
|
|
};
|
|
|
|
// ? comparing the instance.date_seen with the entry date_first_seen &
|
|
// ? date_last_seen to check if they should be replaced
|
|
if (!episode.date_first_seen || instance.date_seen < new Date(episode.date_first_seen)) {
|
|
episodeUpdate.date_first_seen = instance.date_seen;
|
|
}
|
|
if (!episode.date_last_seen || instance.date_seen > new Date(episode.date_last_seen)) {
|
|
episodeUpdate.date_last_seen = instance.date_seen;
|
|
}
|
|
|
|
// * updating the episode
|
|
await episode.update(episodeUpdate);
|
|
} catch (error) {
|
|
console.log(`Sequelize Hook Error: EpisodeSeen.afterCreate: `, error)
|
|
}
|
|
});
|
|
|
|
// * episode-seen udpate
|
|
EpisodeSeen.afterUpdate(async (instance, options) => {
|
|
try {
|
|
// ! updating the entry
|
|
// ? getting the entry that we just created the episode-seen for
|
|
const entry = await Entry.findByPk(instance.entry_id);
|
|
|
|
// ? getting the min & max values of the column date_seen from episode-seen
|
|
// ? for the current entry
|
|
EpisodeSeen.findAll({
|
|
attributes: [
|
|
[sequelize.fn('min', sequelize.col('date_seen')), 'minValue'],
|
|
[sequelize.fn('max', sequelize.col('date_seen')), 'maxValue']
|
|
],
|
|
where: {
|
|
entry_id: instance.entry_id
|
|
}
|
|
}).then(result => {
|
|
let update = {};
|
|
|
|
update.date_first_seen = result[0].dataValues.minValue;
|
|
update.date_last_seen = result[0].dataValues.maxValue;
|
|
|
|
entry.update(update);
|
|
}).catch(err => {
|
|
console.error('Error:', err);
|
|
});
|
|
|
|
// ! updating the episode
|
|
// ? getting the episode that we just created the episode-seen for
|
|
const episode = await Episode.findByPk(instance.episode_id);
|
|
|
|
// ? getting the min & max values of the column date_seen from episode-seen
|
|
// ? for the current episode
|
|
EpisodeSeen.findAll({
|
|
attributes: [
|
|
[sequelize.fn('min', sequelize.col('date_seen')), 'minValue'],
|
|
[sequelize.fn('max', sequelize.col('date_seen')), 'maxValue']
|
|
],
|
|
where: {
|
|
episode_id: instance.episode_id
|
|
}
|
|
}).then(result => {
|
|
let update = {};
|
|
update.date_first_seen = result[0].dataValues.minValue;
|
|
update.date_last_seen = result[0].dataValues.maxValue;
|
|
episode.update(update);
|
|
}).catch(err => {
|
|
console.error('Error:', err);
|
|
});
|
|
} catch (error) {
|
|
console.log(`Sequelize Hook Error: EpisodeSeen.afterUpdate: `, error)
|
|
}
|
|
});
|
|
|
|
// * episode-seen delete
|
|
EpisodeSeen.afterDestroy(async (instance, options) => {
|
|
try {
|
|
// ! updating the entry
|
|
// ? getting the entry that we just created the entry-seen for
|
|
const entry = await Entry.findByPk(instance.entry_id);
|
|
|
|
// ? getting the list of entry-seen for the current entry, sorted by date_seen
|
|
// ? then asigning the first one in the array to: date_first_seen
|
|
// ? and the last one to: date_last_seen
|
|
const episodeSeens = await EpisodeSeen.findAll({
|
|
where: {
|
|
entry_id: instance.entry_id
|
|
},
|
|
order: [
|
|
['date_seen', 'ASC']
|
|
]
|
|
});
|
|
|
|
let update = {};
|
|
|
|
// ? if the deleted entry-seen was the only one available for the current entry
|
|
// ? then set date_first_seen & date_last_seen values to `null`
|
|
if (episodeSeens.length == 0) {
|
|
update.date_first_seen = null;
|
|
update.date_last_seen = null;
|
|
} else {
|
|
update.date_first_seen = episodeSeens[0].date_seen;
|
|
update.date_last_seen = episodeSeens[episodeSeens.length - 1].date_seen;
|
|
}
|
|
|
|
// * updating the entry
|
|
await entry.update(update);
|
|
|
|
// ! updating the episode
|
|
// ? getting the entry that we just created the episode-seen for
|
|
const episode = await Episode.findByPk(instance.episode_id);
|
|
|
|
// ? getting the list of episode-seen for the current episode, sorted by date_seen
|
|
// ? then asigning the first one in the array to: date_first_seen
|
|
// ? and the last one to: date_last_seen
|
|
const episodeSeens2 = await EpisodeSeen.findAll({
|
|
where: {
|
|
episode_id: instance.episode_id
|
|
},
|
|
order: [
|
|
['date_seen', 'ASC']
|
|
]
|
|
});
|
|
|
|
let episodeUpdate = {};
|
|
|
|
// ? if the deleted episode-seen was the only one available for the current episode
|
|
// ? then set date_first_seen & date_last_seen values to `null`
|
|
if (episodeSeens2.length == 0) {
|
|
episodeUpdate.date_first_seen = null;
|
|
episodeUpdate.date_last_seen = null;
|
|
} else {
|
|
episodeUpdate.date_first_seen = episodeSeens2[0].date_seen;
|
|
episodeUpdate.date_last_seen = episodeSeens2[episodeSeens2.length - 1].date_seen;
|
|
}
|
|
|
|
// * updating the episode
|
|
await episode.update(episodeUpdate);
|
|
} catch (error) {
|
|
console.log(`Sequelize Hook Error: EpisodeSeen.afterUpdate: `, error)
|
|
}
|
|
}); |