Merge pull request 'refactor-syncsuppliers' (#1) from refactor-syncsuppliers into master
Reviewed-on: pau/Floriday#1
This commit is contained in:
commit
6c9cf2643e
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_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
|
||||||
|
|
|
@ -6,6 +6,4 @@ let floraholland = [
|
||||||
8718288004970,
|
8718288004970,
|
||||||
];
|
];
|
||||||
|
|
||||||
let others = [];
|
|
||||||
|
|
||||||
export default {floraholland};
|
export default {floraholland};
|
174
utils.js
174
utils.js
|
@ -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({
|
||||||
model: model,
|
where: {
|
||||||
sequenceNumber: current,
|
model: model
|
||||||
maximumSequenceNumber: maximumSequenceNumber
|
},
|
||||||
},{ transaction: tx });
|
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();
|
await tx.commit();
|
||||||
|
|
||||||
|
@ -204,34 +222,12 @@ async function syncSuppliers(){
|
||||||
'Authorization': `Bearer ${await getJWT()}`,
|
'Authorization': `Bearer ${await getJWT()}`,
|
||||||
'X-Api-Key': process.env.API_KEY
|
'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, {
|
for (let i = 0; i < maximumSequenceNumber; i++) {
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue