refs #4823 Now deletes ghost connections
This commit is contained in:
parent
888bbb024f
commit
2f2428795f
|
@ -14,7 +14,7 @@ class Floriday {
|
|||
if (JSON.parse(env.SYNC_SEQUENCE)) await utils.syncSequence();
|
||||
if (JSON.parse(env.SYNC_SUPPLIER)) await utils.syncSuppliers();
|
||||
if (JSON.parse(env.SYNC_WAREHOUSE)) await utils.syncWarehouses();
|
||||
if (JSON.parse(env.SYNC_CONN)) await utils.syncConn();
|
||||
if (JSON.parse(env.SYNC_CONN)) await utils.syncConnections();
|
||||
if (JSON.parse(env.SYNC_TRADEITEM)) await utils.syncTradeItems();
|
||||
} catch (err) {
|
||||
utils.criticalError(err);
|
||||
|
@ -66,7 +66,6 @@ class Floriday {
|
|||
|
||||
async stop() {
|
||||
this.continueSchedule = false;
|
||||
await utils.deleteConnections();
|
||||
await closeConn();
|
||||
console.warn(chalk.dim('Bye, come back soon 👋'))
|
||||
}
|
||||
|
|
76
utils.js
76
utils.js
|
@ -245,9 +245,7 @@ export async function syncSequence(current = 0, model = null , maximumSequenceNu
|
|||
transaction: tx
|
||||
});
|
||||
}
|
||||
|
||||
await tx.commit();
|
||||
|
||||
} catch (error) {
|
||||
await tx.rollback();
|
||||
console.log(`Error while syncing sequence number for: ${model}: ${error}`);
|
||||
|
@ -302,7 +300,11 @@ export async function syncSuppliers(){
|
|||
}
|
||||
}
|
||||
|
||||
export async function syncConn(){
|
||||
/**
|
||||
* Sync the connections in Floriday
|
||||
*/
|
||||
export async function syncConnections(){
|
||||
await deleteConnections();
|
||||
const spinner = ora(`Creating connections...`).start();
|
||||
try {
|
||||
let connections = await models.connection.findAll();
|
||||
|
@ -310,41 +312,29 @@ export async function syncConn(){
|
|||
let headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${await getCurrentToken()}`,
|
||||
'X-Api-Key': process.env.API_KEY
|
||||
'X-Api-Key': process.env.API_KEY,
|
||||
};
|
||||
|
||||
const remoteConnections = (await vnRequest('GET', `${env.API_URL}/connections`, null, headers)).data;
|
||||
|
||||
let i = 1;
|
||||
for (let connection of connections){
|
||||
for (let connection of connections) {
|
||||
spinner.text = `Creating ${i++} of ${connections.length} connections...`
|
||||
let remoteConnection = remoteConnections.find(remoteConnection => remoteConnection == connection.supplierOrganizationId);
|
||||
|
||||
if (!remoteConnection){
|
||||
|
||||
if (!remoteConnection)
|
||||
await vnRequest('PUT', `${env.API_URL}/connections/${connection.supplierOrganizationId}`, null, headers);
|
||||
|
||||
await models.connection.update({ isConnected: true }, {
|
||||
where: {
|
||||
supplierOrganizationId: connection.supplierOrganizationId
|
||||
}
|
||||
});
|
||||
await models.supplier.update({ isConnected: true }, {
|
||||
where: {
|
||||
supplierOrganizationId: connection.supplierOrganizationId
|
||||
}
|
||||
});
|
||||
} else {
|
||||
await models.connection.update({ isConnected: true }, {
|
||||
where: {
|
||||
supplierOrganizationId: connection.supplierOrganizationId
|
||||
}
|
||||
});
|
||||
await models.supplier.update({ isConnected: true }, {
|
||||
where: {
|
||||
supplierOrganizationId: connection.supplierOrganizationId
|
||||
}
|
||||
});
|
||||
}
|
||||
await models.connection.update({isConnected: true }, {
|
||||
where: {
|
||||
supplierOrganizationId: connection.supplierOrganizationId
|
||||
}
|
||||
});
|
||||
await models.supplier.update({ isConnected: true }, {
|
||||
where: {
|
||||
supplierOrganizationId: connection.supplierOrganizationId
|
||||
}
|
||||
});
|
||||
}
|
||||
spinner.succeed();
|
||||
} catch (err) {
|
||||
|
@ -719,26 +709,40 @@ export async function syncWarehouses(){
|
|||
}
|
||||
|
||||
/**
|
||||
* Deletes the connections in Floriday
|
||||
* Removes Floriday connections that we don't have in the database
|
||||
**/
|
||||
export async function deleteConnections() {
|
||||
const spinner = ora(`Deleting connections...`).start();
|
||||
const spinner = ora(`Deleting connections that aren't in the db...`).start();
|
||||
try {
|
||||
let i = 1;
|
||||
let headers = {
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${await getCurrentToken()}`,
|
||||
'X-Api-Key': process.env.API_KEY
|
||||
};
|
||||
let connections = (await vnRequest('GET', `${env.API_URL}/connections`, null, headers)).data;
|
||||
for (let connection of connections) {
|
||||
|
||||
const connectionsInFloriday = (await vnRequest('GET', `${env.API_URL}/connections`, null, headers)).data;
|
||||
const connectionsInDb = await models.connection.findAll();
|
||||
|
||||
let isExists = false, ghostConnections = [];
|
||||
for (let connectionInFloriday of connectionsInFloriday) {
|
||||
for (let connectionInDb of connectionsInDb)
|
||||
if (connectionInFloriday == connectionInDb.supplierOrganizationId) {
|
||||
isExists = true;
|
||||
break;
|
||||
}
|
||||
if (!isExists) ghostConnections.push(connectionInFloriday)
|
||||
isExists = false;
|
||||
}
|
||||
|
||||
for (let connection of ghostConnections) {
|
||||
await vnRequest('DELETE', `${env.API_URL}/connections/${connection}`, null, headers);
|
||||
spinner.text = `Deleting ${i++} connections...`
|
||||
spinner.text = `Deleting ${i++} of ${ghostConnections.length} that aren't in the db...`
|
||||
}
|
||||
spinner.succeed();
|
||||
} catch (err) {
|
||||
spinner.fail();
|
||||
util.criticalError(err);
|
||||
criticalError(err);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue