refactor-syncsuppliers #1

Merged
pau merged 3 commits from refactor-syncsuppliers into master 2023-02-07 13:46:21 +00:00
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_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

View File

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

174
utils.js
View File

@ -182,11 +182,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();
@ -204,34 +222,12 @@ async function syncSuppliers(){
'Authorization': `Bearer ${await getJWT()}`,
'X-Api-Key': process.env.API_KEY
};
let rFloraHolland = suppliersGln.floraholland;
let queryConnections = `${BASE_CUSTOMER_URL}connections`;
let maximumSequenceNumber = 500;
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}`;
for (let i = 0; i < maximumSequenceNumber; i++) {
let query = `${BASE_CUSTOMER_URL}organizations/sync/${i}?organizationType=SUPPLIER&limit=500`;
let response = await fetch(query, {
method: 'GET',
headers: headers
@ -239,101 +235,35 @@ async function syncSuppliers(){
let data = await response.json();
if (response.status === 200) {
console.log('Supplier request successful');
maximumSequenceNumber = data.maximumSequenceNumber;
let suppliers = data.results;
let connection = await models.connections.findOne({
where: {
organizationId: data.organizationId,
connect: true
}
for (let supplier of suppliers) {
i = supplier.sequenceNumber;
await models.suppliers.upsert({
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}`,
});
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('INSERTED:\t', supplier.commercialName, '\nsequenceNumber:\t', supplier.sequenceNumber);
}
await syncSequence(i, 'suppliers', maximumSequenceNumber);
console.log(data.maximumSequenceNumber);
console.log(data.results.length);
console.log(i);
}
}