refs #4823 Now save the maxSequenceNumber of orgs
This commit is contained in:
parent
0d8fde61bd
commit
acebc9a89f
|
@ -0,0 +1,22 @@
|
||||||
|
import { Sequelize } from 'sequelize';
|
||||||
|
|
||||||
|
const sequenceNumber = {
|
||||||
|
model: {
|
||||||
|
type: Sequelize.STRING,
|
||||||
|
primaryKey: true,
|
||||||
|
},
|
||||||
|
maxSequenceNumber: {
|
||||||
|
type: Sequelize.INTEGER,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default (sequelize) => {
|
||||||
|
return sequelize.define(
|
||||||
|
'sequenceNumber',
|
||||||
|
sequenceNumber,
|
||||||
|
{
|
||||||
|
timestamps: false,
|
||||||
|
freezeTableName: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
|
@ -29,13 +29,14 @@ try {
|
||||||
criticalError(err);
|
criticalError(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Conf Models
|
||||||
|
import clientConfig from './config/clientConfig.js';
|
||||||
|
import sequenceNumber from './config/sequenceNumber.js';
|
||||||
|
|
||||||
// Supply Line Models
|
// Supply Line Models
|
||||||
import supplyLine from './supplyLine/supplyLine.js';
|
import supplyLine from './supplyLine/supplyLine.js';
|
||||||
import volumePrices from './supplyLine/volumePrices.js';
|
import volumePrices from './supplyLine/volumePrices.js';
|
||||||
|
|
||||||
// Conf Models
|
|
||||||
import clientConfig from './config/clientConfig.js';
|
|
||||||
|
|
||||||
// Organization Models
|
// Organization Models
|
||||||
import organization from './organization/organization.js';
|
import organization from './organization/organization.js';
|
||||||
import warehouses from './organization/warehouses.js';
|
import warehouses from './organization/warehouses.js';
|
||||||
|
@ -55,6 +56,7 @@ import characteristics from './tradeItem/characteristics.js';
|
||||||
*/
|
*/
|
||||||
let models = {
|
let models = {
|
||||||
sequelize: sequelize,
|
sequelize: sequelize,
|
||||||
|
sequenceNumber: sequenceNumber(sequelize),
|
||||||
clientConfig: clientConfig(sequelize),
|
clientConfig: clientConfig(sequelize),
|
||||||
organization: organization(sequelize),
|
organization: organization(sequelize),
|
||||||
warehouses: warehouses(sequelize),
|
warehouses: warehouses(sequelize),
|
||||||
|
|
54
utils.js
54
utils.js
|
@ -34,7 +34,7 @@ export async function requestToken(isForce = false) {
|
||||||
scope: 'role:app catalog:read supply:read organization:read network:write network:read'
|
scope: 'role:app catalog:read supply:read organization:read network:write network:read'
|
||||||
}).toString();
|
}).toString();
|
||||||
const headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
|
const headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
|
||||||
const response = (await vnRequest('POST', env.API_ENDPOINT, data, headers)).data;
|
const response = (await vnRequest('POST', env.API_ENDPOINT, data, headers, spinner)).data;
|
||||||
|
|
||||||
const tokenExpiration = moment()
|
const tokenExpiration = moment()
|
||||||
.add(response.expires_in, 's')
|
.add(response.expires_in, 's')
|
||||||
|
@ -127,14 +127,20 @@ export async function sleep(ms) {
|
||||||
*/
|
*/
|
||||||
export async function syncOrganizations(){
|
export async function syncOrganizations(){
|
||||||
let spinner = ora('Syncing organizations...').start();
|
let spinner = ora('Syncing organizations...').start();
|
||||||
let i = 1;
|
let i = 0;
|
||||||
try {
|
try {
|
||||||
const maxSequenceNumber = (await vnRequest('GET', `${env.API_URL}/organizations/current-max-sequence`)).data;
|
const sequenceNumberDb = await models.sequenceNumber.findOne({
|
||||||
for (let curSequenceNumber = 0; curSequenceNumber <= maxSequenceNumber; curSequenceNumber++) {
|
where: { model: 'organization' }
|
||||||
|
});
|
||||||
|
const maxSequenceNumber = (await vnRequest('GET', `${env.API_URL}/organizations/current-max-sequence`, null, null, spinner)).data;
|
||||||
|
let curSequenceNumber = (sequenceNumberDb?.maxSequenceNumber)
|
||||||
|
? sequenceNumberDb.maxSequenceNumber
|
||||||
|
: 0
|
||||||
|
for (curSequenceNumber; curSequenceNumber <= maxSequenceNumber; curSequenceNumber++) {
|
||||||
const params = new URLSearchParams({
|
const params = new URLSearchParams({
|
||||||
organizationType: 'SUPPLIER'
|
organizationType: 'SUPPLIER'
|
||||||
}).toString();
|
}).toString();
|
||||||
let response = (await vnRequest('GET', `${env.API_URL}/organizations/sync/${curSequenceNumber}?${params}`)).data;
|
let response = (await vnRequest('GET', `${env.API_URL}/organizations/sync/${curSequenceNumber}?${params}`, null, null, spinner)).data;
|
||||||
curSequenceNumber = response.maximumSequenceNumber;
|
curSequenceNumber = response.maximumSequenceNumber;
|
||||||
const orgs = response.results;
|
const orgs = response.results;
|
||||||
for (let org of orgs) {
|
for (let org of orgs) {
|
||||||
|
@ -145,6 +151,11 @@ export async function syncOrganizations(){
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
await models.sequenceNumber.upsert({
|
||||||
|
model: 'organization',
|
||||||
|
maxSequenceNumber,
|
||||||
|
});
|
||||||
|
spinner.text = `Syncing ${i} organizations, 0 missing...`
|
||||||
spinner.succeed();
|
spinner.succeed();
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
|
@ -172,7 +183,7 @@ export async function syncConnections(){
|
||||||
group: ['organizationId'],
|
group: ['organizationId'],
|
||||||
});
|
});
|
||||||
|
|
||||||
const connectionsInFloriday = (await vnRequest('GET', `${env.API_URL}/connections`)).data;
|
const connectionsInFloriday = (await vnRequest('GET', `${env.API_URL}/connections`, null, null, spinner)).data;
|
||||||
|
|
||||||
let isExists = false, connectionsToPut = [];
|
let isExists = false, connectionsToPut = [];
|
||||||
for (let connectionInDb of connectionsInDb) {
|
for (let connectionInDb of connectionsInDb) {
|
||||||
|
@ -190,7 +201,7 @@ export async function syncConnections(){
|
||||||
let i = 1;
|
let i = 1;
|
||||||
for (let connection of connectionsToPut) {
|
for (let connection of connectionsToPut) {
|
||||||
spinner.text = `Creating ${i++} of ${connectionsToPut.length} connections in Floriday...`
|
spinner.text = `Creating ${i++} of ${connectionsToPut.length} connections in Floriday...`
|
||||||
await vnRequest('PUT', `${env.API_URL}/connections/${connection}`);
|
await vnRequest('PUT', `${env.API_URL}/connections/${connection}`, null, null, spinner);
|
||||||
}
|
}
|
||||||
if (spinner) spinner.succeed();
|
if (spinner) spinner.succeed();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -214,7 +225,7 @@ export async function syncTradeItems(){
|
||||||
const params = new URLSearchParams({
|
const params = new URLSearchParams({
|
||||||
supplierOrganizationId: org.organizationId,
|
supplierOrganizationId: org.organizationId,
|
||||||
}).toString();
|
}).toString();
|
||||||
let tradeItems = (await vnRequest('GET', `${env.API_URL}/trade-items?${params}`)).data
|
let tradeItems = (await vnRequest('GET', `${env.API_URL}/trade-items?${params}`, null, null, spinner)).data
|
||||||
|
|
||||||
spinner.text = `Syncing ${i} trade items of [${x++}|${orgs.length}] organizations...`
|
spinner.text = `Syncing ${i} trade items of [${x++}|${orgs.length}] organizations...`
|
||||||
if (!tradeItems.length) continue;
|
if (!tradeItems.length) continue;
|
||||||
|
@ -250,7 +261,7 @@ export async function syncSupplyLines() {
|
||||||
const params = new URLSearchParams({
|
const params = new URLSearchParams({
|
||||||
supplierOrganizationId: org.organizationId,
|
supplierOrganizationId: org.organizationId,
|
||||||
}).toString();
|
}).toString();
|
||||||
let supplyLines = (await vnRequest('GET',`${env.API_URL}/supply-lines?${params}`)).data;
|
let supplyLines = (await vnRequest('GET',`${env.API_URL}/supply-lines?${params}`, null, null, spinner)).data;
|
||||||
if (!supplyLines.length) continue
|
if (!supplyLines.length) continue
|
||||||
|
|
||||||
for (let supplyLine of supplyLines) {
|
for (let supplyLine of supplyLines) {
|
||||||
|
@ -259,14 +270,14 @@ export async function syncSupplyLines() {
|
||||||
where: { warehouseId: supplyLine.warehouseId }
|
where: { warehouseId: supplyLine.warehouseId }
|
||||||
});
|
});
|
||||||
if (!warehouse) {
|
if (!warehouse) {
|
||||||
let warehouse = (await vnRequest('GET', `${env.API_URL}/warehouses/${supplyLine.warehouseId}`)).data;
|
let warehouse = (await vnRequest('GET', `${env.API_URL}/warehouses/${supplyLine.warehouseId}`, null, null, spinner)).data;
|
||||||
|
|
||||||
// Check if the organization exists, and if it doesn't, create it
|
// Check if the organization exists, and if it doesn't, create it
|
||||||
let organization = await models.organization.findOne({
|
let organization = await models.organization.findOne({
|
||||||
where: { organizationId: warehouse.organizationId }
|
where: { organizationId: warehouse.organizationId }
|
||||||
});
|
});
|
||||||
if (!organization) {
|
if (!organization) {
|
||||||
let organization = (await vnRequest('GET', `${env.API_URL}/organizations/${warehouse.organizationId}`)).data;
|
let organization = (await vnRequest('GET', `${env.API_URL}/organizations/${warehouse.organizationId}`, null, null, spinner)).data;
|
||||||
await insertOrganization(organization);
|
await insertOrganization(organization);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,7 +289,7 @@ export async function syncSupplyLines() {
|
||||||
where: { tradeItemId: supplyLine.tradeItemId }
|
where: { tradeItemId: supplyLine.tradeItemId }
|
||||||
});
|
});
|
||||||
if (!tradeItem) {
|
if (!tradeItem) {
|
||||||
let tradeItem = (await vnRequest('GET', `${env.API_URL}/trade-items/${supplyLine.tradeItemId}`)).data;
|
let tradeItem = (await vnRequest('GET', `${env.API_URL}/trade-items/${supplyLine.tradeItemId}`, null, null, spinner)).data;
|
||||||
await insertItem(tradeItem);
|
await insertItem(tradeItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -462,7 +473,7 @@ export async function syncWarehouses(){
|
||||||
let x = 0, i = 0;
|
let x = 0, i = 0;
|
||||||
for (let org of orgs) {
|
for (let org of orgs) {
|
||||||
spinner.text = `Syncing ${i} warehouses of [${x++}|${orgs.length}] organizations...`
|
spinner.text = `Syncing ${i} warehouses of [${x++}|${orgs.length}] organizations...`
|
||||||
const warehouses = (await vnRequest('GET', `${env.API_URL}/organizations/supplier/${org.organizationId}/warehouses`)).data;
|
const warehouses = (await vnRequest('GET', `${env.API_URL}/organizations/supplier/${org.organizationId}/warehouses`, null, null, spinner)).data;
|
||||||
for (let warehouse of warehouses) {
|
for (let warehouse of warehouses) {
|
||||||
spinner.text = `Syncing ${i++} warehouses of [${x}|${orgs.length}] organizations...`
|
spinner.text = `Syncing ${i++} warehouses of [${x}|${orgs.length}] organizations...`
|
||||||
await insertWarehouse(warehouse);
|
await insertWarehouse(warehouse);
|
||||||
|
@ -483,7 +494,7 @@ export async function deleteConnections() {
|
||||||
let spinner;
|
let spinner;
|
||||||
try {
|
try {
|
||||||
let i = 1;
|
let i = 1;
|
||||||
const connectionsInFloriday = (await vnRequest('GET', `${env.API_URL}/connections`)).data;
|
const connectionsInFloriday = (await vnRequest('GET', `${env.API_URL}/connections`, null, null, spinner)).data;
|
||||||
const connectionsInDb = await models.organization.findAll({
|
const connectionsInDb = await models.organization.findAll({
|
||||||
attributes: ['organizationId'],
|
attributes: ['organizationId'],
|
||||||
where: { isConnected: true }
|
where: { isConnected: true }
|
||||||
|
@ -503,7 +514,7 @@ export async function deleteConnections() {
|
||||||
if (ghostConnections.length) spinner = ora(`Deleting connections that aren't in the db...`).start();
|
if (ghostConnections.length) spinner = ora(`Deleting connections that aren't in the db...`).start();
|
||||||
|
|
||||||
for (let connection of ghostConnections) {
|
for (let connection of ghostConnections) {
|
||||||
await vnRequest('DELETE', `${env.API_URL}/connections/${connection}`);
|
await vnRequest('DELETE', `${env.API_URL}/connections/${connection}`, null, null, spinner);
|
||||||
spinner.text = `Deleting ${i++} of ${ghostConnections.length} that aren't in the db...`
|
spinner.text = `Deleting ${i++} of ${ghostConnections.length} that aren't in the db...`
|
||||||
}
|
}
|
||||||
if (spinner) spinner.succeed();
|
if (spinner) spinner.succeed();
|
||||||
|
@ -520,10 +531,11 @@ export async function deleteConnections() {
|
||||||
* @param {String} method
|
* @param {String} method
|
||||||
* @param {Array} body
|
* @param {Array} body
|
||||||
* @param {Array} header
|
* @param {Array} header
|
||||||
|
* @param {Array} spinner
|
||||||
*
|
*
|
||||||
* @return {Array}
|
* @return {Array}
|
||||||
**/
|
**/
|
||||||
export async function vnRequest(method, url, data, headers) {
|
export async function vnRequest(method, url, data, headers, spinner) {
|
||||||
if (!headers)
|
if (!headers)
|
||||||
headers = {
|
headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
@ -549,29 +561,39 @@ export async function vnRequest(method, url, data, headers) {
|
||||||
switch (err.response.status) {
|
switch (err.response.status) {
|
||||||
case 504:
|
case 504:
|
||||||
case 502:
|
case 502:
|
||||||
|
if (spinner) spinner.warn();
|
||||||
warning(err);
|
warning(err);
|
||||||
await sleep(1000);
|
await sleep(1000);
|
||||||
|
if (spinner) spinner.start();
|
||||||
break;
|
break;
|
||||||
case 429: // Too Many Requests
|
case 429: // Too Many Requests
|
||||||
|
if (spinner) spinner.warn();
|
||||||
warning(err);
|
warning(err);
|
||||||
await sleep(3400); // Stipulated by Floriday
|
await sleep(3400); // Stipulated by Floriday
|
||||||
|
if (spinner) spinner.start();
|
||||||
break;
|
break;
|
||||||
case 401: // Unauthorized
|
case 401: // Unauthorized
|
||||||
|
if (spinner) spinner.warn();
|
||||||
warning(err);
|
warning(err);
|
||||||
await requestToken(true);
|
await requestToken(true);
|
||||||
headers.Authorization
|
headers.Authorization
|
||||||
? headers.Authorization = `Bearer ${await getCurrentToken()}`
|
? headers.Authorization = `Bearer ${await getCurrentToken()}`
|
||||||
: criticalError(err);
|
: criticalError(err);
|
||||||
|
if (spinner) spinner.start();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
if (spinner) spinner.warn();
|
||||||
warning(err);
|
warning(err);
|
||||||
await sleep(env.MS_RETRY_UNHANDLED_ERROR);
|
await sleep(env.MS_RETRY_UNHANDLED_ERROR);
|
||||||
|
if (spinner) spinner.start();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
if (spinner) spinner.warn();
|
||||||
warning(err);
|
warning(err);
|
||||||
await sleep(env.MS_RETRY_UNHANDLED_ERROR);
|
await sleep(env.MS_RETRY_UNHANDLED_ERROR);
|
||||||
|
if (spinner) spinner.start();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue