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;

View File

@@ -16,9 +16,9 @@
}
},
"version" : {
"version" : "1.5.1",
"version" : "1.5.2",
"name": "ETA Beta",
"date":"06/2025"
"date":"21/07/2025"
},
"S3" : {
"bucket": "etaviaporte",

View File

@@ -12,11 +12,18 @@ const proposalsModel = getModel('proposals');
*/
async function onDelivered( userId, elementId ){
const load = await loadsModel.findById( elementId );
if( !load ){
/// Nothing to do, invalid load
return;
}
const proposal_list = await proposalsModel.find({
load: elementId,
is_accepted: true,
is_completed: false
});
const vehicle_list = await vehiclesModel.find({
active_load: elementId
});

View File

@@ -39,6 +39,19 @@ function genKey( len = 6, key="" ){
return otp_str.slice(0,len);
}
/**
* Takes a datetime input and produces a date only output.
* @param {*} date
*/
function normalizeDate( date ){
let in_date = new Date( date );
let year = in_date.getFullYear();
let monthIndex = in_date.getMonth();
let day = in_date.getDate();
// new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds);
return new Date( year, monthIndex, day, 0, 0, 0, 0 );
}
function getPagination( query ){
let limit = {
page : 0,
@@ -101,4 +114,4 @@ async function downloadFile( bucket, key, obj_id ){
return s3resp;
}
module.exports = { genKey , toSha256, getPagination, getPage, queryPage, downloadFile};
module.exports = { genKey , toSha256, normalizeDate, getPagination, getPage, queryPage, downloadFile};