refs #4823 Minor changes
This commit is contained in:
parent
689371fd3f
commit
efa11df1a4
|
@ -25,7 +25,8 @@ SYNC_ORGANIZATION = true
|
|||
SYNC_WAREHOUSE = true
|
||||
SYNC_CONN = true
|
||||
SYNC_TRADEITEM = true
|
||||
MAX_REQUEST_RETRIES = 3
|
||||
MAX_REQUEST_ATTEMPTS = 3
|
||||
|
||||
#DEV OPTIONS
|
||||
SUPPLIERS_ALWAYS_CONN = false
|
||||
SUPPLIERS_ALWAYS_CONN = false
|
||||
APPLY_ORG_FILTER = false
|
103
utils.js
103
utils.js
|
@ -196,27 +196,18 @@ export async function syncSuppliers(){
|
|||
let timeFinish, timeToGoSec, timeToGoMin, timeLeft;
|
||||
for (let curSequenceNumber = 0; curSequenceNumber <= maxSequenceNumber; curSequenceNumber++) {
|
||||
let timeStart = new moment();
|
||||
let data = (await vnRequest('GET', `${env.API_URL}/organizations/sync/${curSequenceNumber}?organizationType=SUPPLIER`)).data;
|
||||
|
||||
let suppliers = data.results;
|
||||
let suppliers = (await vnRequest('GET', `${env.API_URL}/organizations/sync/${curSequenceNumber}?organizationType=SUPPLIER`)).data.results;
|
||||
for (let supplier of suppliers) {
|
||||
curSequenceNumber = supplier.sequenceNumber;
|
||||
spinner.text = `Syncing suppliers, ${maxSequenceNumber - curSequenceNumber} are missing`
|
||||
if (timeFinish)
|
||||
spinner.text = spinner.text + ` (${timeLeft})`
|
||||
await models.organization.upsert({
|
||||
...supplier,
|
||||
isConnected: JSON.parse(env.SUPPLIERS_ALWAYS_CONN),
|
||||
lastSync: moment(),
|
||||
});
|
||||
if (timeFinish) spinner.text = spinner.text + ` (${timeLeft})`
|
||||
if (JSON.parse(env.APPLY_ORG_FILTER) && supplier.companyGln) // Filtro temporal para quitar los que parecen test
|
||||
await insertOrganization(supplier);
|
||||
};
|
||||
timeFinish = new moment();
|
||||
timeToGoSec = (timeFinish.diff(timeStart, 'seconds') * (maxSequenceNumber - curSequenceNumber) / 1000)
|
||||
timeToGoMin = Math.trunc(timeToGoSec / 60)
|
||||
if (!timeToGoMin)
|
||||
timeLeft = `${Math.trunc(timeToGoSec)} sec`
|
||||
else
|
||||
timeLeft = `${timeToGoMin} min`
|
||||
timeToGoSec = (timeFinish.diff(timeStart, 'seconds') * (maxSequenceNumber - curSequenceNumber) / 1000);
|
||||
timeToGoMin = Math.trunc(timeToGoSec / 60);
|
||||
(!timeToGoMin) ? timeLeft = `${Math.trunc(timeToGoSec)} sec` : timeLeft = `${timeToGoMin} min`;
|
||||
}
|
||||
spinner.text = `Syncing suppliers...`;
|
||||
spinner.succeed()
|
||||
|
@ -235,9 +226,15 @@ export async function syncConnections(){
|
|||
|
||||
let spinner;
|
||||
try {
|
||||
let connectionsInDb = await models.organization.findAll({
|
||||
let connectionsInDb = await models.supplyLine.findAll({
|
||||
include : {
|
||||
model: models.organization,
|
||||
where: {
|
||||
isConnected: true,
|
||||
}
|
||||
},
|
||||
attributes: ['organizationId'],
|
||||
where: { isConnected: true }
|
||||
group: ['organizationId'],
|
||||
});
|
||||
|
||||
const connectionsInFloriday = (await vnRequest('GET', `${env.API_URL}/connections`)).data;
|
||||
|
@ -322,6 +319,25 @@ export async function syncSupplyLines() {
|
|||
if (!supplyLines.length) continue
|
||||
|
||||
for (let supplyLine of supplyLines) {
|
||||
// Check if the warehouse exists, and if it doesn't, create it
|
||||
let warehouse = await models.warehouses.findOne({
|
||||
where: { warehouseId: supplyLine.warehouseId }
|
||||
});
|
||||
if (!warehouse) {
|
||||
let warehouse = (await vnRequest('GET', `${env.API_URL}/warehouses/${supplyLine.warehouseId}`)).data;
|
||||
|
||||
// Check if the organization exists, and if it doesn't, create it
|
||||
let organization = await models.organization.findOne({
|
||||
where: { organizationId: warehouse.organizationId }
|
||||
});
|
||||
if (!organization) {
|
||||
let organization = (await vnRequest('GET', `${env.API_URL}/organizations/${warehouse.organizationId}`)).data;
|
||||
await insertOrganization(organization);
|
||||
}
|
||||
|
||||
await insertWarehouse(warehouse);
|
||||
}
|
||||
|
||||
// Check if the trade item exists, and if it doesn't, create it
|
||||
let tradeItem = await models.tradeItem.findOne({
|
||||
where: { tradeItemId: supplyLine.tradeItemId }
|
||||
|
@ -331,28 +347,18 @@ export async function syncSupplyLines() {
|
|||
await insertItem(tradeItem);
|
||||
}
|
||||
|
||||
// Check if the warehouse exists, and if it doesn't, create it
|
||||
let warehouse = await models.warehouses.findOne({
|
||||
where: { warehouseId: supplyLine.warehouseId }
|
||||
});
|
||||
if (!warehouse) {
|
||||
let warehouse = (await vnRequest('GET', `${env.API_URL}/warehouses/${supplyLine.warehouseId}`)).data;
|
||||
await insertWarehouse(warehouse);
|
||||
}
|
||||
|
||||
|
||||
spinner.text = `Syncing ${i++} supply lines of [${x}|${connectedSuppliers.length}] suppliers...`
|
||||
await models.supplyLine.upsert({
|
||||
...supplyLine,
|
||||
organizationId: supplyLine.supplierOrganizationId,
|
||||
pricePerPiece_currency: supplyLine.pricePerPiece ? supplyLine.pricePerPiece.currency : null,
|
||||
pricePerPiece_value: supplyLine.pricePerPiece ? supplyLine.pricePerPiece.value : null,
|
||||
deliveryPeriod_startDateTime: supplyLine.deliveryPeriod ? supplyLine.deliveryPeriod.startDateTime : null,
|
||||
deliveryPeriod_endDateTime: supplyLine.deliveryPeriod ? supplyLine.deliveryPeriod.endDateTime : null,
|
||||
orderPeriod_startDateTime: supplyLine.orderPeriod ? supplyLine.orderPeriod.startDateTime : null,
|
||||
orderPeriod_endDateTime: supplyLine.orderPeriod ? supplyLine.orderPeriod.endDateTime : null,
|
||||
agreementReference_code: supplyLine.agreementReference ? supplyLine.agreementReference.code : null,
|
||||
agreementReference_description: supplyLine.agreementReference ? supplyLine.agreementReference.description : null,
|
||||
pricePerPiece_currency: supplyLine.pricePerPiece?.currency ?? null,
|
||||
pricePerPiece_value: supplyLine.pricePerPiece?.value ?? null,
|
||||
deliveryPeriod_startDateTime: supplyLine.deliveryPeriod?.startDateTime ?? null,
|
||||
deliveryPeriod_endDateTime: supplyLine.deliveryPeriod?.endDateTime ?? null,
|
||||
orderPeriod_startDateTime: supplyLine.orderPeriod?.startDateTime ?? null,
|
||||
orderPeriod_endDateTime: supplyLine.orderPeriod?.endDateTime ?? null,
|
||||
agreementReference_code: supplyLine.agreementReference?.code ?? null,
|
||||
agreementReference_description: supplyLine.agreementReference?.description ?? null,
|
||||
lastSync: moment(),
|
||||
});
|
||||
|
||||
|
@ -491,6 +497,27 @@ export async function insertWarehouse(warehouse) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert organization in db
|
||||
*
|
||||
* @param {array} organization
|
||||
*/
|
||||
export async function insertOrganization(organization) {
|
||||
let tx;
|
||||
try {
|
||||
tx = await models.sequelize.transaction();
|
||||
await models.organization.upsert({
|
||||
...organization,
|
||||
isConnected: JSON.parse(env.SUPPLIERS_ALWAYS_CONN),
|
||||
lastSync: moment(),
|
||||
});
|
||||
await tx.commit();
|
||||
} catch (err) {
|
||||
await tx.rollback();
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync the warehouses for organizations that are connected
|
||||
**/
|
||||
|
@ -574,7 +601,7 @@ export async function vnRequest(method, url, data, headers) {
|
|||
'X-Api-Key': process.env.API_KEY,
|
||||
};
|
||||
|
||||
for(let i = 0; i < env.MAX_REQUEST_RETRIES; i++) {
|
||||
for(let i = 0; i < env.MAX_REQUEST_ATTEMPTS; i++) {
|
||||
try {
|
||||
if (['GET', 'DELETE'].includes(method))
|
||||
return await axios({method, url, headers});
|
||||
|
@ -598,7 +625,7 @@ export async function vnRequest(method, url, data, headers) {
|
|||
break;
|
||||
case 429: // Too Many Requests
|
||||
warning(err);
|
||||
await sleep(3400); // Stipulated by floryday
|
||||
await sleep(3400); // Stipulated by Floriday
|
||||
break;
|
||||
case 401: // Unauthorized
|
||||
warning(err);
|
||||
|
|
Loading…
Reference in New Issue