implemented episode seen hook for create/update/delete
This commit is contained in:
1 parent
a5b66b4136
commit
f94ec0ba14
2 files changed
+190
-121
No files matched your search
@@ -213,3 +213,177 @@ EntrySeen.afterDestroy(async (instance, options) => {
|
||||
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')
|
||||
};
|
||||
|
||||
// ? 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)
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user