From 5e54e2771836ba2bea98776caa5915f90192e0a0 Mon Sep 17 00:00:00 2001 From: jgallego Date: Wed, 17 Aug 2022 12:19:54 +0200 Subject: [PATCH 01/18] quito left en taxes --- print/templates/reports/invoice/sql/taxes.sql | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/print/templates/reports/invoice/sql/taxes.sql b/print/templates/reports/invoice/sql/taxes.sql index 2203d8b8a..5c04d2c74 100644 --- a/print/templates/reports/invoice/sql/taxes.sql +++ b/print/templates/reports/invoice/sql/taxes.sql @@ -7,6 +7,5 @@ SELECT JOIN pgc ON pgc.code = iot.pgcFk LEFT JOIN pgcEqu pe ON pe.equFk = pgc.code JOIN invoiceOut io ON io.id = iot.invoiceOutFk - LEFT JOIN ticket t ON t.refFk = io.ref - WHERE t.refFk = ? + WHERE io.ref = ? ORDER BY iot.id \ No newline at end of file From 011e372558ec37f07625ddd4976f6d2eb4e88112 Mon Sep 17 00:00:00 2001 From: joan Date: Tue, 6 Sep 2022 12:21:38 +0200 Subject: [PATCH 02/18] Removed wrong commit code pick --- back/methods/edi/updateData.js | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/back/methods/edi/updateData.js b/back/methods/edi/updateData.js index 3dae2d47d..121e96778 100644 --- a/back/methods/edi/updateData.js +++ b/back/methods/edi/updateData.js @@ -200,31 +200,6 @@ module.exports = Self => { const toTable = table.toTable; const baseName = table.fileName; - const firstEntry = entries[0]; - const entryName = firstEntry.entryName; - const startIndex = (entryName.length - 10); - const endIndex = (entryName.length - 4); - const dateString = entryName.substring(startIndex, endIndex); - - const lastUpdated = new Date(); - - let updated = null; - if (file.updated) { - updated = new Date(file.updated); - updated.setHours(0, 0, 0, 0); - } - - // Format string date to a date object - lastUpdated.setFullYear(`20${dateString.substring(4, 6)}`); - lastUpdated.setMonth(parseInt(dateString.substring(2, 4)) - 1); - lastUpdated.setDate(dateString.substring(0, 2)); - lastUpdated.setHours(0, 0, 0, 0); - - if (updated && lastUpdated <= updated) { - console.debug(`Table ${toTable} already updated, skipping...`); - return; - } - const tx = await Self.beginTransaction({}); try { From 6410a4db42d1b38f9f9b6603c7091eae1184c347 Mon Sep 17 00:00:00 2001 From: joan Date: Tue, 6 Sep 2022 12:54:33 +0200 Subject: [PATCH 03/18] Send response before ending task execution --- back/methods/edi/updateData.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/back/methods/edi/updateData.js b/back/methods/edi/updateData.js index 121e96778..ef0a84d19 100644 --- a/back/methods/edi/updateData.js +++ b/back/methods/edi/updateData.js @@ -16,7 +16,7 @@ module.exports = Self => { } }); - Self.updateData = async() => { + Self.updateData = async callback => { const models = Self.app.models; // Get files checksum @@ -35,6 +35,8 @@ module.exports = Self => { console.debug(`File already updated, skipping...`); } + callback(null, true); + if (updatableFiles.length === 0) return false; From 37f0b557f45e9a9af03d7a6c52f397cb60edd17f Mon Sep 17 00:00:00 2001 From: joan Date: Tue, 6 Sep 2022 13:13:29 +0200 Subject: [PATCH 04/18] Removed callback --- back/methods/edi/updateData.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/back/methods/edi/updateData.js b/back/methods/edi/updateData.js index ef0a84d19..b68c76368 100644 --- a/back/methods/edi/updateData.js +++ b/back/methods/edi/updateData.js @@ -35,8 +35,6 @@ module.exports = Self => { console.debug(`File already updated, skipping...`); } - callback(null, true); - if (updatableFiles.length === 0) return false; From 5c5440cdfc7a9a6a5a11ea014708914ebfca7319 Mon Sep 17 00:00:00 2001 From: joan Date: Tue, 6 Sep 2022 13:13:49 +0200 Subject: [PATCH 05/18] Removed callback --- back/methods/edi/updateData.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back/methods/edi/updateData.js b/back/methods/edi/updateData.js index b68c76368..121e96778 100644 --- a/back/methods/edi/updateData.js +++ b/back/methods/edi/updateData.js @@ -16,7 +16,7 @@ module.exports = Self => { } }); - Self.updateData = async callback => { + Self.updateData = async() => { const models = Self.app.models; // Get files checksum From 7264595646c2e6d9622aa2eadfb771d3a15bec91 Mon Sep 17 00:00:00 2001 From: joan Date: Tue, 6 Sep 2022 14:23:25 +0200 Subject: [PATCH 06/18] Throw error for max-retries --- back/methods/edi/updateData.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back/methods/edi/updateData.js b/back/methods/edi/updateData.js index 121e96778..a143653c2 100644 --- a/back/methods/edi/updateData.js +++ b/back/methods/edi/updateData.js @@ -126,7 +126,7 @@ module.exports = Self => { const response = await new Promise((resolve, reject) => { ftpClient.exec((err, response) => { - if (response.error) { + if (err || response.error) { console.debug(`Error downloading checksum file... ${response.error}`); reject(err); } From 886abfd89723d3062bf8986cb134012fe1e123ad Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 7 Sep 2022 08:49:24 +0200 Subject: [PATCH 07/18] fix(monitor): fix left join salesFilter --- .../back/methods/sales-monitor/salesFilter.js | 12 ++++++------ .../methods/sales-monitor/specs/salesFilter.spec.js | 8 ++------ 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/modules/monitor/back/methods/sales-monitor/salesFilter.js b/modules/monitor/back/methods/sales-monitor/salesFilter.js index 9a7415055..0b97b11e1 100644 --- a/modules/monitor/back/methods/sales-monitor/salesFilter.js +++ b/modules/monitor/back/methods/sales-monitor/salesFilter.js @@ -207,7 +207,7 @@ module.exports = Self => { LEFT JOIN province p ON p.id = a.provinceFk LEFT JOIN warehouse w ON w.id = t.warehouseFk LEFT JOIN agencyMode am ON am.id = t.agencyModeFk - LEFT JOIN ticketState ts ON ts.ticketFk = t.id + STRAIGHT_JOIN ticketState ts ON ts.ticketFk = t.id LEFT JOIN state st ON st.id = ts.stateFk LEFT JOIN client c ON c.id = t.clientFk LEFT JOIN worker wk ON wk.id = c.salesPersonFk @@ -227,7 +227,7 @@ module.exports = Self => { // Get client debt balance stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.clientGetDebt'); stmts.push(` - CREATE TEMPORARY TABLE tmp.clientGetDebt + CREATE TEMPORARY TABLE tmp.clientGetDebt (PRIMARY KEY (clientFk)) ENGINE = MEMORY SELECT DISTINCT clientFk FROM tmp.filter`); @@ -238,7 +238,7 @@ module.exports = Self => { stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.tickets'); stmt = new ParameterizedSQL(` - CREATE TEMPORARY TABLE tmp.tickets + CREATE TEMPORARY TABLE tmp.tickets (PRIMARY KEY (id)) ENGINE = MEMORY SELECT f.*, r.risk AS debt @@ -268,10 +268,10 @@ module.exports = Self => { stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.sale_getProblems'); stmt = new ParameterizedSQL(` - CREATE TEMPORARY TABLE tmp.sale_getProblems + CREATE TEMPORARY TABLE tmp.sale_getProblems (INDEX (ticketFk)) ENGINE = MEMORY - SELECT f.id ticketFk, f.clientFk, f.warehouseFk, f.shipped + SELECT f.id ticketFk, f.clientFk, f.warehouseFk, f.shipped FROM tmp.filter f LEFT JOIN alertLevel al ON al.id = f.alertLevel WHERE (al.code = 'FREE' OR f.alertLevel IS NULL) @@ -377,7 +377,7 @@ module.exports = Self => { const ticketsIndex = stmts.push(stmt) - 1; stmts.push( - `DROP TEMPORARY TABLE + `DROP TEMPORARY TABLE tmp.filter, tmp.ticket_problems, tmp.sale_getProblems, diff --git a/modules/monitor/back/methods/sales-monitor/specs/salesFilter.spec.js b/modules/monitor/back/methods/sales-monitor/specs/salesFilter.spec.js index a4fb1b0af..0682cef09 100644 --- a/modules/monitor/back/methods/sales-monitor/specs/salesFilter.spec.js +++ b/modules/monitor/back/methods/sales-monitor/specs/salesFilter.spec.js @@ -147,16 +147,12 @@ describe('SalesMonitor salesFilter()', () => { const options = {transaction: tx}; const ctx = {req: {accessToken: {userId: 9}}, args: {pending: false}}; - const filter = {}; + const filter = {order: 'alertLevel ASC'}; const result = await models.SalesMonitor.salesFilter(ctx, filter, options); const firstRow = result[0]; - const secondRow = result[1]; - const thirdRow = result[2]; expect(result.length).toEqual(12); - expect(firstRow.state).toEqual('Entregado'); - expect(secondRow.state).toEqual('Entregado'); - expect(thirdRow.state).toEqual('Entregado'); + expect(firstRow.alertLevel).not.toEqual(0); await tx.rollback(); } catch (e) { From aba1696ad725e68e59de2c5eb12b149b4fc204fd Mon Sep 17 00:00:00 2001 From: joan Date: Wed, 7 Sep 2022 10:26:32 +0200 Subject: [PATCH 08/18] Changes --- back/methods/edi/updateData.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/back/methods/edi/updateData.js b/back/methods/edi/updateData.js index a143653c2..b081c4687 100644 --- a/back/methods/edi/updateData.js +++ b/back/methods/edi/updateData.js @@ -56,8 +56,6 @@ module.exports = Self => { for (const table of tables) { const fileName = table.file; - console.debug(`Downloading file ${fileName}...`); - remoteFile = `codes/${fileName}.ZIP`; tempDir = `${tempPath}/${fileName}`; tempFile = `${tempPath}/${fileName}.zip`; @@ -66,14 +64,14 @@ module.exports = Self => { await fs.readFile(tempFile); } catch (error) { if (error.code === 'ENOENT') { + console.debug(`Downloading file ${fileName}...`); const downloadOutput = await downloadFile(remoteFile, tempFile); if (downloadOutput.error) continue; } } - console.debug(`Extracting file ${fileName}...`); - await extractFile(tempFile, tempDir); + await extractFile(fileName, tempFile, tempDir); console.debug(`Updating table ${table.toTable}...`); await dumpData(tempDir, table); @@ -90,6 +88,7 @@ module.exports = Self => { // Clean files try { + console.debug(`Cleaning files...`); await fs.remove(tempPath); } catch (error) { if (error.code !== 'ENOENT') @@ -128,7 +127,7 @@ module.exports = Self => { ftpClient.exec((err, response) => { if (err || response.error) { console.debug(`Error downloading checksum file... ${response.error}`); - reject(err); + return reject(err); } resolve(response); @@ -159,9 +158,9 @@ module.exports = Self => { return new Promise((resolve, reject) => { ftpClient.exec((err, response) => { - if (response.error) { + if (err || response.error) { console.debug(`Error downloading file... ${response.error}`); - reject(err); + return reject(err); } resolve(response); @@ -169,11 +168,12 @@ module.exports = Self => { }); } - async function extractFile(tempFile, tempDir) { + async function extractFile(fileName, tempFile, tempDir) { const JSZip = require('jszip'); try { await fs.mkdir(tempDir); + console.debug(`Extracting file ${fileName}...`); } catch (error) { if (error.code !== 'EEXIST') throw e; From bbc5790945bce971f39a0e8e48f34b971865f3f4 Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 7 Sep 2022 10:30:50 +0200 Subject: [PATCH 09/18] fix(smartTable): refresh button await query --- front/core/components/smart-table/index.html | 17 +++++++++-------- front/core/components/smart-table/index.js | 6 ++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/front/core/components/smart-table/index.html b/front/core/components/smart-table/index.html index a3295c47e..f26a6b4a2 100644 --- a/front/core/components/smart-table/index.html +++ b/front/core/components/smart-table/index.html @@ -10,9 +10,9 @@
-
- {{model.data.length}} - results +
+ {{model.data.length}} + results
@@ -64,7 +65,7 @@
Shown columns
-
@@ -101,4 +102,4 @@ - \ No newline at end of file + diff --git a/front/core/components/smart-table/index.js b/front/core/components/smart-table/index.js index 401541c2c..9e6e7009c 100644 --- a/front/core/components/smart-table/index.js +++ b/front/core/components/smart-table/index.js @@ -511,6 +511,12 @@ export default class SmartTable extends Component { return this.model.save() .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))); } + + refresh() { + this.isRefreshing = true; + this.model.refresh() + .then(() => this.isRefreshing = false); + } } SmartTable.$inject = ['$element', '$scope', '$transclude']; From 8f86cacc960173daafc4e444458fc996daeb8790 Mon Sep 17 00:00:00 2001 From: joan Date: Wed, 7 Sep 2022 10:48:24 +0200 Subject: [PATCH 10/18] Missing await --- back/methods/edi/updateData.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/back/methods/edi/updateData.js b/back/methods/edi/updateData.js index b081c4687..7f4f0b5e7 100644 --- a/back/methods/edi/updateData.js +++ b/back/methods/edi/updateData.js @@ -79,6 +79,7 @@ module.exports = Self => { // Update files checksum for (const file of updatableFiles) { + console.log(`Updating file ${file.name} checksum...`); await Self.rawSql(` UPDATE edi.fileConfig SET checksum = ? @@ -226,9 +227,9 @@ module.exports = Self => { `, [new Date(), baseName], options); } - tx.commit(); + await tx.commit(); } catch (error) { - tx.rollback(); + await tx.rollback(); throw error; } console.log(`Updated table ${toTable}\n`); From 48916e2b07869e32bd69a54ede1128ab7adcfa29 Mon Sep 17 00:00:00 2001 From: joan Date: Wed, 7 Sep 2022 11:26:58 +0200 Subject: [PATCH 11/18] Debug logs --- back/methods/edi/updateData.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/back/methods/edi/updateData.js b/back/methods/edi/updateData.js index 7f4f0b5e7..b6b06e19f 100644 --- a/back/methods/edi/updateData.js +++ b/back/methods/edi/updateData.js @@ -202,16 +202,20 @@ module.exports = Self => { const baseName = table.fileName; const tx = await Self.beginTransaction({}); - + console.log(`Started transaction`); try { const options = {transaction: tx}; + console.log(`Emptying table ${toTable}...`); const tableName = `edi.${toTable}`; await Self.rawSql(`DELETE FROM ??`, [tableName], options); + console.log(`Reading files...`); const dirFiles = await fs.readdir(tempDir); const files = dirFiles.filter(file => file.startsWith(baseName)); + console.log('Files to import: ' + files.join(', ')); + for (const file of files) { console.log(`Dumping data from file ${file}...`); @@ -228,6 +232,7 @@ module.exports = Self => { } await tx.commit(); + console.log(`Closed transaction`); } catch (error) { await tx.rollback(); throw error; From aec053868d3293ae164a800e53c7f68f26aa682a Mon Sep 17 00:00:00 2001 From: joan Date: Wed, 7 Sep 2022 11:50:02 +0200 Subject: [PATCH 12/18] Debug logs --- back/methods/edi/updateData.js | 1 + 1 file changed, 1 insertion(+) diff --git a/back/methods/edi/updateData.js b/back/methods/edi/updateData.js index b6b06e19f..58ea498ad 100644 --- a/back/methods/edi/updateData.js +++ b/back/methods/edi/updateData.js @@ -201,6 +201,7 @@ module.exports = Self => { const toTable = table.toTable; const baseName = table.fileName; + console.log(`Starting transaction...`); const tx = await Self.beginTransaction({}); console.log(`Started transaction`); try { From ab86ba0652e8cbff70f42fea754bedc043ba8d64 Mon Sep 17 00:00:00 2001 From: joan Date: Wed, 7 Sep 2022 12:28:53 +0200 Subject: [PATCH 13/18] Disabled transaction --- back/methods/edi/updateData.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/back/methods/edi/updateData.js b/back/methods/edi/updateData.js index 58ea498ad..f33466356 100644 --- a/back/methods/edi/updateData.js +++ b/back/methods/edi/updateData.js @@ -202,14 +202,14 @@ module.exports = Self => { const baseName = table.fileName; console.log(`Starting transaction...`); - const tx = await Self.beginTransaction({}); + // const tx = await Self.beginTransaction({}); console.log(`Started transaction`); try { - const options = {transaction: tx}; + // const options = {transaction: tx}; console.log(`Emptying table ${toTable}...`); const tableName = `edi.${toTable}`; - await Self.rawSql(`DELETE FROM ??`, [tableName], options); + await Self.rawSql(`DELETE FROM ??`, [tableName]); console.log(`Reading files...`); const dirFiles = await fs.readdir(tempDir); @@ -224,18 +224,19 @@ module.exports = Self => { const sqlTemplate = await fs.readFile(templatePath, 'utf8'); const filePath = path.join(tempDir, file); - await Self.rawSql(sqlTemplate, [filePath], options); + await Self.rawSql(sqlTemplate, [filePath]); await Self.rawSql(` UPDATE edi.tableConfig SET updated = ? WHERE fileName = ? - `, [new Date(), baseName], options); + `, [new Date(), baseName]); } - await tx.commit(); + // await tx.commit(); console.log(`Closed transaction`); } catch (error) { - await tx.rollback(); + // await tx.rollback(); + console.log(error); throw error; } console.log(`Updated table ${toTable}\n`); From a15c051098dd7ded735b9d15df683c269d9ee36c Mon Sep 17 00:00:00 2001 From: alexm Date: Thu, 8 Sep 2022 08:07:46 +0200 Subject: [PATCH 14/18] fix(monitor_salesFilter): mariadb not optimize query --- modules/monitor/back/methods/sales-monitor/salesFilter.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/modules/monitor/back/methods/sales-monitor/salesFilter.js b/modules/monitor/back/methods/sales-monitor/salesFilter.js index 0b97b11e1..ff642b088 100644 --- a/modules/monitor/back/methods/sales-monitor/salesFilter.js +++ b/modules/monitor/back/methods/sales-monitor/salesFilter.js @@ -164,6 +164,10 @@ module.exports = Self => { let stmt; stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.filter'); + + stmts.push(`SET @_optimizer_search_depth = @@optimizer_search_depth`); + stmts.push(`SET SESSION optimizer_search_depth = 0`); + stmt = new ParameterizedSQL( `CREATE TEMPORARY TABLE tmp.filter (PRIMARY KEY (id)) @@ -224,6 +228,8 @@ module.exports = Self => { stmt.merge(conn.makeWhere(filter.where)); stmts.push(stmt); + stmts.push(`SET SESSION optimizer_search_depth = @_optimizer_search_depth`); + // Get client debt balance stmts.push('DROP TEMPORARY TABLE IF EXISTS tmp.clientGetDebt'); stmts.push(` From 151e1b52ed36eea7bff032a8883f97cba81b5bf2 Mon Sep 17 00:00:00 2001 From: joan Date: Thu, 8 Sep 2022 09:21:05 +0200 Subject: [PATCH 15/18] Updated transaction --- back/methods/edi/updateData.js | 191 +++++++++++++++++---------------- 1 file changed, 100 insertions(+), 91 deletions(-) diff --git a/back/methods/edi/updateData.js b/back/methods/edi/updateData.js index f33466356..af0653672 100644 --- a/back/methods/edi/updateData.js +++ b/back/methods/edi/updateData.js @@ -20,80 +20,89 @@ module.exports = Self => { const models = Self.app.models; // Get files checksum - const files = await Self.rawSql('SELECT name, checksum, keyValue FROM edi.fileConfig'); + const tx = await Self.beginTransaction({}); - const updatableFiles = []; - for (const file of files) { - const fileChecksum = await getChecksum(file); + try { + const options = {transaction: tx}; + const files = await Self.rawSql('SELECT name, checksum, keyValue FROM edi.fileConfig', null, options); - if (file.checksum != fileChecksum) { - updatableFiles.push({ - name: file.name, - checksum: fileChecksum - }); - } else - console.debug(`File already updated, skipping...`); - } + const updatableFiles = []; + for (const file of files) { + const fileChecksum = await getChecksum(file); - if (updatableFiles.length === 0) - return false; - - // Download files - const container = await models.TempContainer.container('edi'); - const tempPath = path.join(container.client.root, container.name); - - let remoteFile; - let tempDir; - let tempFile; - - const fileNames = updatableFiles.map(file => file.name); - - const tables = await Self.rawSql(` - SELECT fileName, toTable, file - FROM edi.tableConfig - WHERE file IN (?)`, [fileNames]); - - for (const table of tables) { - const fileName = table.file; - - remoteFile = `codes/${fileName}.ZIP`; - tempDir = `${tempPath}/${fileName}`; - tempFile = `${tempPath}/${fileName}.zip`; - - try { - await fs.readFile(tempFile); - } catch (error) { - if (error.code === 'ENOENT') { - console.debug(`Downloading file ${fileName}...`); - const downloadOutput = await downloadFile(remoteFile, tempFile); - if (downloadOutput.error) - continue; - } + if (file.checksum != fileChecksum) { + updatableFiles.push({ + name: file.name, + checksum: fileChecksum + }); + } else + console.debug(`File already updated, skipping...`); } - await extractFile(fileName, tempFile, tempDir); + if (updatableFiles.length === 0) + return false; - console.debug(`Updating table ${table.toTable}...`); - await dumpData(tempDir, table); - } + // Download files + const container = await models.TempContainer.container('edi'); + const tempPath = path.join(container.client.root, container.name); - // Update files checksum - for (const file of updatableFiles) { - console.log(`Updating file ${file.name} checksum...`); - await Self.rawSql(` - UPDATE edi.fileConfig - SET checksum = ? - WHERE name = ?`, - [file.checksum, file.name]); - } + let remoteFile; + let tempDir; + let tempFile; - // Clean files - try { - console.debug(`Cleaning files...`); - await fs.remove(tempPath); + const fileNames = updatableFiles.map(file => file.name); + + const tables = await Self.rawSql(` + SELECT fileName, toTable, file + FROM edi.tableConfig + WHERE file IN (?)`, [fileNames], options); + + for (const table of tables) { + const fileName = table.file; + + remoteFile = `codes/${fileName}.ZIP`; + tempDir = `${tempPath}/${fileName}`; + tempFile = `${tempPath}/${fileName}.zip`; + + try { + await fs.readFile(tempFile); + } catch (error) { + if (error.code === 'ENOENT') { + console.debug(`Downloading file ${fileName}...`); + const downloadOutput = await downloadFile(remoteFile, tempFile); + if (downloadOutput.error) + continue; + } + } + + await extractFile(fileName, tempFile, tempDir); + + console.debug(`Updating table ${table.toTable}...`); + await dumpData(tempDir, table, options); + } + + // Update files checksum + for (const file of updatableFiles) { + console.log(`Updating file ${file.name} checksum...`); + await Self.rawSql(` + UPDATE edi.fileConfig + SET checksum = ? + WHERE name = ?`, + [file.checksum, file.name], options); + } + + // Clean files + try { + console.debug(`Cleaning files...`); + await fs.remove(tempPath); + } catch (error) { + if (error.code !== 'ENOENT') + throw e; + } } catch (error) { - if (error.code !== 'ENOENT') - throw e; + await tx.rollback(); + console.log(error); + throw error; } return true; @@ -197,48 +206,48 @@ module.exports = Self => { } } - async function dumpData(tempDir, table) { + async function dumpData(tempDir, table, options) { const toTable = table.toTable; const baseName = table.fileName; console.log(`Starting transaction...`); // const tx = await Self.beginTransaction({}); console.log(`Started transaction`); - try { - // const options = {transaction: tx}; + // try { + // const options = {transaction: tx}; - console.log(`Emptying table ${toTable}...`); - const tableName = `edi.${toTable}`; - await Self.rawSql(`DELETE FROM ??`, [tableName]); + console.log(`Emptying table ${toTable}...`); + const tableName = `edi.${toTable}`; + await Self.rawSql(`DELETE FROM ??`, [tableName]); - console.log(`Reading files...`); - const dirFiles = await fs.readdir(tempDir); - const files = dirFiles.filter(file => file.startsWith(baseName)); + console.log(`Reading files...`); + const dirFiles = await fs.readdir(tempDir); + const files = dirFiles.filter(file => file.startsWith(baseName)); - console.log('Files to import: ' + files.join(', ')); + console.log('Files to import: ' + files.join(', ')); - for (const file of files) { - console.log(`Dumping data from file ${file}...`); + for (const file of files) { + console.log(`Dumping data from file ${file}...`); - const templatePath = path.join(__dirname, `./sql/${toTable}.sql`); - const sqlTemplate = await fs.readFile(templatePath, 'utf8'); - const filePath = path.join(tempDir, file); + const templatePath = path.join(__dirname, `./sql/${toTable}.sql`); + const sqlTemplate = await fs.readFile(templatePath, 'utf8'); + const filePath = path.join(tempDir, file); - await Self.rawSql(sqlTemplate, [filePath]); - await Self.rawSql(` + await Self.rawSql(sqlTemplate, [filePath], options); + await Self.rawSql(` UPDATE edi.tableConfig SET updated = ? WHERE fileName = ? - `, [new Date(), baseName]); - } - - // await tx.commit(); - console.log(`Closed transaction`); - } catch (error) { - // await tx.rollback(); - console.log(error); - throw error; + `, [new Date(), baseName], options); } + + // await tx.commit(); + // console.log(`Closed transaction`); + // } catch (error) { + // await tx.rollback(); + // console.log(error); + // throw error; + // } console.log(`Updated table ${toTable}\n`); } }; From b499de7c08c0f0faca5621353a15b608fb2a7bda Mon Sep 17 00:00:00 2001 From: joan Date: Thu, 8 Sep 2022 10:33:51 +0200 Subject: [PATCH 16/18] tx commit --- back/methods/edi/updateData.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/back/methods/edi/updateData.js b/back/methods/edi/updateData.js index af0653672..747fff4b8 100644 --- a/back/methods/edi/updateData.js +++ b/back/methods/edi/updateData.js @@ -91,6 +91,8 @@ module.exports = Self => { [file.checksum, file.name], options); } + await tx.commit(); + // Clean files try { console.debug(`Cleaning files...`); @@ -101,7 +103,6 @@ module.exports = Self => { } } catch (error) { await tx.rollback(); - console.log(error); throw error; } From 4251b0baeb466657b7456bee360e02633f2d98c8 Mon Sep 17 00:00:00 2001 From: joan Date: Thu, 8 Sep 2022 11:19:04 +0200 Subject: [PATCH 17/18] Removed debug logs --- back/methods/edi/updateData.js | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/back/methods/edi/updateData.js b/back/methods/edi/updateData.js index 747fff4b8..a7375676a 100644 --- a/back/methods/edi/updateData.js +++ b/back/methods/edi/updateData.js @@ -211,22 +211,13 @@ module.exports = Self => { const toTable = table.toTable; const baseName = table.fileName; - console.log(`Starting transaction...`); - // const tx = await Self.beginTransaction({}); - console.log(`Started transaction`); - // try { - // const options = {transaction: tx}; - console.log(`Emptying table ${toTable}...`); const tableName = `edi.${toTable}`; await Self.rawSql(`DELETE FROM ??`, [tableName]); - console.log(`Reading files...`); const dirFiles = await fs.readdir(tempDir); const files = dirFiles.filter(file => file.startsWith(baseName)); - console.log('Files to import: ' + files.join(', ')); - for (const file of files) { console.log(`Dumping data from file ${file}...`); @@ -242,13 +233,6 @@ module.exports = Self => { `, [new Date(), baseName], options); } - // await tx.commit(); - // console.log(`Closed transaction`); - // } catch (error) { - // await tx.rollback(); - // console.log(error); - // throw error; - // } console.log(`Updated table ${toTable}\n`); } }; From fce26342a9aa5ed26f7066884af1d615fcf7d9db Mon Sep 17 00:00:00 2001 From: joan Date: Thu, 8 Sep 2022 11:42:16 +0200 Subject: [PATCH 18/18] Return inside try-catch --- back/methods/edi/updateData.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/back/methods/edi/updateData.js b/back/methods/edi/updateData.js index a7375676a..86a9e1f31 100644 --- a/back/methods/edi/updateData.js +++ b/back/methods/edi/updateData.js @@ -101,12 +101,12 @@ module.exports = Self => { if (error.code !== 'ENOENT') throw e; } + + return true; } catch (error) { await tx.rollback(); throw error; } - - return true; }; let ftpClient;