feat: Adding notifications endpoint
This commit is contained in:
14
scripts/playground.mongodb.js
Normal file
14
scripts/playground.mongodb.js
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
// MongoDB Playground
|
||||||
|
// Use Ctrl+Space inside a snippet or a string literal to trigger completions.
|
||||||
|
|
||||||
|
// The current database to use.
|
||||||
|
use('enrutaviaporte');
|
||||||
|
|
||||||
|
// Create a new document in the collection.
|
||||||
|
db.getCollection('notifications').insertOne({
|
||||||
|
"owner" : ObjectId("65eeadb8ed616b897ca4c4cd"),
|
||||||
|
"title":"Notification title",
|
||||||
|
"description":"Notification description",
|
||||||
|
"tag":"category",
|
||||||
|
"deleted":false
|
||||||
|
});
|
||||||
@@ -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.
|
- `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
|
### /proposals
|
||||||
|
|
||||||
- `GET /find` : Find a list of elements with any of the following fields:
|
- `GET /find` : Find a list of elements with any of the following fields:
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ const loads = require('./loads/routes.js');
|
|||||||
const proposals = require('./proposals/routes.js');
|
const proposals = require('./proposals/routes.js');
|
||||||
const users = require('./users/routes.js');
|
const users = require('./users/routes.js');
|
||||||
const vehicles = require('./vehicles/routes.js');
|
const vehicles = require('./vehicles/routes.js');
|
||||||
|
const notifications = require('./notifications/routes.js');
|
||||||
|
|
||||||
router.use( jwtValidator.middleware );
|
router.use( jwtValidator.middleware );
|
||||||
router.use( context.middleware );
|
router.use( context.middleware );
|
||||||
@@ -25,6 +26,7 @@ router.use('/branches', branches);
|
|||||||
router.use('/companies', companies);
|
router.use('/companies', companies);
|
||||||
router.use('/load-attachments', loadAttachments );
|
router.use('/load-attachments', loadAttachments );
|
||||||
router.use('/loads', loads);
|
router.use('/loads', loads);
|
||||||
|
router.use('/notifications', notifications);
|
||||||
router.use('/proposals', proposals);
|
router.use('/proposals', proposals);
|
||||||
router.use('/users', users);
|
router.use('/users', users);
|
||||||
router.use('/vehicles', vehicles);
|
router.use('/vehicles', vehicles);
|
||||||
|
|||||||
9
v1/src/apps/private/notifications/routes.js
Normal file
9
v1/src/apps/private/notifications/routes.js
Normal 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;
|
||||||
57
v1/src/apps/private/notifications/services.js
Normal file
57
v1/src/apps/private/notifications/services.js
Normal 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 };
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
const { getModel } = require( '../../../lib/Models' );
|
const { getModel } = require( '../../../lib/Models' );
|
||||||
const { getPagination } = require( '../../../lib/Misc' );
|
const { getPagination } = require( '../../../lib/Misc' );
|
||||||
const { GenericHandler } = require( '../../../lib/Handlers/Generic.handler' );
|
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 Model = getModel('proposals');
|
||||||
|
|
||||||
const populate_list = [
|
const populate_list = [
|
||||||
@@ -118,6 +118,8 @@ const postProposal = async(req, res) => {
|
|||||||
}
|
}
|
||||||
const proposal = new Model( data );
|
const proposal = new Model( data );
|
||||||
await proposal.save();
|
await proposal.save();
|
||||||
|
|
||||||
|
await onPostEvent(proposal.id, data);
|
||||||
return res.send( proposal );
|
return res.send( proposal );
|
||||||
}catch(error){
|
}catch(error){
|
||||||
console.error( error );
|
console.error( error );
|
||||||
|
|||||||
@@ -6,9 +6,34 @@ const proposalsModel = getModel('proposals');
|
|||||||
const loadsModel = getModel('loads');
|
const loadsModel = getModel('loads');
|
||||||
const usersModel = getModel('users');
|
const usersModel = getModel('users');
|
||||||
const companiesModel = getModel('companies');
|
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 {*} id
|
||||||
* @param {*} newProposalData
|
* @param {*} newProposalData
|
||||||
* @returns
|
* @returns
|
||||||
@@ -49,4 +74,4 @@ async function onPatchEvent( id , newProposalData ){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { onPatchEvent };
|
module.exports = { onPostEvent, onPatchEvent };
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ const mailer = require('./mailer.model.js');
|
|||||||
const memberships = require('./memberships.model.js');
|
const memberships = require('./memberships.model.js');
|
||||||
const meta_data = require('./meta-data.model.js');
|
const meta_data = require('./meta-data.model.js');
|
||||||
const meta_groups = require('./meta-groups.model.js');
|
const meta_groups = require('./meta-groups.model.js');
|
||||||
|
const notifications = require('./notifications.model.js');
|
||||||
const news = require('./news.model.js');
|
const news = require('./news.model.js');
|
||||||
const orders = require('./orders.model.js');
|
const orders = require('./orders.model.js');
|
||||||
const product_categories = require('./product-categories.model.js');
|
const product_categories = require('./product-categories.model.js');
|
||||||
@@ -47,6 +48,8 @@ function getModel( name ){
|
|||||||
return meta_groups;
|
return meta_groups;
|
||||||
case 'news':
|
case 'news':
|
||||||
return news;
|
return news;
|
||||||
|
case 'notifications':
|
||||||
|
return notifications;
|
||||||
case 'orders':
|
case 'orders':
|
||||||
return orders;
|
return orders;
|
||||||
case 'product_categories':
|
case 'product_categories':
|
||||||
|
|||||||
13
v1/src/lib/Models/notifications.model.js
Normal file
13
v1/src/lib/Models/notifications.model.js
Normal 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 );
|
||||||
Reference in New Issue
Block a user