feat: Adding groups feature without privacity enabled

This commit is contained in:
Josepablo Cruz
2026-03-30 16:52:38 -06:00
parent 4aefb75ef6
commit e2582f7464
7 changed files with 219 additions and 13 deletions

View File

@@ -237,6 +237,7 @@ The following list of endpoints requires a JWT.
- `GET /loads`: List loads related to my company. - `GET /loads`: List loads related to my company.
- `GET /load-attachments`: List load attachments related to my company or load id. - `GET /load-attachments`: List load attachments related to my company or load id.
- `GET /groups/private`: Get the private group for my company.
### /public-load-tracking ### /public-load-tracking
@@ -350,6 +351,12 @@ This endpoint is part of /loads endpoint
- `GET /users/:companyId` : Get the list of users within a company. - `GET /users/:companyId` : Get the list of users within a company.
- `GET /:id` : Get data from specific company. - `GET /:id` : Get data from specific company.
### /groups
- `GET /private` : Get the private group list from my company. If it does not exist, it will be created.
- `PATCH /private/:id` : Add a company (company id) to the private group list.
- `DELETE /private/:id` : Remove a company (company id) from the private group list.
### /load-attachments ### /load-attachments
- `POST /loading/:id` : Upload/Update a loading attachment. - `POST /loading/:id` : Upload/Update a loading attachment.

View File

@@ -0,0 +1,9 @@
'use strict';
const router = require('express').Router();
const services= require('./services.js');
router.get('/private', services.getPrivateGroup);
router.patch('/private/:id', services.addCompanyToGroup);
router.delete('/private/:id', services.removeCompanyFromGroup);
module.exports = router;

View File

@@ -0,0 +1,116 @@
"use strict";
const { getModel } = require( '../../../lib/Models' );
const { getPagination } = require( '../../../lib/Misc.js' );
const CompanyModel = getModel('companies');
const CompanyGroupModel = getModel('company_groups');
const company_projection = 'company_name rfc categories products truck_type company_city company_state';
async function getOrCreatePrivateGroup( owner ) {
let result = await CompanyGroupModel
.findOne({ owner: owner })
.populate({
path: 'allowedCompanies',
select: company_projection,
populate: {
path: 'categories',
select: '-_id name'
}
});
if( result ){
return result;
}
result = new CompanyGroupModel({
owner: owner,
list_name: "private"
});
await result.save();
return await CompanyGroupModel
.findOne({ owner: owner })
.populate({
path: 'allowedCompanies',
select: company_projection,
populate: {
path: 'categories',
select: '-_id name'
}
});
}
async function getPrivateGroup( req , res ) {
try{
const companyId = req.context.companyId;
const result = await getOrCreatePrivateGroup( companyId );
return res.send( result );
}catch( error ){
console.error( error );
return res.status( 500 ).send({ error });
}
}
async function addCompanyToGroup(req, res){
try{
const companyId = req.context.companyId;
const newEntryId = req.params.id;
if( newEntryId === companyId ){
return res.status(400).send({ error: "can't add yourself" });
}
const company = await CompanyModel.findById( newEntryId );
if( !company ){
return res.status(400).send({ error: "invalid entry id" });
}
let result = await getOrCreatePrivateGroup( companyId );
/// Add to the list only if it is not there already
if( ! result.allowedCompanies.some( entry => entry.id === newEntryId ) ){
await CompanyGroupModel.findByIdAndUpdate(result.id, {
$addToSet: { allowedCompanies: company.id }
});
}
result = await getOrCreatePrivateGroup( companyId );
return res.status(200).send( result );
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
}
async function removeCompanyFromGroup(req, res){
try{
const companyId = req.context.companyId;
const newEntryId = req.params.id;
if( newEntryId === companyId ){
return res.status(400).send({ error: "you are not at the list" });
}
let result = await getOrCreatePrivateGroup( companyId );
/// If the item belongs to the list remove it
if( result.allowedCompanies.some( entry => entry.id === newEntryId ) ){
await CompanyGroupModel.findByIdAndUpdate(result.id, {
$pull: { allowedCompanies: newEntryId }
});
}
result = await getOrCreatePrivateGroup( companyId );
return res.status(200).send( result );
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
}
module.exports = {
getPrivateGroup,
addCompanyToGroup,
removeCompanyFromGroup
};

View File

@@ -17,6 +17,7 @@ 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'); const notifications = require('./notifications/routes.js');
const company_groups = require('./groups/routes.js');
router.use( jwtValidator.middleware ); router.use( jwtValidator.middleware );
router.use( context.middleware ); router.use( context.middleware );
@@ -25,6 +26,7 @@ router.use('/account', account);
router.use('/budgets', budgets); router.use('/budgets', budgets);
router.use('/branches', branches); router.use('/branches', branches);
router.use('/companies', companies); router.use('/companies', companies);
router.use('/groups', company_groups);
router.use('/load-attachments', loadAttachments ); router.use('/load-attachments', loadAttachments );
router.use('/loads', loads); router.use('/loads', loads);
router.use('/loads_driver', loads_driver); router.use('/loads_driver', loads_driver);
@@ -33,17 +35,4 @@ router.use('/proposals', proposals);
router.use('/users', users); router.use('/users', users);
router.use('/vehicles', vehicles); router.use('/vehicles', vehicles);
/*
router.use('/orders', test);
router.use('/mailer', test);
router.use('/memberships', test);
router.use('/bootresolvers', test);
router.use('/news', test);
router.use('/branches', test);
router.use('/trackings', test);
router.use('/upload', test);
router.use('/calendars', test);
router.use('/dashboard', test);
*/
module.exports = router; module.exports = router;

View File

@@ -0,0 +1,12 @@
const mongoose = require('mongoose');
const { Schema } = mongoose;
const schema = new Schema({
owner: { type: Schema.Types.ObjectId, ref: 'companies', required: true }, /// Company owner of this list
list_name: { type: String }, /// Name of the list
allowedCompanies: [{ type: Schema.Types.ObjectId, ref: 'companies' }], /// Behaves as a tuple, not repeated elements
createdAt: { type : Date, required : true, default : () => { return Date.now(); } },
updatedAt: { type : Date, required : true, default : () => { return Date.now(); } }
});
module.exports = mongoose.model( "companygroups", schema );

View File

@@ -4,9 +4,11 @@ const branches = require('./branches.model.js');
const budgets = require('./budgets.model.js'); const budgets = require('./budgets.model.js');
const cities = require('./cities.model.js'); const cities = require('./cities.model.js');
const companies = require('./companies.model.js'); const companies = require('./companies.model.js');
const companygroups = require('./company_groups.models.js')
const countries = require('./countries.model.js'); const countries = require('./countries.model.js');
const load_attachments = require('./load-attachments.model.js'); const load_attachments = require('./load-attachments.model.js');
const loads = require('./loads.model.js'); const loads = require('./loads.model.js');
const loadtemplates = require('./load_templates.model.js');
const mailer = require('./mailer.model.js'); 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');
@@ -34,12 +36,16 @@ function getModel( name ){
return cities; return cities;
case 'companies': case 'companies':
return companies; return companies;
case 'company_groups':
return companygroups;
case 'countries': case 'countries':
return countries; return countries;
case 'load_attachments': case 'load_attachments':
return load_attachments; return load_attachments;
case 'loads': case 'loads':
return loads; return loads;
case 'load_templates':
return loadtemplates;
case 'mailer': case 'mailer':
return mailer; return mailer;
case 'memberships': case 'memberships':

View File

@@ -0,0 +1,67 @@
const mongoose = require('mongoose');
const { Schema } = mongoose;
const address = new Schema({
company_name: { type: String },
street_address1: { type: String },
street_address2: { type: String },
city: { type: String },
state: { type: String },
country: { type: String },
zipcode: { type: String },
landmark: { type: String },
lat: { type: String },
lng: { type: String },
});
const pointSchema = new Schema({
type: {
type: String,
enum: ['Point'],
required: true
},
coordinates: {
type: [Number],
required: true
}
});
const schema = new Schema({
company: { type: Schema.Types.ObjectId, ref: 'companies', required: true },
alert_list: [{ type: String, lowercase: true }],
origin_warehouse : { type: Schema.Types.ObjectId, ref: 'branches' },
destination_warehouse : { type: Schema.Types.ObjectId, ref: 'branches' },
origin: address,
origin_geo: {
type: pointSchema,
},
destination: address,
destination_geo: {
type: pointSchema,
},
categories: [{ type: Schema.Types.ObjectId, ref: 'productcategories' }],
product: { type: Schema.Types.ObjectId, ref: 'products' },
truck_type: { type: String },
tyre_type: { type: String },
weight: { type: Number },
estimated_cost: { type: Number },
distance: { type: Number },
actual_cost: { type: Number },
notes: { type: String },
created_by: { type: Schema.Types.ObjectId, ref: 'users' }, // shipper
createdAt: { type : Date, required : true, default : () => { return Date.now(); } }
});
module.exports = mongoose.model( "loadtemplates", schema );