entry seen hook pt2
This commit is contained in:
1 parent
efd5ba9642
commit
a5b66b4136
1 file changed
+12
-35
@@ -1,6 +1,8 @@
|
||||
const express = require('express');
|
||||
const EntrySeen = require('../models/EntrySeen');
|
||||
const Entry = require('../models/Entry');
|
||||
const sequelize = require('../db/database');
|
||||
const { Op } = require('sequelize');
|
||||
const entrySeenRouter = express.Router();
|
||||
|
||||
// ! /entry-seen
|
||||
@@ -9,47 +11,18 @@ entrySeenRouter.post('/', async (req, res) => {
|
||||
try {
|
||||
const { entry_id, date_seen, seen_with } = req.body;
|
||||
|
||||
// Fetch the Entry based on entry_id
|
||||
// ? checking the an entry with the given id does exist
|
||||
const entry = await Entry.findByPk(entry_id);
|
||||
if (!entry) {
|
||||
return res.status(404).json({ error: "Entry not found" });
|
||||
}
|
||||
|
||||
const entrySeens = await EntrySeen.findAll({
|
||||
where: {
|
||||
entry_id: entry_id
|
||||
},
|
||||
order: [
|
||||
['date_seen', 'DESC']
|
||||
]
|
||||
});
|
||||
|
||||
let update = {};
|
||||
const dateSeen = new Date(date_seen);
|
||||
|
||||
if (entrySeens.length == 0) { // ? case of first entrySeen
|
||||
update.date_first_seen = date_seen;
|
||||
update.date_last_seen = date_seen;
|
||||
} else { // ? case of 2nd+ entrySeen
|
||||
if (!entry.date_first_seen || dateSeen < new Date(entry.date_first_seen)) {
|
||||
update.date_first_seen = date_seen;
|
||||
}
|
||||
if (!entry.date_last_seen || dateSeen > new Date(entry.date_last_seen)) {
|
||||
update.date_last_seen = date_seen;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the Entry with new first or last seen dates
|
||||
if (Object.keys(update).length > 0) {
|
||||
await entry.update(update);
|
||||
}
|
||||
|
||||
// Create a new EntrySeen record
|
||||
const newEntrySeen = await EntrySeen.create({ entry_id, date_seen, seen_with });
|
||||
res.status(201).json(newEntrySeen);
|
||||
} catch (error) {
|
||||
console.log(`Entry Seen create (post) error: `, error);
|
||||
res.status(400).json({ error: error.message });
|
||||
console.log(error);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -78,7 +51,7 @@ entrySeenRouter.get('/:id', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// * GET - ENTRY SEEN BY ENTRY ID
|
||||
// * GET - ENTRY SEEN LIST BY ENTRY ID
|
||||
entrySeenRouter.get('/by-entry/:entryId', async (req, res) => {
|
||||
try {
|
||||
const { entryId } = req.params;
|
||||
@@ -117,11 +90,15 @@ entrySeenRouter.put('/:id', async (req, res) => {
|
||||
// * DELETE - ENTRY SEEN
|
||||
entrySeenRouter.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
const deleted = await EntrySeen.destroy({ where: { id: req.params.id } });
|
||||
const entrySeen = await EntrySeen.findByPk(req.params.id);
|
||||
|
||||
if (!entrySeen)
|
||||
res.status(404).json({ message: 'Entry seen record not found.' });
|
||||
|
||||
const deleted = await entrySeen.destroy();
|
||||
|
||||
if (deleted) {
|
||||
res.status(204).send();
|
||||
} else {
|
||||
res.status(404).json({ message: 'Entry seen record not found.' });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
|
||||
Reference in new issue
Block a user