-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
2,167 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { EventBridgeEvent } from 'aws-lambda'; | ||
import { UpdateItemCommandInput, UpdateItemCommand } from '@aws-sdk/client-dynamodb'; | ||
import { marshall } from '@aws-sdk/util-dynamodb'; | ||
import dynamoDbCommand from './utils/dynamoDbClient'; | ||
|
||
type EventData = { | ||
accountId: string; | ||
amount: number; | ||
}; | ||
|
||
const handler = async (event: EventBridgeEvent<string, EventData>) => { | ||
const data = parseEvent(event); | ||
|
||
console.log(`🔔 Received event: ${JSON.stringify(data)}`); | ||
|
||
if (!data || !data.accountId || !data.amount) { | ||
throw new Error(`🛑 No data found in event ${data}`); | ||
} | ||
|
||
// update dynamodb account | ||
const params: UpdateItemCommandInput = { | ||
TableName: process.env.DATA_TABLE_NAME, | ||
Key: marshall({ pk: `acc#${data.accountId}` }), | ||
UpdateExpression: 'SET balance = balance + :amount, updatedAt = :updatedAt', | ||
ExpressionAttributeValues: marshall({ | ||
':amount': data.amount, | ||
':updatedAt': new Date().toISOString(), | ||
}), | ||
ReturnValues: 'UPDATED_NEW', | ||
}; | ||
const result = await dynamoDbCommand(new UpdateItemCommand(params)); | ||
|
||
if (result.$metadata.httpStatusCode !== 200) { | ||
throw new Error(`🛑 Could not update account ${data.accountId}`); | ||
} | ||
}; | ||
|
||
function parseEvent(event: EventBridgeEvent<string, EventData>): EventData { | ||
const eventString: string = JSON.stringify(event); | ||
|
||
console.debug(`🕧 Received event: ${eventString}`); | ||
|
||
return JSON.parse(eventString).detail; | ||
} | ||
|
||
module.exports = { handler }; |
Oops, something went wrong.