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

@@ -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 );