Skip to content

Commit

Permalink
Add RequestID and Timestamp attributes to the DynamoDB item
Browse files Browse the repository at this point in the history
  • Loading branch information
kris-szlapa committed Mar 4, 2024
1 parent c0f1f9a commit fc0143f
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packages/updatePrescriptionStatus/src/updatePrescriptionStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@ import {marshall} from "@aws-sdk/util-dynamodb"
import middy from "@middy/core"
import inputOutputLogger from "@middy/input-output-logger"
import errorHandler from "@nhs/fhir-middy-error-handler"
import {v4 as uuidv4} from "uuid"

const logger = new Logger({serviceName: "updatePrescriptionStatus"})
const client = new DynamoDBClient({region: "eu-west-2"})
const tableName = "PrescriptionStatusUpdate"

const lambdaHandler = async (
event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {
try {
// Parse request body
let requestBody
if (typeof event.body === "string") {
requestBody = JSON.parse(event.body)
} else {
throw new Error("Invalid request body")
}
const requestBody = JSON.parse(event.body || "")

// Extract relevant data from request body
const {
Expand Down Expand Up @@ -59,17 +56,22 @@ const lambdaHandler = async (
LineItemStatus: line_item_status,
TerminalStatusIndicator: terminal_status_indicator,
LastUpdated: last_updated,
Note: note || null // Ensuring 'null' if note is undefined or null
Note: note || null, // Ensuring 'null' if note is undefined or null
RequestID: uuidv4(), // Adding a unique request ID
Timestamp: new Date().toISOString() // Adding timestamp
})

// Put item in DynamoDB table
const command = new PutItemCommand({
TableName: "PrescriptionStatusTable",
TableName: tableName,
Item: item
})

await client.send(command)

// Log audit for request
logger.info("updatePrescriptionStatus request", {requestBody})

// Return success response
return {
statusCode: 201,
Expand All @@ -79,6 +81,9 @@ const lambdaHandler = async (
// Log error using powertools logger
logger.error("Error occurred: ", error as Error) // Cast error to Error type

// Log audit for request error
logger.error("updatePrescriptionStatus request error", {event})

// Return error response
if (error instanceof SyntaxError) {
// Return 400 Bad Request if the request body is not valid JSON
Expand Down

0 comments on commit fc0143f

Please sign in to comment.