fix: Update code generation of Vehicle/User/Company/Load to avoid collisions

This commit is contained in:
Josepablo C
2024-11-25 17:31:56 -06:00
parent 10d47a166c
commit 682505d333
6 changed files with 20 additions and 16 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
**/node_modules/ **/node_modules/
**/.env **/.env
**/package-lock.json **/package-lock.json
**/migrate.js

View File

@@ -1,7 +1,6 @@
"use strict"; "use strict";
const { ROOT_PATH, LIB_PATH } = process.env;
const { getModel } = require( '../../../lib/Models' ); const { getModel } = require( '../../../lib/Models' );
const { getPagination } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` ); const { getPagination, genKey } = require( '../../../lib/Misc.js' );
const { GenericHandler } = require( '../../../lib/Handlers/Generic.handler.js' ); const { GenericHandler } = require( '../../../lib/Handlers/Generic.handler.js' );
const Model = getModel('loads'); const Model = getModel('loads');
const CompanyModel = getModel('companies'); const CompanyModel = getModel('companies');
@@ -260,7 +259,7 @@ const postLoad = async(req, res) => {
await load.save(); await load.save();
const id = "" + load._id; const id = "" + load._id;
const shipment_code = "ETA-" + id.substring( 0 , 6 ); const shipment_code = "ETA-" + genKey( 6, id );
await Model.findByIdAndUpdate( id , { await Model.findByIdAndUpdate( id , {
shipment_code shipment_code
}); });

View File

@@ -1,6 +1,6 @@
"use strict"; "use strict";
const { getModel } = require( '../../../lib/Models' ); const { getModel } = require( '../../../lib/Models' );
const { getPagination } = require( '../../../lib/Misc' ); const { getPagination, genKey } = require( '../../../lib/Misc' );
const { GenericHandler } = require( '../../../lib/Handlers/Generic.handler' ); const { GenericHandler } = require( '../../../lib/Handlers/Generic.handler' );
const Model = getModel('vehicles'); const Model = getModel('vehicles');
const CompanyModel = getModel('companies'); const CompanyModel = getModel('companies');
@@ -187,7 +187,7 @@ const postVehicle = async(req, res) => {
/// Use id to create vehicle_code /// Use id to create vehicle_code
const vehicle_id = "" + vehicle._id; const vehicle_id = "" + vehicle._id;
const vehicle_code = "C-" + vehicle_id.substring( 0 , 6 ); const vehicle_code = "C-" + genKey( 6, vehicle_id );
await Model.findByIdAndUpdate( vehicle._id , { await Model.findByIdAndUpdate( vehicle._id , {
vehicle_code vehicle_code
}); });

View File

@@ -1,8 +1,7 @@
'user strict'; 'user strict';
const { ROOT_PATH, API_CONFIG, MODELS_PATH, LIB_PATH } = process.env; const { getModel } = require( '../../Models' );
const { getModel } = require( `${ROOT_PATH}/${MODELS_PATH}` ); const apiConfig = require( '../../../config/apiConfig.json' );
const apiConfig = require( `${ROOT_PATH}/${API_CONFIG}` ); const { toSha256, genKey } = require( '../../Misc' );
const { toSha256 } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const UserModel = getModel('users'); const UserModel = getModel('users');
const companiesModels = getModel('companies'); const companiesModels = getModel('companies');
@@ -112,7 +111,13 @@ async function complete_register( userId , data ){
/// Use company.id to create company_code /// Use company.id to create company_code
const company_id = "" + company._id; const company_id = "" + company._id;
const company_code = "C-" + company_id.substring( 0 , 6 );
let company_code = genKey( 6, company_id );
if( company.company_type === "Shipper" ){
company_code = "S-" + company_code
}else{
company_code = "C-" + company_code
}
await companiesModels.findByIdAndUpdate( company._id , { await companiesModels.findByIdAndUpdate( company._id , {
company_code company_code
}); });

View File

@@ -154,7 +154,7 @@ async function createUserWithinCompany( companyId , data ){
// Create user code // Create user code
const id = "" + user._id; const id = "" + user._id;
const employee_id = "E-" + id.substring( 0 , 6 ); const employee_id = "E-" + genKey( 6, id );
await usersModel.findByIdAndUpdate( id , { await usersModel.findByIdAndUpdate( id , {
employee_id employee_id
}); });

View File

@@ -1,6 +1,5 @@
"use strict"; "use strict";
const { ROOT_PATH, API_CONFIG } = process.env; const apiConfig = require( '../config/apiConfig.json' );
const apiConfig = require( `${ROOT_PATH}/${API_CONFIG}` );
const crypto = require('crypto'); const crypto = require('crypto');
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3'); const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3');
@@ -35,9 +34,9 @@ function genKey( len = 5 , key="" ){
throw "invalid key len"; throw "invalid key len";
} }
const shacode = toSha256( key + new Date() + tokenSecret ); const shacode = toSha256( key + new Date() + tokenSecret );
const otp_hex = shacode.slice(0 , len ); /// 16 hex chars = 8 bytes = 64 bits integer
const otp_dec = Number.parseInt( otp_hex , 16 ); const otp_str = "" + Number.parseInt( shacode.slice(0,16) , 16 );
return ""+otp_dec; return otp_str.slice(0,len);
} }
function getPagination( query ){ function getPagination( query ){