Skip to content

Commit 400bba9

Browse files
Grant grace period on uploads for attachments (#371)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Added a 30-minute grace period before removing attachment references when files are temporarily unavailable, preventing premature cleanup of recently uploaded attachments and improving reliability. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 06aa3c0 commit 400bba9

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

src/api/routes/roomRequests.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ import {
3535
getAllUserEmails,
3636
getDefaultFilteringQuerystring,
3737
} from "common/utils.js";
38-
import { ROOM_RESERVATION_RETENTION_DAYS } from "common/constants.js";
38+
import {
39+
ROOM_RESERVATION_RETENTION_DAYS,
40+
UPLOAD_GRACE_PERIOD_MS,
41+
} from "common/constants.js";
3942
import { createPresignedGet, createPresignedPut } from "api/functions/s3.js";
4043
import { HeadObjectCommand, NotFound, S3Client } from "@aws-sdk/client-s3";
4144

@@ -680,22 +683,29 @@ const roomRequestRoutes: FastifyPluginAsync = async (fastify, _options) => {
680683
);
681684
} catch (error) {
682685
if (error instanceof NotFound) {
683-
// Key doesn't exist in S3, delete the attribute from DynamoDB
684-
await fastify.dynamoClient.send(
685-
new UpdateItemCommand({
686-
TableName: genericConfig.RoomRequestsStatusTableName,
687-
Key: {
688-
requestId: { S: request.params.requestId },
689-
"createdAt#status": {
690-
S: `${request.params.createdAt}#${request.params.status}`,
686+
// Check if grace period has passed since creation
687+
const createdAt = new Date(request.params.createdAt);
688+
const now = new Date();
689+
const timeSinceCreation = now.getTime() - createdAt.getTime();
690+
691+
if (timeSinceCreation >= UPLOAD_GRACE_PERIOD_MS) {
692+
// Grace period has passed, delete the attribute from DynamoDB
693+
await fastify.dynamoClient.send(
694+
new UpdateItemCommand({
695+
TableName: genericConfig.RoomRequestsStatusTableName,
696+
Key: {
697+
requestId: { S: request.params.requestId },
698+
"createdAt#status": {
699+
S: `${request.params.createdAt}#${request.params.status}`,
700+
},
691701
},
692-
},
693-
UpdateExpression: "REMOVE #attachmentS3key",
694-
ExpressionAttributeNames: {
695-
"#attachmentS3key": "attachmentS3key",
696-
},
697-
}),
698-
);
702+
UpdateExpression: "REMOVE #attachmentS3key",
703+
ExpressionAttributeNames: {
704+
"#attachmentS3key": "attachmentS3key",
705+
},
706+
}),
707+
);
708+
}
699709

700710
throw new NotFoundError({
701711
endpointName: request.url,

src/common/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export const AUDIT_LOG_RETENTION_DAYS = 365;
33
export const ROOM_RESERVATION_RETENTION_DAYS = 1460;
44
export const FULFILLED_PURCHASES_RETENTION_DAYS = 1460; // ticketing/merch: after the purchase is marked as fulfilled.
55
export const EVENTS_EXPIRY_AFTER_LAST_OCCURRENCE_DAYS = 1460; // hold events for 4 years after last occurrence
6+
export const UPLOAD_GRACE_PERIOD_MS = 30 * 60 * 1000; // 30 minutes
67
// we keep data longer for historical analytics purposes in S3 as needed

0 commit comments

Comments
 (0)