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