feat: Adding notifications endpoint

This commit is contained in:
Josepablo C
2024-08-09 21:16:24 -06:00
parent 288fdc10a7
commit 9b24704f76
9 changed files with 137 additions and 3 deletions

View File

@@ -391,6 +391,15 @@ This endpoint is part of /loads endpoint
- `GET /loads/find?$sort[createdAt]=-1&load_status=Loading`: Get loads with status "Loading" sorted by createdAt.
### /notifications
- `GET /` : Get list of latest notifications:
- `DELETE /:id` : Delete a single notification.
In order to delete a list of notifications use the following query:
`DELETE ?list[0]=66b6d50ce91a3a5b96686863&list[1]=66b6d50ce91a3a5b96686863&list[2]=66b6d50cf193e37bb1dd913d`
### /proposals
- `GET /find` : Find a list of elements with any of the following fields:

View File

@@ -15,6 +15,7 @@ const loads = require('./loads/routes.js');
const proposals = require('./proposals/routes.js');
const users = require('./users/routes.js');
const vehicles = require('./vehicles/routes.js');
const notifications = require('./notifications/routes.js');
router.use( jwtValidator.middleware );
router.use( context.middleware );
@@ -25,6 +26,7 @@ router.use('/branches', branches);
router.use('/companies', companies);
router.use('/load-attachments', loadAttachments );
router.use('/loads', loads);
router.use('/notifications', notifications);
router.use('/proposals', proposals);
router.use('/users', users);
router.use('/vehicles', vehicles);

View File

@@ -0,0 +1,9 @@
'use strict';
const router = require('express').Router();
const services= require('./services.js');
router.get('/', services.getNotifications );
router.delete('/', services.deleteNotifications );
router.delete('/:id', services.deleteById );
module.exports = router;

View File

@@ -0,0 +1,57 @@
"use strict";
const Notifications = require( '../../../lib/Models/notifications.model' );
async function getNotifications(req, res, next) {
try{
const userId = req.context.userId;
const list = await Notifications.find( {
owner : userId,
deleted : false
} );
return res.send( list );
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
}
async function deleteNotifications(req, res, next) {
try{
const userId = req.context.userId;
const { list } = req.query;
const ret = await Notifications.deleteMany({
_id : list,
owner: userId
});
return res.send( {
msg : "Done"
} );
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
}
async function deleteById(req, res, next) {
try{
const userId = req.context.userId;
const notificationId = req.params.id;
const ret = await Notifications.deleteOne( {
_id : notificationId,
owner: userId
} );
return res.send( {
msg : "Done"
} );
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
}
module.exports = { getNotifications, deleteNotifications, deleteById };

View File

@@ -3,7 +3,7 @@
const { getModel } = require( '../../../lib/Models' );
const { getPagination } = require( '../../../lib/Misc' );
const { GenericHandler } = require( '../../../lib/Handlers/Generic.handler' );
const { onPatchEvent } = require('../../../lib/Handlers/Proposals.handler');
const { onPostEvent, onPatchEvent } = require('../../../lib/Handlers/Proposals.handler');
const Model = getModel('proposals');
const populate_list = [
@@ -118,6 +118,8 @@ const postProposal = async(req, res) => {
}
const proposal = new Model( data );
await proposal.save();
await onPostEvent(proposal.id, data);
return res.send( proposal );
}catch(error){
console.error( error );

View File

@@ -6,9 +6,34 @@ const proposalsModel = getModel('proposals');
const loadsModel = getModel('loads');
const usersModel = getModel('users');
const companiesModel = getModel('companies');
const notificationsModel = getModel('notifications');
/**
* When the proposal is accepted then the load should be updated to have the
* When the proposal is created then the load owner should be notified
* @param {*} id
* @param {*} newProposalData
* @returns
*/
async function onPostEvent( id , newProposalData ){
const proposal = await proposalsModel.findById( id );
const load = await loadsModel.findById( proposal.load );
const user = await usersModel.findById( load.posted_by );
const notification = new notificationsModel({
"owner": user.id,
"title": "New proposal",
"description": `Your load ${load.shipment_code} has a new proposal!`,
"tag":"new_proposal",
"deleted":false
});
await notification.save();
}
/**
* When the proposal is accepted then the load should be updated to have the latest
* shipper, vehicle, etc.
* @param {*} id
* @param {*} newProposalData
* @returns
@@ -49,4 +74,4 @@ async function onPatchEvent( id , newProposalData ){
}
}
module.exports = { onPatchEvent };
module.exports = { onPostEvent, onPatchEvent };

View File

@@ -11,6 +11,7 @@ const mailer = require('./mailer.model.js');
const memberships = require('./memberships.model.js');
const meta_data = require('./meta-data.model.js');
const meta_groups = require('./meta-groups.model.js');
const notifications = require('./notifications.model.js');
const news = require('./news.model.js');
const orders = require('./orders.model.js');
const product_categories = require('./product-categories.model.js');
@@ -47,6 +48,8 @@ function getModel( name ){
return meta_groups;
case 'news':
return news;
case 'notifications':
return notifications;
case 'orders':
return orders;
case 'product_categories':

View File

@@ -0,0 +1,13 @@
const mongoose = require('mongoose');
const { Schema } = mongoose;
const schema = new Schema({
owner: { type: Schema.Types.ObjectId, ref: 'users' },
title: { type: String, required : true },
description: { type: String, required : true },
tag: { type: String, require, required : true },
createdAt: { type : Date, required : true, default : () => { return Date.now(); } },
deleted: { type: Boolean, default: false, required : true }
});
module.exports = mongoose.model( "notifications", schema );