diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index 1a3f6b4..b801d00 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -52,10 +52,4 @@ jobs: run: npm ci - name: Deploy - run: | - serverless deploy \ - --param="MONGODB_URI=${{ secrets.MONGODB_URI_DEV }}" \ - --param="EVENT_BUS_NAME=${{ secrets.EVENT_BUS_DEV }}" \ - --param="EVENT_SOURCE=${{ secrets.EVENT_SOURCE }}" \ - --param="EVENT_DETAIL_TYPE=${{ secrets.EVENT_DETAIL_TYPE }}" \ - --param="AWS_REGION=${{ secrets.AWS_REGION }}" \ No newline at end of file + run: npm run deploy \ No newline at end of file diff --git a/.github/workflows/deploy-prod.yml b/.github/workflows/deploy-prod.yml index 0fd2b7b..9fa0712 100644 --- a/.github/workflows/deploy-prod.yml +++ b/.github/workflows/deploy-prod.yml @@ -52,4 +52,4 @@ jobs: run: npm ci - name: Deploy - run: serverless deploy --stage production \ No newline at end of file + run: npm run deploy:prod \ No newline at end of file diff --git a/package.json b/package.json index b694851..09a613b 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,8 @@ "scripts": { "clean": "rm -rf dist build", "build": "npm run clean && tsc", - "deploy": "serverless deploy -s production -r eu-north-1 -v", + "deploy": "serverless deploy -s dev -v", + "deploy:prod": "serverless deploy -s production -v", "offline": "npm run build && serverless offline", "lint": "eslint .", "lint:fix": "eslint . --fix", diff --git a/src/services/reportingService.ts b/src/services/reportingService.ts index aa8a988..2bf4f0e 100644 --- a/src/services/reportingService.ts +++ b/src/services/reportingService.ts @@ -1,6 +1,7 @@ import { Model } from 'mongoose' + import { IStatement } from '../models/statement' -import { TransactionEvent, TransactionType } from '../events/transactionEvent' +import { TransactionEvent } from '../events/transactionEvent' import { getMonthYearFromDate } from '../utils/dateUtils' import { NotFoundException } from '../utils/exceptions' @@ -14,46 +15,8 @@ export class ReportingService { async processTransactionEvent(event: TransactionEvent): Promise { const { accountId, currency, amount, type, date } = event const { month, year } = getMonthYearFromDate(date) - - const statement = await this.getOrCreateStatement(accountId, month, year) - const currencyStatement = this.getOrCreateCurrencyStatement( - statement, - currency, - ) - - const currencyStatementIndex = statement.currencies.findIndex( - (cs) => cs.currency === currency, - ) - this.updateCurrencyStatement(currencyStatement, amount, type, date) - statement.currencies[currencyStatementIndex] = currencyStatement - - await statement.save() - } - - async getMonthlyStatements( - accountId: string, - year: number, - month: number, - ): Promise { - const statement = await this.statementModel - .findOne({ accountId, year, month }) - .exec() - - if (!statement) { - throw new NotFoundException('Statement not found') - } - - return statement - } - - private async getOrCreateStatement( - accountId: string, - month: number, - year: number, - ): Promise { - let statement = await this.statementModel - .findOne({ accountId, month, year }) - .exec() + + let statement = await this.statementModel.findOne({ accountId, month, year }).exec() if (!statement) { statement = new this.statementModel({ accountId, @@ -62,13 +25,7 @@ export class ReportingService { currencies: [], }) } - return statement - } - - private getOrCreateCurrencyStatement( - statement: IStatement, - currency: string, - ) { + let currencyStatement = statement.currencies.find( (cs) => cs.currency === currency, ) @@ -80,23 +37,36 @@ export class ReportingService { } statement.currencies.push(currencyStatement) } - return currencyStatement - } - - private updateCurrencyStatement( - currencyStatement: IStatement['currencies'][0], - amount: number, - type: TransactionType, - date: Date, - ) { + + const currencyStatementIndex = statement.currencies.findIndex( + (cs) => cs.currency === currency, + ) const previousClosingBalance = currencyStatement.closingBalance || 0 - if (type === TransactionType.INBOUND) { + if (type === 'INBOUND') { currencyStatement.closingBalance = previousClosingBalance + amount - } else if (type === TransactionType.OUTBOUND) { + } else if (type === 'OUTBOUND') { currencyStatement.closingBalance = previousClosingBalance - amount } currencyStatement.transactions.push({ type, amount, date }) + + statement.currencies[currencyStatementIndex] = currencyStatement + + await statement.save() + } + + async getMonthlyStatements( + accountId: string, + year: number, + month: number, + ): Promise { + const statement = await this.statementModel.findOne({ accountId, year, month }).exec() + + if(!statement) { + throw new NotFoundException('Statement not found') + } + + return statement } -} +} \ No newline at end of file