Skip to content

Commit f022bf9

Browse files
fix failing unit tests
1 parent 038d755 commit f022bf9

File tree

6 files changed

+69
-29
lines changed

6 files changed

+69
-29
lines changed

lambdas/api-handler/src/handlers/post-letters.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { APIGatewayProxyHandler } from "aws-lambda";
2-
import { Metrics$ } from "@aws-sdk/client-s3";
32
import { MetricsLogger, Unit, metricScope } from "aws-embedded-metrics";
43
import type { Deps } from "../config/deps";
54
import { ApiErrorDetail } from "../contracts/errors";
@@ -98,10 +97,9 @@ export default function createPostLettersHandler(
9897

9998
const statusesMapping = new Map<string, number>();
10099
await enqueueLetterUpdateRequests(
101-
mapToUpdateCommands(postLettersRequest, supplierId),
100+
mapToUpdateCommands(postLettersRequest, supplierId, statusesMapping),
102101
commonIds.value.correlationId,
103102
deps,
104-
statusesMapping,
105103
);
106104

107105
await emitMetics(metrics, supplierId, statusesMapping);

lambdas/api-handler/src/handlers/post-mi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { APIGatewayProxyHandler } from "aws-lambda";
2+
import { MetricsLogger, metricScope } from "aws-embedded-metrics";
23
import postMIOperation from "../services/mi-operations";
34
import { ApiErrorDetail } from "../contracts/errors";
45
import ValidationError from "../errors/validation-error";
@@ -8,8 +9,7 @@ import { extractCommonIds } from "../utils/common-ids";
89
import { PostMIRequest, PostMIRequestSchema } from "../contracts/mi";
910
import { mapToMI } from "../mappers/mi-mapper";
1011
import { Deps } from "../config/deps";
11-
import { metricScope, MetricsLogger } from "aws-embedded-metrics";
12-
import { emitForSingleSupplier, MetricStatus } from "../utils/metrics";
12+
import { MetricStatus, emitForSingleSupplier } from "../utils/metrics";
1313

1414
export default function createPostMIHandler(
1515
deps: Deps,

lambdas/api-handler/src/mappers/__tests__/letter-mapper.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,55 @@ import {
33
mapToGetLetterResponse,
44
mapToGetLettersResponse,
55
mapToPatchLetterResponse,
6+
mapToUpdateCommands,
67
} from "../letter-mapper";
78
import {
89
GetLetterResponse,
910
GetLettersResponse,
1011
PatchLetterResponse,
12+
PostLettersRequest,
1113
} from "../../contracts/letters";
1214

1315
describe("letter-mapper", () => {
16+
it("maps PostLetterRequest to UpdateLetterCommands", () => {
17+
const request: PostLettersRequest = {
18+
data: [
19+
{
20+
id: "id1",
21+
type: "Letter",
22+
attributes: {
23+
status: "REJECTED",
24+
reasonCode: "123",
25+
reasonText: "Reason text",
26+
},
27+
},
28+
{ id: "id2", type: "Letter", attributes: { status: "ACCEPTED" } },
29+
{ id: "id3", type: "Letter", attributes: { status: "DELIVERED" } },
30+
],
31+
};
32+
const supplierId = "testSupplierId";
33+
const updateLetterCommands = mapToUpdateCommands(request, supplierId);
34+
expect(updateLetterCommands).toEqual([
35+
{
36+
id: "id1",
37+
reasonCode: "123",
38+
reasonText: "Reason text",
39+
supplierId: "testSupplierId",
40+
status: "REJECTED",
41+
},
42+
{
43+
id: "id2",
44+
supplierId: "testSupplierId",
45+
status: "ACCEPTED",
46+
},
47+
{
48+
id: "id3",
49+
supplierId: "testSupplierId",
50+
status: "DELIVERED",
51+
},
52+
]);
53+
});
54+
1455
it("maps an internal Letter to a PatchLetterResponse", () => {
1556
const date = new Date().toISOString();
1657
const letter: Letter = {

lambdas/api-handler/src/mappers/letter-mapper.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,24 @@ export function mapToUpdateCommand(
5858
export function mapToUpdateCommands(
5959
request: PostLettersRequest,
6060
supplierId: string,
61+
statusesMapping?: Map<string, number>,
6162
): UpdateLetterCommand[] {
62-
return request.data.map((letterToUpdate: PostLettersRequestResource) => ({
63-
id: letterToUpdate.id,
64-
supplierId,
65-
status: LetterStatus.parse(letterToUpdate.attributes.status),
66-
reasonCode: letterToUpdate.attributes.reasonCode,
67-
reasonText: letterToUpdate.attributes.reasonText,
68-
}));
63+
return request.data.map((letterToUpdate: PostLettersRequestResource) => {
64+
const letterCommand = {
65+
id: letterToUpdate.id,
66+
supplierId,
67+
status: LetterStatus.parse(letterToUpdate.attributes.status),
68+
reasonCode: letterToUpdate.attributes.reasonCode,
69+
reasonText: letterToUpdate.attributes.reasonText,
70+
};
71+
if (statusesMapping) {
72+
statusesMapping.set(
73+
letterCommand.status,
74+
(statusesMapping.get(letterCommand.status) || 0) + 1,
75+
);
76+
}
77+
return letterCommand;
78+
});
6979
}
7080

7181
// ---------------------------------------------

lambdas/api-handler/src/services/letter-operations.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ export async function enqueueLetterUpdateRequests(
9595
updateLetterCommands: UpdateLetterCommand[],
9696
correlationId: string,
9797
deps: Deps,
98-
statusesMapping?: Map<string, number>,
9998
) {
10099
const BATCH_SIZE = 10; // SQS SendMessageBatch max
101100
const CONCURRENCY = 5; // number of parallel batch API calls
@@ -109,21 +108,13 @@ export async function enqueueLetterUpdateRequests(
109108

110109
await Promise.all(
111110
window.map(async (batch, batchIdx) => {
112-
const entries = batch.map((request, idx) => {
113-
if (statusesMapping) {
114-
statusesMapping.set(
115-
request.status,
116-
(statusesMapping.get(request.status) || 0) + 1,
117-
);
118-
}
119-
return {
120-
Id: `${i + batchIdx}-${idx}`, // unique per batch entry
121-
MessageBody: JSON.stringify(request),
122-
MessageAttributes: {
123-
CorrelationId: { DataType: "String", StringValue: correlationId },
124-
},
125-
};
126-
});
111+
const entries = batch.map((request, idx) => ({
112+
Id: `${i + batchIdx}-${idx}`, // unique per batch entry
113+
MessageBody: JSON.stringify(request),
114+
MessageAttributes: {
115+
CorrelationId: { DataType: "String", StringValue: correlationId },
116+
},
117+
}));
127118

128119
const cmd = new SendMessageBatchCommand({
129120
QueueUrl: deps.env.QUEUE_URL,

lambdas/api-handler/src/utils/metrics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function emitForSingleSupplier(
1616
metrics.putMetric(message, count, Unit.Count);
1717
}
1818

19-
export declare enum MetricStatus {
19+
export enum MetricStatus {
2020
Success = "success",
2121
Failure = "failure",
2222
}

0 commit comments

Comments
 (0)