Skip to content

Commit 917a3da

Browse files
authored
refactor: replace use of Status with new Status#CreatedAt attribute when requesting Vault items (#116)
1 parent ca13d75 commit 917a3da

File tree

7 files changed

+38
-22
lines changed

7 files changed

+38
-22
lines changed

src/lib/vault/confirmFormSubmission.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@ export async function confirmFormSubmission(
4343
NAME_OR_CONF: `NAME#${submissionName}`,
4444
},
4545
UpdateExpression:
46-
"SET #status = :status, #statusCreatedAtKey = :statusCreatedAtValue, ConfirmTimestamp = :confirmTimestamp, RemovalDate = :removalDate",
46+
"SET #statusCreatedAtKey = :statusCreatedAtValue, ConfirmTimestamp = :confirmTimestamp, RemovalDate = :removalDate",
4747
ExpressionAttributeNames: {
48-
"#status": "Status",
4948
"#statusCreatedAtKey": "Status#CreatedAt",
5049
},
5150
ExpressionAttributeValues: {
52-
":status": "Confirmed",
5351
":statusCreatedAtValue": `Confirmed#${formSubmission.createdAt}`,
5452
":confirmTimestamp": confirmationTimestamp,
5553
":removalDate": removalDate,

src/lib/vault/getFormSubmission.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { GetCommand } from "@aws-sdk/lib-dynamodb";
22
import { AwsServicesConnector } from "@lib/integration/awsServicesConnector.js";
3-
import type {
4-
FormSubmission,
3+
import {
4+
type FormSubmission,
55
FormSubmissionStatus,
66
} from "@lib/vault/types/formSubmission.js";
77
import { logMessage } from "@lib/logging/logger.js";
@@ -17,9 +17,9 @@ export async function getFormSubmission(
1717
TableName: "Vault",
1818
Key: { FormID: formId, NAME_OR_CONF: `NAME#${submissionName}` },
1919
ProjectionExpression:
20-
"CreatedAt,#status,ConfirmationCode,FormSubmission,FormSubmissionHash",
20+
"CreatedAt,#statusCreatedAtKey,ConfirmationCode,FormSubmission,FormSubmissionHash",
2121
ExpressionAttributeNames: {
22-
"#status": "Status",
22+
"#statusCreatedAtKey": "Status#CreatedAt",
2323
},
2424
}),
2525
);
@@ -44,9 +44,32 @@ function formSubmissionFromDynamoDbResponse(
4444
): FormSubmission {
4545
return {
4646
createdAt: response.CreatedAt as number,
47-
status: response.Status as FormSubmissionStatus,
47+
status: formSubmissionStatusFromStatusCreatedAt(
48+
response["Status#CreatedAt"] as string,
49+
),
4850
confirmationCode: response.ConfirmationCode as string,
4951
answers: response.FormSubmission as string,
5052
checksum: response.FormSubmissionHash as string,
5153
};
5254
}
55+
56+
function formSubmissionStatusFromStatusCreatedAt(
57+
statusCreatedAtValue: string,
58+
): FormSubmissionStatus {
59+
const status = statusCreatedAtValue.split("#")[0];
60+
61+
switch (status) {
62+
case "New":
63+
return FormSubmissionStatus.New;
64+
case "Downloaded":
65+
return FormSubmissionStatus.Downloaded;
66+
case "Confirmed":
67+
return FormSubmissionStatus.Confirmed;
68+
case "Problem":
69+
return FormSubmissionStatus.Problem;
70+
default:
71+
throw new Error(
72+
`Unsupported Status#CreatedAt value. Value = ${statusCreatedAtValue}.`,
73+
);
74+
}
75+
}

src/lib/vault/reportProblemWithFormSubmission.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@ export async function reportProblemWithFormSubmission(
3131
NAME_OR_CONF: `NAME#${submissionName}`,
3232
},
3333
UpdateExpression:
34-
"SET #status = :status, #statusCreatedAtKey = :statusCreatedAtValue, ProblemTimestamp = :problemTimestamp REMOVE RemovalDate",
34+
"SET #statusCreatedAtKey = :statusCreatedAtValue, ProblemTimestamp = :problemTimestamp REMOVE RemovalDate",
3535
ExpressionAttributeNames: {
36-
"#status": "Status",
3736
"#statusCreatedAtKey": "Status#CreatedAt",
3837
},
3938
ExpressionAttributeValues: {
40-
":status": "Problem",
4139
":statusCreatedAtValue": `Problem#${formSubmission.createdAt}`,
4240
":problemTimestamp": Date.now(),
4341
},

test/lib/vault/confirmFormSubmission.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@ describe("confirmFormSubmission should", () => {
4545
expect(dynamoDbMock.commandCalls(UpdateCommand).length).toEqual(1);
4646
expect(dynamoDbMock.commandCalls(UpdateCommand)[0].args[0].input).toEqual({
4747
ExpressionAttributeNames: {
48-
"#status": "Status",
4948
"#statusCreatedAtKey": "Status#CreatedAt",
5049
},
5150
ExpressionAttributeValues: {
5251
":confirmTimestamp": 1519129853500,
5352
":removalDate": 1521721853500,
54-
":status": "Confirmed",
5553
":statusCreatedAtValue": "Confirmed#1519129853500",
5654
},
5755
Key: {
@@ -60,7 +58,7 @@ describe("confirmFormSubmission should", () => {
6058
},
6159
TableName: "Vault",
6260
UpdateExpression:
63-
"SET #status = :status, #statusCreatedAtKey = :statusCreatedAtValue, ConfirmTimestamp = :confirmTimestamp, RemovalDate = :removalDate",
61+
"SET #statusCreatedAtKey = :statusCreatedAtValue, ConfirmTimestamp = :confirmTimestamp, RemovalDate = :removalDate",
6462
});
6563
});
6664

test/lib/vault/getFormSubmission.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DynamoDBDocumentClient, GetCommand } from "@aws-sdk/lib-dynamodb";
44
import { getFormSubmission } from "@lib/vault/getFormSubmission.js";
55
import { FormSubmissionStatus } from "@lib/vault/types/formSubmission.js";
66
import { logMessage } from "@lib/logging/logger.js";
7+
import { buildMockedVaultItem } from "test/mocks/dynamodb.js";
78

89
const dynamoDbMock = mockClient(DynamoDBDocumentClient);
910

@@ -27,9 +28,7 @@ describe("getFormSubmission should", () => {
2728

2829
it("return a form submission if DynamoDB was able to find it", async () => {
2930
dynamoDbMock.on(GetCommand).resolvesOnce({
30-
Item: {
31-
Status: "New",
32-
},
31+
Item: buildMockedVaultItem("New"),
3332
});
3433

3534
const formSubmission = await getFormSubmission(

test/lib/vault/reportProblemWithFormSubmission.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,10 @@ describe("reportProblemWithFormSubmission should", () => {
4343
expect(dynamoDbMock.commandCalls(UpdateCommand).length).toEqual(1);
4444
expect(dynamoDbMock.commandCalls(UpdateCommand)[0].args[0].input).toEqual({
4545
ExpressionAttributeNames: {
46-
"#status": "Status",
4746
"#statusCreatedAtKey": "Status#CreatedAt",
4847
},
4948
ExpressionAttributeValues: {
5049
":problemTimestamp": 1519129853500,
51-
":status": "Problem",
5250
":statusCreatedAtValue": "Problem#1519129853500",
5351
},
5452
Key: {
@@ -57,7 +55,7 @@ describe("reportProblemWithFormSubmission should", () => {
5755
},
5856
TableName: "Vault",
5957
UpdateExpression:
60-
"SET #status = :status, #statusCreatedAtKey = :statusCreatedAtValue, ProblemTimestamp = :problemTimestamp REMOVE RemovalDate",
58+
"SET #statusCreatedAtKey = :statusCreatedAtValue, ProblemTimestamp = :problemTimestamp REMOVE RemovalDate",
6159
});
6260
});
6361

test/mocks/dynamodb.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ export function buildMockedVaultItem(
22
status = "New",
33
confirmationCode = "620b203c-9836-4000-bf30-1c3bcc26b834",
44
): Record<string, unknown> {
5+
const createdAt = Date.now();
6+
57
return {
6-
CreatedAt: Date.now(),
7-
Status: status,
8+
CreatedAt: createdAt,
9+
"Status#CreatedAt": `${status}#${createdAt}`,
810
ConfirmationCode: confirmationCode,
911
FormSubmission: '{"1":"Test response"}',
1012
};

0 commit comments

Comments
 (0)