-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathretrieveSubmission.v1.ts
More file actions
132 lines (119 loc) · 3.87 KB
/
Copy pathretrieveSubmission.v1.ts
File metadata and controls
132 lines (119 loc) · 3.87 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
import { encryptResponse } from "@lib/encryption/encryptResponse.js";
import { getPublicKey } from "@lib/formsClient/getPublicKey.js";
import { auditLog } from "@lib/logging/auditLogs.js";
import {
RequestContextualStoreKey,
retrieveRequestContextData,
} from "@lib/storage/requestContextualStore.js";
import { getFormSubmission } from "@lib/vault/getFormSubmission.js";
import { getFormSubmissionAttachmentDownloadLink } from "@lib/vault/getFormSubmissionAttachmentDownloadLink.js";
import { FormSubmissionNotFoundException } from "@lib/vault/types/exceptions.types.js";
import {
AttachmentScanStatus,
type FormSubmission,
type PartialAttachment,
} from "@lib/vault/types/formSubmission.types.js";
import type { ApiOperation } from "@operations/types/operation.js";
import type { NextFunction, Request, Response } from "express";
type CompleteAttachment = PartialAttachment & {
downloadLink: string;
};
async function v1(
request: Request,
response: Response,
next: NextFunction,
): Promise<void> {
const formId = request.params.formId;
const submissionName = request.params.submissionName;
const serviceUserId = retrieveRequestContextData(
RequestContextualStoreKey.serviceUserId,
);
const serviceAccountId = retrieveRequestContextData(
RequestContextualStoreKey.serviceAccountId,
);
try {
const formSubmission = await getFormSubmission(formId, submissionName);
const attachments =
formSubmission.attachments.length > 0
? await getCompleteFormSubmissionAttachments(formSubmission.attachments)
: undefined;
const responsePayload = buildJsonResponse(formSubmission, attachments);
const serviceAccountPublicKey = await getPublicKey(serviceAccountId);
const encryptedResponse = encryptResponse(
serviceAccountPublicKey,
responsePayload,
);
auditLog({
userId: serviceUserId,
subject: { type: "Response", id: submissionName },
event: "DownloadResponse",
});
response.json(encryptedResponse);
} catch (error) {
switch ((error as Error).constructor) {
case FormSubmissionNotFoundException:
response.status(404).json({ error: "Form submission does not exist" });
break;
default: {
next(
new Error(
`[operation] Internal error while retrieving submission. Params: formId = ${formId} ; submissionName = ${submissionName}`,
{ cause: error },
),
);
break;
}
}
}
}
function getCompleteFormSubmissionAttachments(
partialAttachments: PartialAttachment[],
): Promise<CompleteAttachment[]> {
return Promise.all(
partialAttachments.map((partialAttachment) => {
return getFormSubmissionAttachmentDownloadLink(
partialAttachment.path,
).then((downloadLink) => {
return { ...partialAttachment, downloadLink };
});
}),
);
}
function buildJsonResponse(
formSubmission: FormSubmission,
attachments: CompleteAttachment[] | undefined,
): string {
return JSON.stringify({
createdAt: formSubmission.createdAt,
status: formSubmission.status,
confirmationCode: formSubmission.confirmationCode,
answers: formSubmission.answers,
checksum: formSubmission.checksum,
...(attachments && {
attachments: attachments.map((attachment) => ({
id: attachment.id,
name: attachment.name,
downloadLink: attachment.downloadLink,
isPotentiallyMalicious: isAttachmentPotentiallyMalicious(
attachment.scanStatus,
),
md5: attachment.md5,
})),
}),
version: formSubmission.version,
});
}
function isAttachmentPotentiallyMalicious(
scanStatus: AttachmentScanStatus,
): boolean {
switch (scanStatus) {
case AttachmentScanStatus.noThreatsFound:
return false;
default:
return true;
}
}
export const retrieveSubmissionOperationV1: ApiOperation = {
middleware: [],
handler: v1,
};