backend/fastify/update-notifications/server.js
2024-05-26 22:28:08 +02:00

29 lines
681 B
JavaScript
Executable File

// Declare a route
module.exports = function (fastify, opts, done) {
const pb = opts.pb;
fastify.post('/update-notifications', async (request, reply) => {
const data = request.body;
const notificationList = data?.unreadList;
let output;
try {
const itemsToUpdate = notificationList?.filter(item => !item.readed);
// Perform updates in parallel
await Promise.all(itemsToUpdate.map(item =>
pb.collection("notifications").update(item, { readed: 'true' })
));
output = 'success'
}
catch(e) {
output = 'failure';
}
reply.send({ items: output })
});
done();
};