Files
ETAApi/v1/src/apps/private/branches/services.js
2025-06-04 13:24:40 -06:00

161 lines
4.8 KiB
JavaScript

"use strict";
const { ROOT_PATH, LIB_PATH, MODELS_PATH, HANDLERS_PATH } = process.env;
const { getModel } = require( `${ROOT_PATH}/${MODELS_PATH}` );
const { getPagination } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { GenericHandler } = require( `${ROOT_PATH}/${HANDLERS_PATH}/Generic.handler.js` );
const Model = getModel('branches');
const populate_list = ['company', 'categories'];
const generic = new GenericHandler( Model, null, populate_list );
function getAndFilterList( query ){
const filter_list = [];
const {
categories,
branch_name,
phone,
city,
state,
truck_type,
type,
zipcode,
alert_list,
} = query;
if( categories ) { filter_list.push({ categories }); }
if( branch_name ) { filter_list.push({ branch_name }); }
if( phone ) { filter_list.push({ phone }); }
if( city ) { filter_list.push({ city }); }
if( state ) { filter_list.push({ state }); }
if( truck_type ) { filter_list.push({ truck_type }); }
if( type ) { filter_list.push({ type }); }
if( zipcode ) { filter_list.push({ zipcode }); }
if( alert_list ) { filter_list.push({ alert_list }); }
if( filter_list.length == 0 ){
return null;
}
return filter_list;
}
async function findElements( companyId , query ){
const { page, elements } = getPagination( query );
const andFilterList = getAndFilterList( query );
let filter;
if( andFilterList ){
andFilterList.push({ company : companyId });
filter = { $and : andFilterList };
}else{
filter = { company : companyId };
}
const { total , limit, skip, data } = await generic.getList( page , elements, filter );
const list_elements = data;
// for(let i=0; i<list_elements.length; i++){
// const item = list_elements[i].toObject();
// if (item.categories) {
// let categories = item.categories.map((c) => c.name);
// item._categories = categories.join(", ");
// }
// if (item.truck_type) {
// item._truck_types = item.truck_type.join(", ");
// }
// list_elements[i] = item;
// }
return {
total,
limit,
skip,
data:list_elements
};
}
async function findElementById( elementId , companyId ){
const filter = {
$and : [
{ _id : elementId },
{ company : companyId }
]
};
let retVal = await Model.findOne( filter ).populate( populate_list ) || {};
return retVal;
}
const findList = async(req, res) => {
try{
const query = req.query || {};
const companyId = req.context.companyId;
const retVal = await findElements( companyId , query );
res.send( retVal );
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
};
const getById = async(req, res) => {
try{
const companyId = req.context.companyId;
const elementId = req.params.id;
res.send( await findElementById( elementId , companyId ) );
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
};
const patchBranch = async(req, res) => {
try{
const companyId = req.context.companyId;
const elementId = req.params.id;
const data = req.body;
const branch = await findElementById( elementId , companyId );
if( !branch ){
throw "You can't modify this branch";
}
if( !data ){
throw "load data not sent";
}
data.company = companyId;
await Model.findByIdAndUpdate( elementId , data );
return res.send( await Model.findById( elementId ) );
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
};
const postBranch = async(req, res) => {
try{
const companyId = req.context.companyId;
const data = req.body;
if( !data ){
throw "Branch data not sent";
}
data.company = companyId;
const branch = new Model( data );
await branch.save();
return res.send( branch );
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
};
const deleteBranch = async(req, res) => {
try{
const companyId = req.context.companyId;
const elementId = req.params.id;
const element = await findElementById( elementId , companyId );
if(!element){
throw "You can't delete this branch";
}
await Model.findByIdAndDelete( elementId );
return res.send(element);
}catch(error){
console.error( error );
return res.status( 500 ).send({ error });
}
};
module.exports = { findList, getById, patchBranch, postBranch, deleteBranch };