implemented entry seen hook for create/update/delete
This commit is contained in:
1 parent
e8c4fba943
commit
efd5ba9642
1 file changed
+79
-3
@@ -18,7 +18,7 @@ const sequelize = require('./database');
|
|||||||
// * entry add
|
// * entry add
|
||||||
UserCollectionEntry.afterCreate(async (instance, options) => {
|
UserCollectionEntry.afterCreate(async (instance, options) => {
|
||||||
try {
|
try {
|
||||||
// ? mimicking a change to trigger updatedAt property update for the UserCollection
|
// ? mimicking a change in UserCollection to trigger updatedAt property update for the UserCollection
|
||||||
const userCollections = await UserCollection.findByPk(instance.user_collection_id);
|
const userCollections = await UserCollection.findByPk(instance.user_collection_id);
|
||||||
await userCollections.update({
|
await userCollections.update({
|
||||||
name: sequelize.literal('name')
|
name: sequelize.literal('name')
|
||||||
@@ -42,7 +42,7 @@ UserCollectionEntry.afterCreate(async (instance, options) => {
|
|||||||
UserCollectionEntry.afterUpdate(async (instance, options) => {
|
UserCollectionEntry.afterUpdate(async (instance, options) => {
|
||||||
console.log("we're in after update!!!!!")
|
console.log("we're in after update!!!!!")
|
||||||
try {
|
try {
|
||||||
// ? mimicking a change to trigger updatedAt property update for the UserCollection
|
// ? mimicking a change in UserCollection to trigger updatedAt property update for the UserCollection
|
||||||
const userCollections = await UserCollection.findByPk(instance.user_collection_id);
|
const userCollections = await UserCollection.findByPk(instance.user_collection_id);
|
||||||
await userCollections.update({
|
await userCollections.update({
|
||||||
name: sequelize.literal('name')
|
name: sequelize.literal('name')
|
||||||
@@ -123,17 +123,93 @@ UserCollectionEntry.afterDestroy(async (instance, options) => {
|
|||||||
// ? entry-seen
|
// ? entry-seen
|
||||||
// * entry-seen create
|
// * entry-seen create
|
||||||
EntrySeen.afterCreate(async (instance, options) => {
|
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 = {
|
||||||
|
title: sequelize.literal('title')
|
||||||
|
};
|
||||||
|
|
||||||
|
// ? 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
|
// * entry-seen udpate
|
||||||
EntrySeen.afterUpdate(async (instance, options) => {
|
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
|
// * entry-seen delete
|
||||||
EntrySeen.afterDestroy(async (instance, options) => {
|
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']
|
||||||
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
// ? episode seen
|
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)
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in new issue
Block a user