Merge pull request 'refactor-syncsuppliers' (#1) from refactor-syncsuppliers into master

Reviewed-on: pau/Floriday#1
This commit is contained in:
Pau 2023-02-07 13:46:21 +00:00
commit 6c9cf2643e
3 changed files with 53 additions and 125 deletions

View File

@ -17,7 +17,7 @@ let tokenExpirationDate = await vnUtils.getClientToken(models);
process.env.SYNC_SEQUENCE ? await vnUtils.syncSequence() : null; process.env.SYNC_SEQUENCE ? await vnUtils.syncSequence() : null;
process.env.SYNC_SUPPLIER ? await vnUtils.syncSuppliers() : 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 { try {
// eslint-disable-next-line no-constant-condition // eslint-disable-next-line no-constant-condition

View File

@ -6,6 +6,4 @@ let floraholland = [
8718288004970, 8718288004970,
]; ];
let others = [];
export default {floraholland}; export default {floraholland};

168
utils.js
View File

@ -182,11 +182,29 @@ async function syncSequence(current = 0, model = null ,maximumSequenceNumber = 0
try { try {
await models.sequenceNumber.upsert({ let sequence = await models.sequenceNumber.findOrCreate({
where: {
model: model
},
defaults: {
model: model, model: model,
sequenceNumber: current, sequenceNumber: current,
maximumSequenceNumber: maximumSequenceNumber maximumSequenceNumber: maximumSequenceNumber
},{ transaction: tx }); },
transaction: tx
});
if (sequence[1] == false){
await models.sequenceNumber.update({
sequenceNumber: current,
maximumSequenceNumber: maximumSequenceNumber
}, {
where: {
model: model
},
transaction: tx
});
}
await tx.commit(); await tx.commit();
@ -205,33 +223,11 @@ async function syncSuppliers(){
'X-Api-Key': process.env.API_KEY 'X-Api-Key': process.env.API_KEY
}; };
let rFloraHolland = suppliersGln.floraholland; let maximumSequenceNumber = 500;
let queryConnections = `${BASE_CUSTOMER_URL}connections`; for (let i = 0; i < maximumSequenceNumber; i++) {
let responseConnections = await fetch(queryConnections, {
method: 'GET',
headers: headers
});
let dataConnections = await responseConnections.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 query = `${BASE_CUSTOMER_URL}organizations/sync/${i}?organizationType=SUPPLIER&limit=500`;
let response = await fetch(query, { let response = await fetch(query, {
method: 'GET', method: 'GET',
headers: headers headers: headers
@ -239,101 +235,35 @@ async function syncSuppliers(){
let data = await response.json(); let data = await response.json();
if (response.status === 200) { maximumSequenceNumber = data.maximumSequenceNumber;
console.log('Supplier request successful'); let suppliers = data.results;
let connection = await models.connections.findOne({ for (let supplier of suppliers) {
where: { i = supplier.sequenceNumber;
organizationId: data.organizationId, await models.suppliers.upsert({
connect: true isConnected: false,
} commercialName: supplier.commercialName,
email: supplier.email,
phone: supplier.phone,
website: supplier.website,
mailingAddress: supplier.mailingAddress,
physicalAddress: supplier.physicalAddress,
pythosanitaryNumber: supplier.pythosanitaryNumber,
sequenceNumber: supplier.sequenceNumber,
organizationId: supplier.organizationId,
companyGln: supplier.companyGln,
name: supplier.name,
endDate: supplier.endDate,
rfhRelationId: supplier.rfhRelationId,
organizationType: supplier.organizationType,
paymentProviders: `${supplier.paymentProviders}`,
}); });
console.log('INSERTED:\t', supplier.commercialName, '\nsequenceNumber:\t', supplier.sequenceNumber);
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);
} }
await syncSequence(i, 'suppliers', maximumSequenceNumber);
console.log(data.maximumSequenceNumber);
console.log(data.results.length);
console.log(i);
} }
} }