-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathget-letter-data.ts
More file actions
75 lines (69 loc) · 2.1 KB
/
get-letter-data.ts
File metadata and controls
75 lines (69 loc) · 2.1 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
import { APIGatewayProxyHandler } from "aws-lambda";
import { MetricsLogger, metricScope } from "aws-embedded-metrics";
import { MetricStatus, emitForSingleSupplier } from "@internal/helpers";
import { assertNotEmpty } from "../utils/validation";
import { extractCommonIds } from "../utils/common-ids";
import { ApiErrorDetail } from "../contracts/errors";
import { processError } from "../mappers/error-mapper";
import ValidationError from "../errors/validation-error";
import { getLetterDataUrl } from "../services/letter-operations";
import type { Deps } from "../config/deps";
export default function createGetLetterDataHandler(
deps: Deps,
): APIGatewayProxyHandler {
return metricScope((metrics: MetricsLogger) => {
return async (event) => {
const commonIds = extractCommonIds(
event.headers,
event.requestContext,
deps,
);
if (!commonIds.ok) {
return processError(
commonIds.error,
commonIds.correlationId,
deps.logger,
);
}
const { supplierId } = commonIds.value;
try {
const letterId = assertNotEmpty(
event.pathParameters?.id,
new ValidationError(
ApiErrorDetail.InvalidRequestMissingLetterIdPathParameter,
),
);
const presignedUrl = await getLetterDataUrl(supplierId, letterId, deps);
deps.logger.info({
description: "Generated presigned URL",
supplierId,
letterId,
correlationId: commonIds.value.correlationId,
});
emitForSingleSupplier(
metrics,
"getLetterData",
supplierId,
1,
MetricStatus.Success,
);
return {
statusCode: 303,
headers: {
Location: presignedUrl,
},
body: "",
};
} catch (error) {
emitForSingleSupplier(
metrics,
"getLetterData",
supplierId,
1,
MetricStatus.Failure,
);
return processError(error, commonIds.value.correlationId, deps.logger);
}
};
});
}