-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpatch-letter.ts
More file actions
117 lines (105 loc) · 3.52 KB
/
patch-letter.ts
File metadata and controls
117 lines (105 loc) · 3.52 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
import { APIGatewayProxyHandler } from "aws-lambda";
import { MetricsLogger, Unit, metricScope } from "aws-embedded-metrics";
import { MetricStatus } from "@internal/helpers";
import { enqueueLetterUpdateRequests } from "../services/letter-operations";
import {
PatchLetterRequest,
PatchLetterRequestSchema,
UpdateLetterCommand,
} from "../contracts/letters";
import { ApiErrorDetail } from "../contracts/errors";
import ValidationError from "../errors/validation-error";
import { processError } from "../mappers/error-mapper";
import { assertNotEmpty } from "../utils/validation";
import { extractCommonIds } from "../utils/common-ids";
import { mapToUpdateCommand } from "../mappers/letter-mapper";
import type { Deps } from "../config/deps";
export default function createPatchLetterHandler(
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;
metrics.setNamespace(
process.env.AWS_LAMBDA_FUNCTION_NAME || "patchLetters",
);
try {
const letterId = assertNotEmpty(
event.pathParameters?.id,
new ValidationError(
ApiErrorDetail.InvalidRequestMissingLetterIdPathParameter,
),
);
const body = assertNotEmpty(
event.body,
new ValidationError(ApiErrorDetail.InvalidRequestMissingBody),
);
let patchLetterRequest: PatchLetterRequest;
try {
patchLetterRequest = PatchLetterRequestSchema.parse(JSON.parse(body));
} catch (error) {
emitErrorMetric(metrics, supplierId);
const typedError =
error instanceof Error
? new ValidationError(ApiErrorDetail.InvalidRequestBody, {
cause: error,
})
: error;
throw typedError;
}
deps.logger.info({
description: "Received patch letter request",
supplierId: commonIds.value.supplierId,
letterId,
newStatus: patchLetterRequest.data.attributes.status,
correlationId: commonIds.value.correlationId,
});
const updateLetterCommand: UpdateLetterCommand = mapToUpdateCommand(
patchLetterRequest,
supplierId,
);
if (updateLetterCommand.id !== letterId) {
emitErrorMetric(metrics, supplierId);
throw new ValidationError(
ApiErrorDetail.InvalidRequestLetterIdsMismatch,
);
}
await enqueueLetterUpdateRequests(
[updateLetterCommand],
commonIds.value.correlationId,
deps,
);
metrics.putDimensions({
supplier: supplierId,
status: updateLetterCommand.status,
});
metrics.putMetric(MetricStatus.Success, 1, Unit.Count);
return {
statusCode: 202,
body: "",
};
} catch (error) {
emitErrorMetric(metrics, supplierId);
return processError(error, commonIds.value.correlationId, deps.logger);
}
};
});
}
function emitErrorMetric(metrics: MetricsLogger, supplierId: string) {
metrics.putDimensions({
supplier: supplierId,
});
metrics.putMetric(MetricStatus.Failure, 1, Unit.Count);
}