Skip to content

Commit 8762db6

Browse files
committed
lambda changes
1 parent 1f61c45 commit 8762db6

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

lambdas/letter-stream-forwarder/src/__tests__/letter-stream-forwarder.test.ts

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('letter-stream-forwarder Lambda', () => {
2626
eventName: 'MODIFY',
2727
dynamodb: {
2828
Keys: { id: { S: '123' } },
29-
OldImage: { status: { S: 'PENDING' } },
29+
OldImage: { status: { S: 'PENDING' }, id: { S: '123' } },
3030
NewImage: { status: { S: 'ACCEPTED' }, id: { S: '123' } },
3131
},
3232
},
@@ -46,14 +46,70 @@ describe('letter-stream-forwarder Lambda', () => {
4646
);
4747
});
4848

49-
it('does not forward if status did not change', async () => {
49+
50+
it('forwards to Kinesis if a reason code is added', async () => {
51+
const event: DynamoDBStreamEvent = {
52+
Records: [
53+
{
54+
eventName: 'MODIFY',
55+
dynamodb: {
56+
Keys: { id: { S: '123' } },
57+
OldImage: { status: { S: 'PENDING' }, id: { S: '123' } },
58+
NewImage: { status: { S: 'PENDING' }, id: { S: '123' }, reasonCode: {S: 'r1'} },
59+
},
60+
},
61+
],
62+
};
63+
64+
const handler = createHandler(mockedDeps);
65+
await handler(event, mockDeep<Context>(), jest.fn());
66+
67+
expect(mockedDeps.kinesisClient.send).toHaveBeenCalledWith(
68+
expect.objectContaining({
69+
input: expect.objectContaining({
70+
StreamName: 'test-stream',
71+
PartitionKey: '123',
72+
}),
73+
})
74+
);
75+
});
76+
77+
78+
it('forwards to Kinesis if a reason code is changed', async () => {
79+
const event: DynamoDBStreamEvent = {
80+
Records: [
81+
{
82+
eventName: 'MODIFY',
83+
dynamodb: {
84+
Keys: { id: { S: '123' } },
85+
OldImage: { status: { S: 'PENDING' }, id: { S: '123' }, reasonCode: {S: 'r1'} },
86+
NewImage: { status: { S: 'PENDING' }, id: { S: '123' }, reasonCode: {S: 'r2'} },
87+
},
88+
},
89+
],
90+
};
91+
92+
const handler = createHandler(mockedDeps);
93+
await handler(event, mockDeep<Context>(), jest.fn());
94+
95+
expect(mockedDeps.kinesisClient.send).toHaveBeenCalledWith(
96+
expect.objectContaining({
97+
input: expect.objectContaining({
98+
StreamName: 'test-stream',
99+
PartitionKey: '123',
100+
}),
101+
})
102+
);
103+
});
104+
105+
it('does not forward if neither status nor reason code changed', async () => {
50106
const event: DynamoDBStreamEvent = {
51107
Records: [
52108
{
53109
eventName: 'MODIFY',
54110
dynamodb: {
55111
Keys: { id: { S: '123' } },
56-
OldImage: { status: { S: 'PENDING' } },
112+
OldImage: { status: { S: 'PENDING' }, id: { S: '123' } },
57113
NewImage: { status: { S: 'PENDING' }, id: { S: '123' } },
58114
},
59115
},
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LetterBase } from "@internal/datastore";
2-
import { DynamoDBStreamEvent, Handler } from "aws-lambda";
2+
import { DynamoDBStreamEvent, Handler, DynamoDBRecord, AttributeValue } from "aws-lambda";
33
import { PutRecordCommand } from "@aws-sdk/client-kinesis";
44
import { Deps } from "./deps";
55
import { unmarshall } from "@aws-sdk/util-dynamodb";
@@ -8,11 +8,7 @@ export function createHandler(deps: Deps): Handler<DynamoDBStreamEvent> {
88
return async (event: DynamoDBStreamEvent): Promise<void> => {
99
const statusChanges = event.Records
1010
.filter(record => record.eventName === "MODIFY")
11-
.filter(record => {
12-
const oldStatus = record.dynamodb?.OldImage?.status?.S;
13-
const newStatus = record.dynamodb?.NewImage?.status?.S;
14-
return oldStatus !== newStatus;
15-
});
11+
.filter(record => isChanged(record, 'status') || isChanged(record, 'reasonCode'));
1612

1713
for (const record of statusChanges) {
1814
const newImage = record.dynamodb?.NewImage!;
@@ -24,4 +20,10 @@ export function createHandler(deps: Deps): Handler<DynamoDBStreamEvent> {
2420
}));
2521
}
2622
};
23+
24+
function isChanged(record: DynamoDBRecord, property: string): boolean {
25+
const oldValue = record.dynamodb?.OldImage![property];
26+
const newValue = record.dynamodb?.NewImage![property];
27+
return oldValue?.S !== newValue?.S;
28+
}
2729
}

0 commit comments

Comments
 (0)