diff --git a/src/api/routes/roomRequests.ts b/src/api/routes/roomRequests.ts index 5d8ea850..6dd95636 100644 --- a/src/api/routes/roomRequests.ts +++ b/src/api/routes/roomRequests.ts @@ -35,7 +35,10 @@ import { getAllUserEmails, getDefaultFilteringQuerystring, } from "common/utils.js"; -import { ROOM_RESERVATION_RETENTION_DAYS } from "common/constants.js"; +import { + ROOM_RESERVATION_RETENTION_DAYS, + UPLOAD_GRACE_PERIOD_MS, +} from "common/constants.js"; import { createPresignedGet, createPresignedPut } from "api/functions/s3.js"; import { HeadObjectCommand, NotFound, S3Client } from "@aws-sdk/client-s3"; @@ -680,22 +683,29 @@ const roomRequestRoutes: FastifyPluginAsync = async (fastify, _options) => { ); } catch (error) { if (error instanceof NotFound) { - // Key doesn't exist in S3, delete the attribute from DynamoDB - await fastify.dynamoClient.send( - new UpdateItemCommand({ - TableName: genericConfig.RoomRequestsStatusTableName, - Key: { - requestId: { S: request.params.requestId }, - "createdAt#status": { - S: `${request.params.createdAt}#${request.params.status}`, + // Check if grace period has passed since creation + const createdAt = new Date(request.params.createdAt); + const now = new Date(); + const timeSinceCreation = now.getTime() - createdAt.getTime(); + + if (timeSinceCreation >= UPLOAD_GRACE_PERIOD_MS) { + // Grace period has passed, delete the attribute from DynamoDB + await fastify.dynamoClient.send( + new UpdateItemCommand({ + TableName: genericConfig.RoomRequestsStatusTableName, + Key: { + requestId: { S: request.params.requestId }, + "createdAt#status": { + S: `${request.params.createdAt}#${request.params.status}`, + }, }, - }, - UpdateExpression: "REMOVE #attachmentS3key", - ExpressionAttributeNames: { - "#attachmentS3key": "attachmentS3key", - }, - }), - ); + UpdateExpression: "REMOVE #attachmentS3key", + ExpressionAttributeNames: { + "#attachmentS3key": "attachmentS3key", + }, + }), + ); + } throw new NotFoundError({ endpointName: request.url, diff --git a/src/common/constants.ts b/src/common/constants.ts index 32bc6978..9b29a08b 100644 --- a/src/common/constants.ts +++ b/src/common/constants.ts @@ -3,4 +3,5 @@ export const AUDIT_LOG_RETENTION_DAYS = 365; export const ROOM_RESERVATION_RETENTION_DAYS = 1460; export const FULFILLED_PURCHASES_RETENTION_DAYS = 1460; // ticketing/merch: after the purchase is marked as fulfilled. export const EVENTS_EXPIRY_AFTER_LAST_OCCURRENCE_DAYS = 1460; // hold events for 4 years after last occurrence +export const UPLOAD_GRACE_PERIOD_MS = 30 * 60 * 1000; // 30 minutes // we keep data longer for historical analytics purposes in S3 as needed