change syncsuppliers

This commit is contained in:
Pau 2023-02-07 09:41:54 +01:00
parent 559bb6e12c
commit d00570afc7
2 changed files with 29 additions and 128 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

147
utils.js
View File

@ -186,11 +186,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();
@ -211,134 +229,17 @@ async function syncSuppliers(){
let rFloraHolland = suppliersGln.floraholland; 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', method: 'GET',
headers: headers headers: headers
}); });
let dataConnections = await responseConnections.json(); let dataMaxSequence = await responseMaxSequence.json();
for(let connection of dataConnections){ console.log('Maximum sequence number: ', dataMaxSequence);
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);
}
}
} }
async function syncTradeItems(){ async function syncTradeItems(){