From b1837eb43c16267170b4fdf7edaed75f70c2b28f Mon Sep 17 00:00:00 2001 From: Mark Turner Date: Fri, 3 May 2024 14:56:30 +0100 Subject: [PATCH] adjusted for blank column in excel sheet --- src/api/transaction/services/transaction.js | 30 +++++++++++-------- .../1.0.0/full_documentation.json | 2 +- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/api/transaction/services/transaction.js b/src/api/transaction/services/transaction.js index f654172..78f8747 100644 --- a/src/api/transaction/services/transaction.js +++ b/src/api/transaction/services/transaction.js @@ -15,7 +15,8 @@ module.exports = createCoreService('api::transaction.transaction', ({ strapi }) await workbook.xlsx.readFile(file.path) const transactionSheet = workbook.getWorksheet(TransactionsWorksheetName) - let transactions = [] + let transactions = [], + errorCount = 0 // clear all previous transactions; ID is always not null await strapi.db.query('api::transaction.transaction').deleteMany({ @@ -42,14 +43,14 @@ module.exports = createCoreService('api::transaction.transaction', ({ strapi }) documentNumber: Number(row.values[5]), documentHeader: row.values[6], name: row.values[7], - fiscalYear: Number(row.values[8]), - fiscalPeriod: Number(row.values[9]), - documentDate: DateTime.fromJSDate(new Date(row.values[10])).toISODate(), - postedDate: DateTime.fromJSDate(new Date(row.values[11])).toISODate(), + fiscalYear: Number(row.values[9]), + fiscalPeriod: Number(row.values[10]), + documentDate: DateTime.fromJSDate(new Date(row.values[11])).toISODate(), + postedDate: DateTime.fromJSDate(new Date(row.values[12])).toISODate(), // SAP gets the debit and credit wrong way around, times -1 to fix - value: (row.values[12].hasOwnProperty('result') ? parseFloat(row.values[12].result) : parseFloat(row.values[12])) * -1, - bwCategory: row.values[13].hasOwnProperty('result') ? row.values[13].result : row.values[13], - ieCategory: row.values[14].hasOwnProperty('result') ? row.values[14].result : row.values[14], + value: (row.values[13].hasOwnProperty('result') ? parseFloat(row.values[13].result) : parseFloat(row.values[13])) * -1, + bwCategory: row.values[14].hasOwnProperty('result') ? row.values[14].result : row.values[14], + ieCategory: row.values[15].hasOwnProperty('result') ? row.values[15].result : row.values[15], internalCategory: null } @@ -61,6 +62,7 @@ module.exports = createCoreService('api::transaction.transaction', ({ strapi }) isNaN(transaction.fiscalPeriod) || isNaN(transaction.value) ) { + errorCount++ console.error(transaction) } @@ -129,9 +131,13 @@ module.exports = createCoreService('api::transaction.transaction', ({ strapi }) } }) - let count = await strapi.db.query('api::transaction.transaction').createMany({ data: transactions }) - - return { message: 'Successfully uploaded transaction data', count } - + try { + await strapi.db.query('api::transaction.transaction').createMany({ data: transactions }) + return { message: 'Successfully uploaded transaction data' } + } + catch (error) { + console.error(`Failed to add ${transactions.length} transactions to the database. Encountered ${errorCount} errors.`) + console.error(error) + } }, })) diff --git a/src/extensions/documentation/documentation/1.0.0/full_documentation.json b/src/extensions/documentation/documentation/1.0.0/full_documentation.json index c87d518..7186cc2 100644 --- a/src/extensions/documentation/documentation/1.0.0/full_documentation.json +++ b/src/extensions/documentation/documentation/1.0.0/full_documentation.json @@ -14,7 +14,7 @@ "name": "Apache 2.0", "url": "https://www.apache.org/licenses/LICENSE-2.0.html" }, - "x-generation-date": "2024-04-28T13:53:40.862Z" + "x-generation-date": "2024-05-03T13:44:46.648Z" }, "x-strapi-config": { "path": "/documentation",