fix(loads): Normalize dates on post and patch (remove time from dates)

This commit is contained in:
Josepablo C
2025-07-21 12:07:46 -06:00
parent c9d5566288
commit 43bcc7c0f3
72 changed files with 118 additions and 3273 deletions

View File

@@ -1,6 +1,6 @@
"use strict";
const { getModel } = require( '../../../lib/Models' );
const { getPagination, genKey } = require( '../../../lib/Misc.js' );
const { getPagination, genKey, normalizeDate } = require( '../../../lib/Misc.js' );
const { GenericHandler } = require( '../../../lib/Handlers/Generic.handler.js' );
const { onPatchEvent } = require('../../../lib/Handlers/Loads.handler');
const Model = getModel('loads');
@@ -275,6 +275,25 @@ const getById = async(req, res) => {
}
};
function normalizeDatesFromData( data ){
let fields2normalize = [
"contract_start_date",
"contract_end_date",
"est_loading_date",
"est_unloading_date",
"published_date",
"loaded_date",
"transit_date",
"delivered_date",
];
for( const item of fields2normalize ){
if( Object.hasOwn( data, item ) ){
data[ item ] = normalizeDate( data[ item ] )
}
}
return data;
}
function getDataToModify( data ){
/**
* Take the only fields from model that
@@ -329,7 +348,7 @@ function getDataToModify( data ){
throw "nothing to change";
}
return filtered_data;
return normalizeDatesFromData( filtered_data );
}
const patchLoad = async(req, res) => {
try{
@@ -357,23 +376,92 @@ const patchLoad = async(req, res) => {
}
};
function getDataToCreate( data ){
/**
* Take the only fields from model that
* should be modifiable by the client.
* The rest are populated on demand by the event handlers.
*/
let data_fields = {
carrier: null,
vehicle: null,
driver: null,
bidder: null,
alert_list: null,
origin_warehouse: null,
destination_warehouse: null,
origin: null,
origin_geo: null,
destination: null,
destination_geo: null,
categories: null,
product: null,
truck_type: null,
tyre_type: null,
weight: null,
estimated_cost: null,
distance: null,
actual_cost: null,
status: null,
load_status: null,
contract_start_date: null,
contract_end_date: null,
est_loading_date: null,
est_unloading_date: null,
published_date: null,
loaded_date: null,
transit_date: null,
delivered_date: null,
notes: null,
};
let filtered_data = {};
if( Object.keys( data_fields ).length === 0 ){
throw "no data to add";
}
for ( const [key, value] of Object.entries( data_fields ) ) {
if( Object.hasOwn( data, key ) ){
filtered_data[ key ] = data[ key ];
}
}
if( Object.keys( filtered_data ).length === 0 ){
throw "no data to add";
}
return normalizeDatesFromData( filtered_data );
}
const postLoad = async(req, res) => {
try{
const companyId = req.context.companyId;
const userId = req.context.userId;
const user_name = req.context.user.first_name;
let user_name;
if( req.context.user.last_name ){
user_name = `${req.context.user.first_name} ${req.context.user.last_name}`;
}else{
user_name = req.context.user.first_name;
}
const permissions = req.context.permissions;
const data = req.body;
if( !data ){
if( !req.body ){
throw "Load data not sent";
}
if(permissions !== "role_shipper" ){
throw "You can't create loads";
}
const data = getDataToCreate( req.body );
data.company = companyId;
data.posted_by = userId;
data.name = user_name;
const load = new Model( data );/// ToDo, check data content and normalize Dates!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
data.posted_by_name = user_name;
const load = new Model( data );
await load.save();
const id = "" + load._id;