Skip to content

Commit

Permalink
Removing opening balance
Browse files Browse the repository at this point in the history
  • Loading branch information
hhassan01 committed Aug 30, 2024
1 parent 13ccb22 commit c9410c2
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 18 deletions.
4 changes: 3 additions & 1 deletion serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ resources:
source:
- 'accounts-service'
detail-type:
- 'TransactionCreated'
- 'TransactionCreated'
- 'AccountCreated'
- 'AccountUpdated'
Targets:
- Arn:
Fn::GetAtt:
Expand Down
20 changes: 20 additions & 0 deletions src/events/accountEvent.mock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "0",
"id": "c0cfc740-a244-f569-5a34-e7784dc1f12c",
"detail-type": "AccountCreated",
"source": "accounts-service",
"accountId": "123456789012",
"time": "2024-08-29T07:35:00Z",
"date": "2024-08-29T07:35:00Z",
"region": "eu-north-1",
"resources": [],
"detail": {
"id": "66cbccb349f8791ba0cc98b8",
"currencies": ["USD", "BTC"],
"balances": {
"USD": 123,
"BTC": 12455
},
"date": "2024-08-29T07:35:00Z"
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion src/handlers/transactionEventListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const handler = async (event: EventBridgeEvent): Promise<void> => {
const reportingService = new ReportingService(Statement)
const eventBridgeEvent = JSON.stringify(event, null, 2)
const eventDetails = JSON.parse(eventBridgeEvent)['detail']

console.log({eventDetails})
try {
if (event['detail-type'] === Events.TransactionCreated) {
await reportingService.processTransactionEvent(eventDetails)
Expand Down
2 changes: 0 additions & 2 deletions src/models/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export interface ICurrencyStatement {
amount: number
date: Date
}[]
openingBalance: number
closingBalance: number
}

Expand All @@ -33,7 +32,6 @@ const CurrencyStatementSchema = new Schema<ICurrencyStatement>({
date: { type: Date, required: true },
},
],
openingBalance: { type: Number, required: true },
closingBalance: { type: Number, required: true },
})

Expand Down
9 changes: 4 additions & 5 deletions src/services/reportingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ export class ReportingService {
async processAccountEvent(event: AccountEvent): Promise<void> {
const { id: accountId, currencies, balances, date } = event
const { month, year } = getMonthYearFromDate(date)

let statement = await this.getOrCreateStatement(accountId, month, year)

currencies.forEach(currency => {
let currencyStatement = this.getOrCreateCurrencyStatement(statement, currency)
currencyStatement.closingBalance = balances.get(currency) || 0
currencyStatement.closingBalance = balances[currency] || 0

const currencyStatementIndex = statement.currencies.findIndex(
(cs) => cs.currency === currency,
)

statement.currencies[currencyStatementIndex] = currencyStatement
})

console.log("GO 10")
await statement.save()
}

Expand Down Expand Up @@ -99,7 +99,6 @@ export class ReportingService {
currencyStatement = {
currency,
transactions: [],
openingBalance: 0,
closingBalance: 0,
}
statement.currencies.push(currencyStatement)
Expand Down
1 change: 0 additions & 1 deletion tests/handlers/getMonthlyStatements.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ describe('getMonthlyStatementsHandler', () => {
{
currency: 'USD',
transactions: [],
openingBalance: 0,
closingBalance: 100,
},
],
Expand Down
4 changes: 0 additions & 4 deletions tests/models/statement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe('Statement Model Test', () => {
date: new Date('2024-08-28T12:00:00Z'),
},
],
openingBalance: 0,
closingBalance: 100,
},
],
Expand Down Expand Up @@ -71,7 +70,6 @@ describe('Statement Model Test', () => {
{
currency: 'USD',
transactions: [],
openingBalance: 0,
closingBalance: 0,
},
],
Expand All @@ -81,7 +79,6 @@ describe('Statement Model Test', () => {
statement.currencies.push({
currency: 'EUR',
transactions: [],
openingBalance: 0,
closingBalance: 0,
})
const updatedStatement = await statement.save()
Expand All @@ -105,7 +102,6 @@ describe('Statement Model Test', () => {
date: new Date('2024-08-28T12:00:00Z'),
},
],
openingBalance: 0,
closingBalance: 100,
},
],
Expand Down
4 changes: 0 additions & 4 deletions tests/services/reportingService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ describe('ReportingService', () => {
{
currency: 'USD',
transactions: [],
openingBalance: 0,
closingBalance: 50,
},
],
Expand Down Expand Up @@ -103,7 +102,6 @@ describe('ReportingService', () => {
{
currency: 'EUR',
transactions: [],
openingBalance: 0,
closingBalance: 0,
},
],
Expand Down Expand Up @@ -181,13 +179,11 @@ describe('ReportingService', () => {
{
currency: 'USD',
transactions: [],
openingBalance: 0,
closingBalance: 1000,
},
{
currency: 'EUR',
transactions: [],
openingBalance: 0,
closingBalance: 2000,
},
],
Expand Down

0 comments on commit c9410c2

Please sign in to comment.