refs #4823 Added clockPresalesSupply
This commit is contained in:
parent
f4be5fbc09
commit
52d2e783c8
|
@ -58,7 +58,8 @@ class Floriday {
|
|||
await utils.syncConnections();
|
||||
await utils.syncSupplyLines();
|
||||
|
||||
// ------------------------
|
||||
// Deshabilitado porque no hay de momento
|
||||
// await utils.syncClockPresalesSupply();
|
||||
|
||||
} catch (err) {
|
||||
if (err.name === 'SequelizeConnectionRefusedError') throw err;
|
||||
|
|
|
@ -36,6 +36,7 @@ import sequenceNumber from './config/sequenceNumber.js';
|
|||
// Supply Line Models
|
||||
import supplyLine from './supplyLine/supplyLine.js';
|
||||
import volumePrices from './supplyLine/volumePrices.js';
|
||||
import clockPresalesSupply from './supplyLine/clockPresalesSupply.js';
|
||||
|
||||
// Organization Models
|
||||
import organization from './organization/organization.js';
|
||||
|
@ -69,6 +70,7 @@ let models = {
|
|||
seasonalPeriod: seasonalPeriod(sequelize),
|
||||
supplyLine: supplyLine(sequelize),
|
||||
volumePrices: volumePrices(sequelize),
|
||||
clockPresalesSupply: clockPresalesSupply(sequelize),
|
||||
package: packageModel(sequelize),
|
||||
};
|
||||
|
||||
|
@ -137,6 +139,18 @@ try {
|
|||
foreignKey: 'organizationId',
|
||||
targetKey: 'organizationId',
|
||||
});
|
||||
models.clockPresalesSupply.belongsTo(models.supplyLine, {
|
||||
foreignKey: 'supplyLineId',
|
||||
targetKey: 'supplyLineId',
|
||||
});
|
||||
models.clockPresalesSupply.belongsTo(models.tradeItem, {
|
||||
foreignKey: 'tradeItemId',
|
||||
targetKey: 'tradeItemId',
|
||||
});
|
||||
models.clockPresalesSupply.belongsTo(models.organization, {
|
||||
foreignKey: 'organizationId',
|
||||
targetKey: 'organizationId',
|
||||
});
|
||||
} catch (err) {
|
||||
criticalError(err);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
import { Sequelize } from 'sequelize';
|
||||
|
||||
const clockPresalesSupply = {
|
||||
supplyLineId: {
|
||||
type: Sequelize.STRING,
|
||||
primaryKey: true,
|
||||
},
|
||||
status: {
|
||||
type: Sequelize.STRING,
|
||||
},
|
||||
tradeItemId: {
|
||||
type: Sequelize.STRING,
|
||||
},
|
||||
pricePerPiece_currency: {
|
||||
type: Sequelize.STRING,
|
||||
},
|
||||
pricePerPiece_value: {
|
||||
type: Sequelize.DECIMAL(10,2),
|
||||
},
|
||||
deliveryNoteReference: {
|
||||
type: Sequelize.STRING,
|
||||
},
|
||||
numberOfPieces: {
|
||||
type: Sequelize.INTEGER,
|
||||
},
|
||||
packingConfigurations: {
|
||||
type: Sequelize.JSON,
|
||||
},
|
||||
tradePeriod_startDateTime: {
|
||||
type: Sequelize.DATE,
|
||||
},
|
||||
tradePeriod_endDateTime: {
|
||||
type: Sequelize.DATE,
|
||||
},
|
||||
organizationId: {
|
||||
type: Sequelize.STRING,
|
||||
},
|
||||
tradeInstrument: {
|
||||
type: Sequelize.STRING,
|
||||
},
|
||||
salesChannel: {
|
||||
type: Sequelize.STRING,
|
||||
},
|
||||
sequenceNumber: {
|
||||
type: Sequelize.INTEGER,
|
||||
},
|
||||
creationDateTime: {
|
||||
type: Sequelize.DATE,
|
||||
},
|
||||
lastModifiedDateTime: {
|
||||
type: Sequelize.DATE,
|
||||
},
|
||||
};
|
||||
|
||||
export default (sequelize) => {
|
||||
return sequelize.define(
|
||||
'supplyLine_clockPresalesSupply',
|
||||
clockPresalesSupply, {
|
||||
timestamps: false,
|
||||
freezeTableName: true,
|
||||
}
|
||||
);
|
||||
};
|
78
utils.js
78
utils.js
|
@ -1,4 +1,5 @@
|
|||
import { models } from './models/sequelize.js';
|
||||
import { Op } from 'sequelize';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import axios from 'axios';
|
||||
import moment from 'moment';
|
||||
|
@ -323,6 +324,58 @@ export async function syncSupplyLines() {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync the lock presales supply for supply lines of today.
|
||||
*/
|
||||
export async function syncClockPresalesSupply() {
|
||||
const spinner = ora(`Syncing clock presales supply...`).start();
|
||||
const supplyLines = await models.supplyLine.findAll({
|
||||
attributes: ['supplyLineId'],
|
||||
where: {
|
||||
lastSync: {
|
||||
[Op.between]: [moment().startOf('day').toDate(), moment().endOf('day').toDate()]
|
||||
}
|
||||
}
|
||||
});
|
||||
let i = 0, x = 1;
|
||||
for (let supplyLine of supplyLines) {
|
||||
spinner.text = `Syncing ${i} clock presales supplies of [${x++}|${supplyLines.length}] supply lines...`
|
||||
try {
|
||||
let response = await vnRequest('GET', `${env.API_URL}/auction/clock-presales-supply/${supplyLine.supplyLineId}`, null, null, spinner)
|
||||
|
||||
if (response.response.status == 404) continue; // Not Found
|
||||
const clockPresalesSupplies = response.data;
|
||||
|
||||
for (let clockPresalesSupply of clockPresalesSupplies) {
|
||||
// Check if the organization exists, and if it doesn't, create it
|
||||
let organization = await models.organization.findOne({
|
||||
where: { organizationId: clockPresalesSupply.supplierOrganizationId }
|
||||
});
|
||||
if (!organization) {
|
||||
let organization = (await vnRequest('GET', `${env.API_URL}/organizations/${clockPresalesSupply.supplierOrganizationId}`, null, null, spinner)).data;
|
||||
await insertOrganization(organization);
|
||||
}
|
||||
|
||||
// Check if the trade item exists, and if it doesn't, create it
|
||||
let tradeItem = await models.tradeItem.findOne({
|
||||
where: { tradeItemId: clockPresalesSupply.tradeItemId }
|
||||
});
|
||||
if (!tradeItem) {
|
||||
let tradeItem = (await vnRequest('GET', `${env.API_URL}/trade-items/${clockPresalesSupply.tradeItemId}`, null, null, spinner)).data;
|
||||
await insertItem(tradeItem);
|
||||
}
|
||||
|
||||
await insertClockPresalesSupply(clockPresalesSupply);
|
||||
spinner.text = `Syncing ${i++} clock presales supplies of [${x}|${supplyLines.length}] supply lines...`
|
||||
};
|
||||
} catch (err) {
|
||||
spinner.fail();
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
spinner.succeed()
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert trade item and dependences in the database.
|
||||
*
|
||||
|
@ -415,6 +468,27 @@ export async function insertItem(tradeItem) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert clock presales supply in the database.
|
||||
*
|
||||
* @param {Array} clockPresalesSupply
|
||||
*/
|
||||
export async function insertClockPresalesSupply(clockPresalesSupply) {
|
||||
const tx = await models.sequelize.transaction();
|
||||
try {
|
||||
await models.clockPresalesSupply.upsert({
|
||||
...clockPresalesSupply,
|
||||
pricePerPiece_currency: clockPresalesSupply.pricePerPiece.currency,
|
||||
pricePerPiece_value: clockPresalesSupply.pricePerPiece.value,
|
||||
organizationId: clockPresalesSupply.supplierOrganizationId,
|
||||
});
|
||||
await tx.commit();
|
||||
} catch (err) {
|
||||
await tx.rollback();
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert warehouse in the database.
|
||||
*
|
||||
|
@ -561,6 +635,8 @@ export async function vnRequest(method, url, data, headers, spinner) {
|
|||
case 'ECONNREFUSED':
|
||||
case 'ERR_BAD_REQUEST':
|
||||
switch (err.response.status) {
|
||||
case 404: // Not found
|
||||
return err;
|
||||
case 504:
|
||||
case 502:
|
||||
if (spinner) spinner.warn();
|
||||
|
@ -571,7 +647,7 @@ export async function vnRequest(method, url, data, headers, spinner) {
|
|||
case 429: // Too Many Requests
|
||||
if (spinner) spinner.warn();
|
||||
warning(err);
|
||||
await sleep(30000);
|
||||
await sleep(60000);
|
||||
if (spinner) spinner.start();
|
||||
break;
|
||||
case 401: // Unauthorized
|
||||
|
|
Loading…
Reference in New Issue