From 2d9c8e30693d1bddf1af7b2ffe083b467e7600f6 Mon Sep 17 00:00:00 2001 From: Inga Date: Mon, 15 Jan 2024 20:00:22 +0000 Subject: [PATCH 1/2] fixed app crashing when database query rejects (closes #3) --- src/transaction/transaction.controller.ts | 47 +++++++++++++---------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/src/transaction/transaction.controller.ts b/src/transaction/transaction.controller.ts index b6af91b..0c50631 100644 --- a/src/transaction/transaction.controller.ts +++ b/src/transaction/transaction.controller.ts @@ -117,17 +117,22 @@ export class TransactionController { try { this.exchangeRateService.convertCurrency(exhangeRateInput).subscribe({ - next: async (exchangeRateResponse) => { - this.transactionService.insertOne({ - date: transactionInput.date, - amount: parseInt(transactionInput.amount), - currency: transactionInput.currency, - client_id: transactionInput.client_id, - commission: await commissionAmount, - base_currency: Currency.EUR, - base_amount: parseInt(transactionInput.amount) * exchangeRateResponse[transactionInput.currency], - }); - }, + next: (exchangeRateResponse) => + commissionAmount + .then((commission) => + this.transactionService.insertOne({ + date: transactionInput.date, + amount: parseInt(transactionInput.amount), + currency: transactionInput.currency, + client_id: transactionInput.client_id, + commission, + base_currency: Currency.EUR, + base_amount: + parseInt(transactionInput.amount) * + exchangeRateResponse[transactionInput.currency], + }), + ) + .catch((error) => console.log(error)), error: (error) => { console.log(error); }, @@ -145,15 +150,17 @@ export class TransactionController { transactionInput, ); try { - this.transactionService.insertOne({ - date: transactionInput.date, - amount: parseInt(transactionInput.amount), - currency: transactionInput.currency, - client_id: transactionInput.client_id, - commission: commissionAmount, - base_currency: Currency.EUR, - base_amount: parseInt(transactionInput.amount), - }); + this.transactionService + .insertOne({ + date: transactionInput.date, + amount: parseInt(transactionInput.amount), + currency: transactionInput.currency, + client_id: transactionInput.client_id, + commission: commissionAmount, + base_currency: Currency.EUR, + base_amount: parseInt(transactionInput.amount), + }) + .catch((error) => console.log(error)); } catch (error) { console.log(error); throw error; From 8deb17f87bba80e16a4dac38157d565da5f53314 Mon Sep 17 00:00:00 2001 From: Inga Date: Mon, 15 Jan 2024 20:01:18 +0000 Subject: [PATCH 2/2] enabled no-floating-promises eslint rules to prevent #3 from happening again --- .eslintrc.js | 1 + src/main.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.eslintrc.js b/.eslintrc.js index f6c62be..96eeef8 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -16,6 +16,7 @@ module.exports = { }, ignorePatterns: ['.eslintrc.js'], rules: { + '@typescript-eslint/no-floating-promises': 'error', '@typescript-eslint/interface-name-prefix': 'off', '@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/explicit-module-boundary-types': 'off', diff --git a/src/main.ts b/src/main.ts index a0b44e3..0a7a6f1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,4 +6,4 @@ async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(3000); } -bootstrap(); +void bootstrap();