Skip to content

Commit

Permalink
Fixed serverless permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
hhassan01 committed Aug 30, 2024
1 parent 8ac73a4 commit f093964
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 28 deletions.
50 changes: 42 additions & 8 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,59 @@ provider:
# MONGODB_URI: ${ssm:/accounts-service/MONGODB_URI}
# EVENT_BUS_NAME: ${ssm:/my-service/EVENT_BUS_NAME~true}


functions:
transactionEventListener:
handler: dist/handlers/transactionEventListener.handler
handler: src/handlers/transactionEventListener.handler
events:
- eventBridge:
eventBus: default
pattern:
source:
- accounts-service
- 'accounts-service'
detail-type:
- TransactionCreated
- 'TransactionCreated'
# retryPolicy:
# maximumRetryAttempts: 5

getMonthlyStatements:
handler: dist/handlers/getMonthlyStatements.handler
handler: src/handlers/getMonthlyStatements.handler
events:
- http:
path: statements/{accountId}/{year}/{month}
method: get

resources:
Resources:
TransactionEventListenerLambdaPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName:
Fn::GetAtt:
- TransactionEventListenerLambdaFunction
- Arn
Principal: events.amazonaws.com
SourceArn:
Fn::GetAtt:
- TransactionEventListenerEventRule
- Arn

TransactionEventListenerEventRule:
Type: AWS::Events::Rule
Properties:
EventBusName: default
EventPattern:
source:
- 'accounts-service'
detail-type:
- 'TransactionCreated'
Targets:
- Arn:
Fn::GetAtt:
- TransactionEventListenerLambdaFunction
- Arn
Id: "TransactionEventListenerTarget"

package:
individually: true
Expand All @@ -53,7 +88,6 @@ build:
target: "node20"
platform: "node"
exclude:
# - "jest"
# - "mongodb-memory-server"
- "prettier"
buildConcurrency: 3
- "jest"
- "mongodb-memory-server"
- "prettier"
7 changes: 0 additions & 7 deletions src/events/mockEvent.json

This file was deleted.

18 changes: 18 additions & 0 deletions src/events/mockEventBridge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": "0",
"id": "c0cfc740-a244-f569-5a34-e7784dc1f12c",
"detail-type": "TransactionCreated",
"source": "accounts-service",
"accountId": "123456789012",
"time": "2024-08-29T07:35:00Z",
"date": "2024-08-29T07:35:00Z",
"region": "eu-north-1",
"resources": [],
"detail": {
"accountId": "66cbccb349f8791ba0cc98b8",
"type": "INBOUND",
"currency": "USD",
"amount": 909,
"date": "2024-08-29T07:35:00Z"
}
}
18 changes: 8 additions & 10 deletions src/handlers/transactionEventListener.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { Statement } from '../models/statement'
import { connectToDatabase } from '../utils/connectToDB'
import { TransactionEvent } from '../events/transactionEvent'
import { ReportingService } from '../services/reportingService'

interface EventBridgeEvent {
Records: {
body: string;
}[];
body: string
}
}

export const handler = async (event: EventBridgeEvent): Promise<void> => {
await connectToDatabase()
const reportingService = new ReportingService(Statement)
// Check if running locally by detecting if it's an API Gateway event or a direct invocation
const records = event.Records
console.log({ records });
for (const record of records) {
const transactionEvent: TransactionEvent = JSON.parse(record.body)

await reportingService.processTransactionEvent(transactionEvent)
const transactionEvent = JSON.stringify(event, null, 2)

try {
await reportingService.processTransactionEvent(JSON.parse(transactionEvent).detail)
} catch (e) {
console.error("Error in processing transaction", e)
}
}
6 changes: 3 additions & 3 deletions src/services/reportingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class ReportingService {
async processTransactionEvent(event: TransactionEvent): Promise<void> {
const { accountId, currency, amount, type, date } = event
const { month, year } = getMonthYearFromDate(date)

let statement = await this.statementModel.findOne({ accountId, month, year }).exec()
if (!statement) {
statement = new this.statementModel({
Expand All @@ -25,7 +25,7 @@ export class ReportingService {
currencies: [],
})
}

let currencyStatement = statement.currencies.find(
(cs) => cs.currency === currency,
)
Expand All @@ -38,7 +38,7 @@ export class ReportingService {
}
statement.currencies.push(currencyStatement)
}

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

0 comments on commit f093964

Please sign in to comment.