diff --git a/backend/db/sequelizeHooks.js b/backend/db/sequelizeHooks.js index 413fd8a..4b8f0f3 100644 --- a/backend/db/sequelizeHooks.js +++ b/backend/db/sequelizeHooks.js @@ -18,7 +18,7 @@ const sequelize = require('./database'); // * entry add UserCollectionEntry.afterCreate(async (instance, options) => { 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); await userCollections.update({ name: sequelize.literal('name') @@ -42,7 +42,7 @@ UserCollectionEntry.afterCreate(async (instance, options) => { UserCollectionEntry.afterUpdate(async (instance, options) => { console.log("we're in after update!!!!!") 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); await userCollections.update({ name: sequelize.literal('name') @@ -123,17 +123,93 @@ UserCollectionEntry.afterDestroy(async (instance, options) => { // ? 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 = { + 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 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 \ No newline at end of file