From 9bb264da38a48fac4a1a4e1996926c1f9c974361 Mon Sep 17 00:00:00 2001 From: joan Date: Fri, 3 Jun 2022 12:52:41 +0200 Subject: [PATCH] Fixes --- back/methods/edi/updateData.js | 80 +++++++++++++++++----------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/back/methods/edi/updateData.js b/back/methods/edi/updateData.js index cef60adf9..b3b52e060 100644 --- a/back/methods/edi/updateData.js +++ b/back/methods/edi/updateData.js @@ -83,7 +83,7 @@ module.exports = Self => { const AdmZip = require('adm-zip'); console.log(paths.remoteFile); console.log(paths.tempFile); - console.log('Exists temp file: ', fs.existsSync(paths.tempFile)) + console.log('Exists temp file: ', fs.existsSync(paths.tempFile)); const zip = new AdmZip(paths.tempFile); const entries = zip.getEntries(); @@ -102,58 +102,58 @@ module.exports = Self => { const toTable = file.toTable; const baseName = file.fileName; - for (const zipEntry of entries) { - const entryName = zipEntry.entryName; - console.log(`Reading file ${entryName}...`); + const tableName = `edi.${toTable}`; + await Self.rawSql(`DELETE FROM ??`, [tableName], options); - const startIndex = (entryName.length - 10); - const endIndex = (entryName.length - 4); - const dateString = entryName.substring(startIndex, endIndex); - const lastUpdated = new Date(); + const tx = await Self.beginTransaction({}); + try { + const options = {transaction: tx}; - // Format string date to a date object - let updated = null; - if (file.updated) { - updated = new Date(file.updated); - updated.setHours(0, 0, 0, 0); - } + for (const zipEntry of entries) { + const entryName = zipEntry.entryName; + console.log(`Reading file ${entryName}...`); - 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); + const startIndex = (entryName.length - 10); + const endIndex = (entryName.length - 4); + const dateString = entryName.substring(startIndex, endIndex); + const lastUpdated = new Date(); - if (updated && lastUpdated <= updated) { - console.debug(`Table ${toTable} already updated, skipping...`); - continue; - } + // Format string date to a date object + let updated = null; + if (file.updated) { + updated = new Date(file.updated); + updated.setHours(0, 0, 0, 0); + } - console.log('Dumping data...'); - const templatePath = path.join(__dirname, `./sql/${toTable}.sql`); - const sqlTemplate = fs.readFileSync(templatePath, 'utf8'); + 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); - const rawPath = path.join(paths.tempDir, entryName); + if (updated && lastUpdated <= updated) { + console.debug(`Table ${toTable} already updated, skipping...`); + continue; + } - const tx = await Self.beginTransaction({}); - try { - const options = {transaction: tx}; + console.log('Dumping data...'); + const templatePath = path.join(__dirname, `./sql/${toTable}.sql`); + const sqlTemplate = fs.readFileSync(templatePath, 'utf8'); + + const rawPath = path.join(paths.tempDir, entryName); - const tableName = `edi.${toTable}`; - await Self.rawSql(`DELETE FROM ??`, [tableName], options); await Self.rawSql(sqlTemplate, [rawPath], options); await Self.rawSql(` - UPDATE edi.fileConfig - SET updated = ? - WHERE fileName = ? - `, [lastUpdated, baseName], options); + UPDATE edi.fileConfig + SET updated = ? + WHERE fileName = ? + `, [lastUpdated, baseName], options); tx.commit(); - } catch (error) { - tx.rollback(); - throw error; } - - console.log(`Updated table ${toTable}\n`); + } catch (error) { + tx.rollback(); + throw error; } + console.log(`Updated table ${toTable}\n`); } };