implemented hooks for user-collections & user-collection-entry models to update updatedAt and order fields
This commit is contained in:
1 parent
36866dabe6
commit
cea1b207a3
12 files changed
+170
-61
No files matched your search
+1
-15
@@ -102,18 +102,4 @@ EpisodeSeen.belongsTo(Entry, { foreignKey: 'entry_id' });
|
||||
|
||||
// * ----
|
||||
Franchise.hasMany(Entry, { foreignKey: 'franchise_id' });
|
||||
Entry.belongsTo(Franchise, { foreignKey: 'franchise_id' });
|
||||
|
||||
|
||||
// ! TRIGGERS
|
||||
UserCollectionEntry.afterUpdate(async (instance, options) => {
|
||||
await UserCollection.update(instance.user_collection_id, {
|
||||
updatedAt: new Date()
|
||||
})
|
||||
});
|
||||
|
||||
UserCollectionEntry.afterDestroy(async (instance, options) => {
|
||||
await UserCollection.update(instance.user_collection_id, {
|
||||
updatedAt: new Date()
|
||||
})
|
||||
});
|
||||
Entry.belongsTo(Franchise, { foreignKey: 'franchise_id' });
|
||||
@@ -0,0 +1,127 @@
|
||||
// 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/UserCollection');
|
||||
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 {
|
||||
const userCollections = await UserCollection.findByPk(instance.user_collection_id);
|
||||
|
||||
const userCollectionEntriesCount = await UserCollectionEntry.count({
|
||||
where: {
|
||||
user_collection_id: instance.user_collection_id
|
||||
}
|
||||
})
|
||||
|
||||
// ? updating the order
|
||||
await instance.update({
|
||||
order: userCollectionEntriesCount
|
||||
})
|
||||
|
||||
|
||||
// ? mimicking a change to trigger updatedAt property update
|
||||
await userCollections.update({
|
||||
name: sequelize.literal('name')
|
||||
})
|
||||
} catch (error) {
|
||||
console.log(`Sequelize Hook Error: UserCollectionEntry.afterCreate: `, error)
|
||||
}
|
||||
});
|
||||
|
||||
// * entry update (order)
|
||||
UserCollectionEntry.afterUpdate(async (instance, options) => {
|
||||
try {
|
||||
const userCollections = await UserCollection.findByPk(instance.user_collection_id);
|
||||
|
||||
// ? mimicking a change to trigger updatedAt property update
|
||||
await userCollections.update({
|
||||
name: sequelize.literal('name')
|
||||
})
|
||||
|
||||
// TODO reorder entries
|
||||
} 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')
|
||||
}
|
||||
|
||||
// ? updating collection poster
|
||||
const collectionsEntries = await UserCollectionEntry.findAll({
|
||||
where: {
|
||||
user_collection_id: instance.user_collection_id
|
||||
},
|
||||
include: [
|
||||
{ model: Entry, as: 'entry' },
|
||||
]
|
||||
})
|
||||
|
||||
// ? remove the poster if the collection is empty now
|
||||
if (collectionsEntries.length == 0) {
|
||||
updates.poster = null;
|
||||
} else {
|
||||
// todo add is customPoster field
|
||||
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 })
|
||||
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 (isntance, options) => {
|
||||
|
||||
});
|
||||
|
||||
// * entry-seen udpate
|
||||
EntrySeen.afterUpdate(async (isntance, options) => {
|
||||
|
||||
});
|
||||
|
||||
// * entry-seen delete
|
||||
EntrySeen.afterDestroy(async (isntance, options) => {
|
||||
|
||||
});
|
||||
|
||||
// ? episode seen
|
||||
Reference in new issue
Block a user