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

View File

@@ -1,8 +1,7 @@
'user strict';
const { ROOT_PATH, API_CONFIG, MODELS_PATH, LIB_PATH } = process.env;
const { getModel } = require( `${ROOT_PATH}/${MODELS_PATH}` );
const apiConfig = require( `${ROOT_PATH}/${API_CONFIG}` );
const { toSha256 } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { getModel } = require( '../../Models' );
const apiConfig = require( '../../../config/apiConfig.json' );
const { toSha256, genKey } = require( '../../Misc' );
const UserModel = getModel('users');
const companiesModels = getModel('companies');
@@ -112,7 +111,13 @@ async function complete_register( userId , data ){
/// Use company.id to create company_code
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 , {
company_code
});

View File

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

View File

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