Skip to content

Commit

Permalink
feat: update new Status#CreatedAt field when changing Status in Dynam…
Browse files Browse the repository at this point in the history
…oDB Vault entries
  • Loading branch information
craigzour committed Nov 20, 2024
1 parent 0ae0402 commit 4272287
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ export const getSubmissionsByFormat = async ({
return {
id: item.name,
status: item.status,
createdAt: item.createdAt,
};
});

Expand Down
16 changes: 10 additions & 6 deletions app/api/id/[form]/submission/report/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ async function getSubmissionsFromSubmissionNames(
formId: string,
submissionNames: string[]
): Promise<{
submissionsToReport: { name: string; confirmationCode: string }[];
submissionsToReport: { name: string; createdAt: number; confirmationCode: string }[];
submissionNamesAlreadyUsed: string[];
submissionNamesNotFound: string[];
}> {
const accumulatedSubmissions: {
[name: string]: { status: VaultStatus; confirmationCode: string };
[name: string]: { status: VaultStatus; createdAt: number; confirmationCode: string };
} = {};

let requestedKeys = submissionNames.map((name) => {
Expand All @@ -33,7 +33,7 @@ async function getSubmissionsFromSubmissionNames(
RequestItems: {
Vault: {
Keys: requestedKeys,
ProjectionExpression: "#name,#status,ConfirmationCode",
ProjectionExpression: "#name,#status,ConfirmationCode,CreatedAt",
ExpressionAttributeNames: {
"#name": "Name",
"#status": "Status",
Expand All @@ -49,6 +49,7 @@ async function getSubmissionsFromSubmissionNames(
response.Responses.Vault.forEach((record) => {
accumulatedSubmissions[record["Name"]] = {
status: record["Status"],
createdAt: record["CreatedAt"],
confirmationCode: record["ConfirmationCode"],
};
});
Expand All @@ -75,13 +76,14 @@ async function getSubmissionsFromSubmissionNames(
} else {
acc.submissionsToReport.push({
name: currentSubmissionName,
createdAt: submission.createdAt,
confirmationCode: submission.confirmationCode,
});
}
return acc;
},
{
submissionsToReport: Array<{ name: string; confirmationCode: string }>(),
submissionsToReport: Array<{ name: string; createdAt: number; confirmationCode: string }>(),
submissionNamesAlreadyUsed: Array<string>(),
submissionNamesNotFound: Array<string>(),
}
Expand All @@ -90,7 +92,7 @@ async function getSubmissionsFromSubmissionNames(

async function report(
formId: string,
submissionsToReport: { name: string; confirmationCode: string }[]
submissionsToReport: { name: string; createdAt: number; confirmationCode: string }[]
): Promise<void> {
const request = new TransactWriteCommand({
TransactItems: submissionsToReport.flatMap((submission) => {
Expand All @@ -103,12 +105,14 @@ async function report(
NAME_OR_CONF: `NAME#${submission.name}`,
},
UpdateExpression:
"SET #status = :status, ProblemTimestamp = :problemTimestamp REMOVE RemovalDate",
"SET #status = :status, #statusCreatedAtKey = :statusCreatedAtValue, ProblemTimestamp = :problemTimestamp REMOVE RemovalDate",
ExpressionAttributeNames: {
"#status": "Status",
"#statusCreatedAtKey": "Status#CreatedAt",
},
ExpressionAttributeValues: {
":status": "Problem",
":statusCreatedAtValue": `Problem#${submission.createdAt}`,
":problemTimestamp": Date.now(),
},
},
Expand Down
26 changes: 18 additions & 8 deletions lib/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ export async function retrieveSubmissions(
* @param email Email address of the user downloading the Submission
*/
export async function updateLastDownloadedBy(
responses: Array<{ id: string; status: string }>,
responses: Array<{ id: string; status: string; createdAt: number }>,
formID: string,
email: string
) {
Expand All @@ -407,16 +407,22 @@ export async function updateLastDownloadedBy(
NAME_OR_CONF: `NAME#${response.id}`,
},
UpdateExpression: "SET LastDownloadedBy = :email, DownloadedAt = :downloadedAt".concat(
isNewResponse ? ", #status = :statusUpdate" : ""
isNewResponse
? ", #status = :statusUpdate, #statusCreatedAtKey = :statusCreatedAtValue"
: ""
),
ExpressionAttributeValues: {
":email": email,
":downloadedAt": Date.now(),
...(isNewResponse && { ":statusUpdate": "Downloaded" }),
...(isNewResponse && {
":statusUpdate": "Downloaded",
":statusCreatedAtValue": `Downloaded#${response.createdAt}`,
}),
},
...(isNewResponse && {
ExpressionAttributeNames: {
"#status": "Status",
"#statusCreatedAtKey": "Status#CreatedAt",
},
}),
},
Expand Down Expand Up @@ -577,7 +583,7 @@ async function getSubmissionsFromConfirmationCodes(
formId: string,
confirmationCodes: string[]
): Promise<{
submissionsToConfirm: { name: string; confirmationCode: string }[];
submissionsToConfirm: { name: string; createdAt: number; confirmationCode: string }[];
confirmationCodesAlreadyUsed: string[];
confirmationCodesNotFound: string[];
}> {
Expand All @@ -594,7 +600,7 @@ async function getSubmissionsFromConfirmationCodes(
});

const accumulatedSubmissions: {
[confCode: string]: { name: string; removalDate?: number };
[confCode: string]: { name: string; createdAt: number; removalDate?: number };
} = {};

let requestedKeys = confirmationCodes.map((code) => {
Expand All @@ -606,7 +612,7 @@ async function getSubmissionsFromConfirmationCodes(
RequestItems: {
Vault: {
Keys: requestedKeys,
ProjectionExpression: "#name,ConfirmationCode,RemovalDate",
ProjectionExpression: "#name,CreatedAt,ConfirmationCode,RemovalDate",
ExpressionAttributeNames: {
"#name": "Name",
},
Expand All @@ -621,6 +627,7 @@ async function getSubmissionsFromConfirmationCodes(
response.Responses.Vault.forEach((record) => {
accumulatedSubmissions[record["ConfirmationCode"]] = {
name: record["Name"],
createdAt: record["CreatedAt"],
removalDate: record["RemovalDate"],
};
});
Expand All @@ -647,13 +654,14 @@ async function getSubmissionsFromConfirmationCodes(
} else {
acc.submissionsToConfirm.push({
name: submission.name,
createdAt: submission.createdAt,
confirmationCode: currentConfirmationCode,
});
}
return acc;
},
{
submissionsToConfirm: Array<{ name: string; confirmationCode: string }>(),
submissionsToConfirm: Array<{ name: string; createdAt: number; confirmationCode: string }>(),
confirmationCodesAlreadyUsed: Array<string>(),
confirmationCodesNotFound: Array<string>(),
}
Expand Down Expand Up @@ -724,12 +732,14 @@ export const confirmResponses = async (
NAME_OR_CONF: `NAME#${submission.name}`,
},
UpdateExpression:
"SET #status = :status, ConfirmTimestamp = :confirmTimestamp, RemovalDate = :removalDate",
"SET #status = :status, #statusCreatedAtKey = :statusCreatedAtValue, ConfirmTimestamp = :confirmTimestamp, RemovalDate = :removalDate",
ExpressionAttributeNames: {
"#status": "Status",
"#statusCreatedAtKey": "Status#CreatedAt",
},
ExpressionAttributeValues: {
":status": "Confirmed",
":statusCreatedAtValue": `Confirmed#${submission.createdAt}`,
":confirmTimestamp": confirmationTimestamp,
":removalDate": removalDate,
},
Expand Down

0 comments on commit 4272287

Please sign in to comment.