From d00570afc78471e6329aff51aae74caf7fc4e291 Mon Sep 17 00:00:00 2001 From: Pau Navarro Date: Tue, 7 Feb 2023 09:41:54 +0100 Subject: [PATCH 1/2] change syncsuppliers --- index.js | 2 +- utils.js | 155 ++++++++++--------------------------------------------- 2 files changed, 29 insertions(+), 128 deletions(-) diff --git a/index.js b/index.js index 4e5cc86..1fdd4ca 100644 --- a/index.js +++ b/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 diff --git a/utils.js b/utils.js index 9f01908..0428cfe 100644 --- a/utils.js +++ b/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(){ From be0c20307b998983f9f968dc93288b1bb103833d Mon Sep 17 00:00:00 2001 From: Pau Navarro Date: Tue, 7 Feb 2023 14:38:54 +0100 Subject: [PATCH 2/2] get all the suppliers from floriday api --- suppliersGln.js | 2 -- utils.js | 49 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/suppliersGln.js b/suppliersGln.js index 4ba939c..0692031 100644 --- a/suppliersGln.js +++ b/suppliersGln.js @@ -6,6 +6,4 @@ let floraholland = [ 8718288004970, ]; -let others = []; - export default {floraholland}; \ No newline at end of file diff --git a/utils.js b/utils.js index dab88c3..76f609c 100644 --- a/utils.js +++ b/utils.js @@ -222,20 +222,49 @@ async function syncSuppliers(){ 'Authorization': `Bearer ${await getJWT()}`, 'X-Api-Key': process.env.API_KEY }; - - let rFloraHolland = suppliersGln.floraholland; - let queryMaxSeqNumber = `${BASE_CUSTOMER_URL}organizations/current-max-sequence`; + let maximumSequenceNumber = 500; - let responseMaxSequence = await fetch(queryMaxSeqNumber, { - method: 'GET', - headers: headers - }); + for (let i = 0; i < maximumSequenceNumber; i++) { - let dataMaxSequence = await responseMaxSequence.json(); + let query = `${BASE_CUSTOMER_URL}organizations/sync/${i}?organizationType=SUPPLIER&limit=500`; + let response = await fetch(query, { + method: 'GET', + headers: headers + }); - console.log('Maximum sequence number: ', dataMaxSequence); - + let data = await response.json(); + + maximumSequenceNumber = data.maximumSequenceNumber; + let suppliers = data.results; + + 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}`, + }); + 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); + } } async function syncTradeItems(){