-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformSubmission.mapper.ts
More file actions
170 lines (155 loc) · 5.61 KB
/
Copy pathformSubmission.mapper.ts
File metadata and controls
170 lines (155 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { logMessage } from "@lib/logging/logger.js";
import {
AttachmentScanStatus,
type FormSubmission,
type NewFormSubmission,
type PartialAttachment,
SubmissionStatus,
} from "@lib/vault/types/formSubmission.types.js";
export function mapNewFormSubmissionFromDynamoDbResponse(
response: Record<string, unknown>,
): NewFormSubmission {
try {
// response.Version could be undefined if API users are retrieving responses that have been created before we implemented the form versioning feature
if (response.Name === undefined || response.CreatedAt === undefined) {
throw new Error("Missing key properties in DynamoDB response");
}
if (
typeof response.Name !== "string" ||
typeof response.CreatedAt !== "number" ||
// response.Version could be undefined if API users are retrieving responses that have been created before we implemented the form versioning feature
(response.Version !== undefined && typeof response.Version !== "number")
) {
throw new Error("Unexpected type in DynamoDB response");
}
return {
name: response.Name,
createdAt: response.CreatedAt,
version: response.Version ?? 1,
};
} catch (error) {
logMessage.info(
error,
"[mapper] Failed to map new form submission from DynamoDB response",
);
throw error;
}
}
export function mapFormSubmissionFromDynamoDbResponse(
response: Record<string, unknown>,
): FormSubmission {
try {
if (
response.CreatedAt === undefined ||
response["Status#CreatedAt"] === undefined ||
response.ConfirmationCode === undefined ||
response.FormSubmission === undefined ||
response.FormSubmissionHash === undefined
// response.SubmissionAttachments could be undefined if API users are retrieving responses that have been created before we implemented the submission attachments feature
// response.Version could be undefined if API users are retrieving responses that have been created before we implemented the form versioning feature
) {
throw new Error("Missing key properties in DynamoDB response");
}
if (
typeof response.CreatedAt !== "number" ||
typeof response["Status#CreatedAt"] !== "string" ||
typeof response.ConfirmationCode !== "string" ||
typeof response.FormSubmission !== "string" ||
typeof response.FormSubmissionHash !== "string" ||
// response.SubmissionAttachments could be undefined if API users are retrieving responses that have been created before we implemented the submission attachments feature
(response.SubmissionAttachments !== undefined &&
typeof response.SubmissionAttachments !== "string") ||
// response.Version could be undefined if API users are retrieving responses that have been created before we implemented the form versioning feature
(response.Version !== undefined && typeof response.Version !== "number")
) {
throw new Error("Unexpected type in DynamoDB response");
}
return {
createdAt: response.CreatedAt,
status: submissionStatusFromStatusCreatedAt(response["Status#CreatedAt"]),
confirmationCode: response.ConfirmationCode,
answers: response.FormSubmission,
checksum: response.FormSubmissionHash,
attachments: response.SubmissionAttachments
? partialAttachmentFromSubmissionAttachmentsAsJson(
response.SubmissionAttachments,
)
: [],
version: response.Version ?? 1,
};
} catch (error) {
logMessage.info(
error,
"[mapper] Failed to map form submission from DynamoDB response",
);
throw error;
}
}
function submissionStatusFromStatusCreatedAt(
statusCreatedAtValue: string,
): SubmissionStatus {
const status = statusCreatedAtValue.split("#").shift();
switch (status) {
case "New":
return SubmissionStatus.new;
case "Downloaded":
return SubmissionStatus.downloaded;
case "Confirmed":
return SubmissionStatus.confirmed;
case "Problem":
return SubmissionStatus.problem;
default:
throw new Error(
`Unsupported Status#CreatedAt value. Value = ${statusCreatedAtValue}.`,
);
}
}
function partialAttachmentFromSubmissionAttachmentsAsJson(
submissionAttachmentsAsJson: string,
): PartialAttachment[] {
const attachments: Record<string, unknown>[] = JSON.parse(
submissionAttachmentsAsJson,
);
return attachments.map((item) => {
if (
item.id === undefined ||
item.name === undefined ||
item.path === undefined ||
item.scanStatus === undefined
) {
throw new Error("Missing key properties in submission attachment JSON");
}
if (
(typeof item.md5 !== "string" && typeof item.md5 !== "undefined") ||
typeof item.id !== "string" ||
typeof item.name !== "string" ||
typeof item.path !== "string" ||
typeof item.scanStatus !== "string"
) {
throw new Error("Unexpected type in submission attachment JSON");
}
return {
id: item.id,
name: item.name,
path: item.path,
scanStatus: attachmentScanStatusFromScanStatus(item.scanStatus),
md5: item.md5,
};
});
}
function attachmentScanStatusFromScanStatus(
scanStatus: string,
): AttachmentScanStatus {
switch (scanStatus) {
case "NO_THREATS_FOUND":
return AttachmentScanStatus.noThreatsFound;
case "THREATS_FOUND":
return AttachmentScanStatus.threatsFound;
case "UNSUPPORTED":
return AttachmentScanStatus.unsupported;
case "FAILED":
return AttachmentScanStatus.failed;
default:
throw new Error(`Unsupported scan status value. Value = ${scanStatus}.`);
}
}