refactor-syncsuppliers #1
2
index.js
2
index.js
|
@ -17,7 +17,7 @@ let tokenExpirationDate = await vnUtils.getClientToken(models);
|
|||
|
||||
process.env.SYNC_SEQUENCE ? await vnUtils.syncSequence() : null;
|
||||
process.env.SYNC_SUPPLIER ? await vnUtils.syncSuppliers() : null;
|
||||
process.env.SYNC_TRADEITEM ? await vnUtils.syncTradeItems() : null;
|
||||
//process.env.SYNC_TRADEITEM ? await vnUtils.syncTradeItems() : null;
|
||||
|
||||
try {
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
|
|
155
utils.js
155
utils.js
|
@ -186,11 +186,29 @@ async function syncSequence(current = 0, model = null ,maximumSequenceNumber = 0
|
|||
|
||||
try {
|
||||
|
||||
await models.sequenceNumber.upsert({
|
||||
model: model,
|
||||
sequenceNumber: current,
|
||||
maximumSequenceNumber: maximumSequenceNumber
|
||||
},{ transaction: tx });
|
||||
let sequence = await models.sequenceNumber.findOrCreate({
|
||||
where: {
|
||||
model: model
|
||||
},
|
||||
defaults: {
|
||||
model: model,
|
||||
sequenceNumber: current,
|
||||
maximumSequenceNumber: maximumSequenceNumber
|
||||
},
|
||||
transaction: tx
|
||||
});
|
||||
|
||||
if (sequence[1] == false){
|
||||
await models.sequenceNumber.update({
|
||||
sequenceNumber: current,
|
||||
maximumSequenceNumber: maximumSequenceNumber
|
||||
}, {
|
||||
where: {
|
||||
model: model
|
||||
},
|
||||
transaction: tx
|
||||
});
|
||||
}
|
||||
|
||||
await tx.commit();
|
||||
|
||||
|
@ -211,134 +229,17 @@ async function syncSuppliers(){
|
|||
|
||||
let rFloraHolland = suppliersGln.floraholland;
|
||||
|
||||
let queryConnections = `${BASE_CUSTOMER_URL}connections`;
|
||||
let queryMaxSeqNumber = `${BASE_CUSTOMER_URL}organizations/current-max-sequence`;
|
||||
|
||||
let responseConnections = await fetch(queryConnections, {
|
||||
let responseMaxSequence = await fetch(queryMaxSeqNumber, {
|
||||
method: 'GET',
|
||||
headers: headers
|
||||
});
|
||||
|
||||
let dataConnections = await responseConnections.json();
|
||||
let dataMaxSequence = await responseMaxSequence.json();
|
||||
|
||||
for(let connection of dataConnections){
|
||||
await models.connections.findOrCreate({
|
||||
where: {
|
||||
organizationId: connection
|
||||
},
|
||||
defaults: {
|
||||
organizationId: connection,
|
||||
connect : true
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for(let producer of rFloraHolland){
|
||||
|
||||
let query = `${BASE_CUSTOMER_URL}organizations/gln/${producer}`;
|
||||
|
||||
let response = await fetch(query, {
|
||||
method: 'GET',
|
||||
headers: headers
|
||||
});
|
||||
|
||||
let data = await response.json();
|
||||
|
||||
if (response.status === 200) {
|
||||
console.log('Supplier request successful');
|
||||
|
||||
let connection = await models.connections.findOne({
|
||||
where: {
|
||||
organizationId: data.organizationId,
|
||||
connect: true
|
||||
}
|
||||
});
|
||||
|
||||
let isConnected = false;
|
||||
|
||||
|
||||
|
||||
|
||||
if (connection != null) {
|
||||
isConnected = true;
|
||||
|
||||
let where = {
|
||||
companyGln: producer,
|
||||
isConnected: isConnected
|
||||
};
|
||||
let defaults = {
|
||||
isConnected: isConnected,
|
||||
commercialName: data.commercialName,
|
||||
email: data.email,
|
||||
phone: data.phone,
|
||||
website: data.website,
|
||||
mailingAddress: data.mailingAddress,
|
||||
physicalAddress: data.physicalAddress,
|
||||
pythosanitaryNumber: data.phytosanitaryNumber,
|
||||
sequenceNumber: data.sequenceNumber,
|
||||
organizationId: data.organizationId,
|
||||
companyGln: producer,
|
||||
name: data.name,
|
||||
endDate: data.endDate,
|
||||
rfhRelationId: data.rfhRelationId,
|
||||
organizationType: data.organizationType,
|
||||
paymentProviders: JSON.stringify(data.paymentProviders),
|
||||
};
|
||||
|
||||
await models.suppliers.destroy({
|
||||
where: {
|
||||
companyGln: producer,
|
||||
isConnected: false
|
||||
}
|
||||
});
|
||||
|
||||
await models.suppliers.findOrCreate({
|
||||
where : where,
|
||||
defaults: defaults
|
||||
});
|
||||
} else {
|
||||
let where = {
|
||||
companyGln: producer,
|
||||
isConnected: isConnected
|
||||
};
|
||||
let defaults = {
|
||||
isConnected: isConnected,
|
||||
commercialName: data.commercialName,
|
||||
email: data.email,
|
||||
phone: data.phone,
|
||||
website: data.website,
|
||||
mailingAddress: data.mailingAddress,
|
||||
physicalAddress: data.physicalAddress,
|
||||
pythosanitaryNumber: data.phytosanitaryNumber,
|
||||
sequenceNumber: data.sequenceNumber,
|
||||
organizationId: data.organizationId,
|
||||
companyGln: producer,
|
||||
name: data.name,
|
||||
endDate: data.endDate,
|
||||
rfhRelationId: data.rfhRelationId,
|
||||
organizationType: data.organizationType,
|
||||
paymentProviders: JSON.stringify(data.paymentProviders),
|
||||
};
|
||||
await models.suppliers.destroy({
|
||||
where: {
|
||||
companyGln: producer,
|
||||
isConnected: true
|
||||
}
|
||||
});
|
||||
await models.suppliers.findOrCreate({
|
||||
where: where,
|
||||
defaults: defaults
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
console.log(`DATA FOR SUPPLIER: ${data.name} OK`);
|
||||
} else {
|
||||
console.log('Supplier request failed with status ', response.status);
|
||||
console.log('Supplier: ', data);
|
||||
console.log('response: ', response);
|
||||
console.log('query: ', query);
|
||||
}
|
||||
}
|
||||
console.log('Maximum sequence number: ', dataMaxSequence);
|
||||
|
||||
}
|
||||
|
||||
async function syncTradeItems(){
|
||||
|
|
Loading…
Reference in New Issue