Skip to content

Commit

Permalink
transaction event process fixed and deploymen
Browse files Browse the repository at this point in the history
  • Loading branch information
hhassan01 committed Aug 30, 2024
1 parent ddf6834 commit fb43b19
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 69 deletions.
8 changes: 1 addition & 7 deletions .github/workflows/deploy-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
run: npm run deploy
2 changes: 1 addition & 1 deletion .github/workflows/deploy-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ jobs:
run: npm ci

- name: Deploy
run: serverless deploy --stage production
run: npm run deploy:prod
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
90 changes: 30 additions & 60 deletions src/services/reportingService.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -14,46 +15,8 @@ export class ReportingService {
async processTransactionEvent(event: TransactionEvent): Promise<void> {
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<IStatement> {
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<IStatement> {
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,
Expand All @@ -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,
)
Expand All @@ -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<IStatement> {
const statement = await this.statementModel.findOne({ accountId, year, month }).exec()

if(!statement) {
throw new NotFoundException('Statement not found')
}

return statement
}
}
}

0 comments on commit fb43b19

Please sign in to comment.