From 8f956625b9a57de6325d9dbec9982e56efd9a4d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Faneca?= Date: Wed, 26 Jun 2024 17:36:42 +0100 Subject: [PATCH 1/2] [#72] GET /budgets/:id - category amount calculations for GET /budgets/:id --- src/services/categoryService.ts | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/services/categoryService.ts b/src/services/categoryService.ts index 9bf9594..f49159d 100644 --- a/src/services/categoryService.ts +++ b/src/services/categoryService.ts @@ -119,32 +119,28 @@ const getAmountForCategoryInPeriod = async ( category_balance_credit: number; category_balance_debit: number }> => performDatabaseRequest(async (prismaTx) => { - /* Logger.addLog( - `Category: ${categoryId} | fromDate: ${fromDate} | toDate: ${toDate} | includeTransfers: ${includeTransfers}`); */ - let accountsToExcludeListInSQL = ''; const listOfAccountsToExclude = await prismaTx.accounts.findMany({ - where: {exclude_from_budgets: true}, + where: { exclude_from_budgets: true }, }); - accountsToExcludeListInSQL = buildSqlForExcludedAccountsList(listOfAccountsToExclude); - if (includeTransfers) { - return prismaTx.$queryRaw`SELECT sum(if(type = 'I' OR - (type = 'T' AND accounts_account_to_id NOT IN (${accountsToExcludeListInSQL})), - amount, - 0)) as 'category_balance_credit', + + const sqlQuery = includeTransfers ? + prismaTx.$queryRaw`SELECT sum(if(type = 'I', amount, 0)) as 'category_balance_credit', sum(if(type = 'E' OR - (type = 'T' AND accounts_account_from_id NOT IN (${accountsToExcludeListInSQL})), + + (type = 'T' AND accounts_account_to_id IN (${Prisma.join(listOfAccountsToExclude.map((a) => a.account_id))})), amount, 0)) as 'category_balance_debit' FROM transactions WHERE date_timestamp between ${fromDate} AND ${toDate} - AND categories_category_id = ${categoryId} `; - } - return prismaTx.$queryRaw`SELECT sum(if(type = 'I', amount, 0)) as 'category_balance_credit', + AND categories_category_id = ${categoryId} ` : + prismaTx.$queryRaw`SELECT sum(if(type = 'I', amount, 0)) as 'category_balance_credit', sum(if(type = 'E', amount, 0)) as 'category_balance_debit' FROM transactions WHERE date_timestamp between ${fromDate} AND ${toDate} AND categories_category_id = ${categoryId} `; + + return sqlQuery; }, dbClient); export interface CalculatedCategoryAmounts { From 1fb63ec67020874e91690f46f332ef2086fcd654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3nio=20Faneca?= Date: Wed, 26 Jun 2024 17:39:25 +0100 Subject: [PATCH 2/2] [#74] app version to 2.4.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 333ca8a..6088237 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "db:seed": "prisma db seed" }, "name": "myfin-api", - "version": "2.3.1", + "version": "2.4.0", "description": "NodeJS API for Myfin", "main": "src/server.js", "devDependencies": {