Skip to content

Commit e42d26c

Browse files
authored
CCM-13697: Letters Key and Source Data Item (#323)
* swap partition and sort for letters * store changes and source, subject, billingref * unsafe marker * rename correctly
1 parent 547f6b1 commit e42d26c

File tree

28 files changed

+80
-23
lines changed

28 files changed

+80
-23
lines changed

infrastructure/terraform/components/api/ddb_table_letters.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ resource "aws_dynamodb_table" "letters" {
22
name = "${local.csi}-letters"
33
billing_mode = "PAY_PER_REQUEST"
44

5-
hash_key = "supplierId"
6-
range_key = "id"
5+
hash_key = "id"
6+
range_key = "supplierId"
77

88
ttl {
99
attribute_name = "ttl"

internal/datastore/src/__test__/db.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ const createLetterTableCommand = new CreateTableCommand({
5151
TableName: "letters",
5252
BillingMode: "PAY_PER_REQUEST",
5353
KeySchema: [
54-
{ AttributeName: "supplierId", KeyType: "HASH" }, // Partition key
55-
{ AttributeName: "id", KeyType: "RANGE" }, // Sort key
54+
{ AttributeName: "id", KeyType: "HASH" }, // Partition key (letter ID)
55+
{ AttributeName: "supplierId", KeyType: "RANGE" }, // Sort key
5656
],
5757
GlobalSecondaryIndexes: [
5858
{

internal/datastore/src/__test__/heathcheck.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ describe("DBHealthcheck", () => {
2424
await deleteTables(db);
2525
});
2626

27+
afterAll(async () => {
28+
await db.container.stop();
29+
});
30+
2731
it("passes when the database is available", async () => {
2832
const dbHealthCheck = new DBHealthcheck(db.docClient, db.config);
2933
await expect(dbHealthCheck.check()).resolves.not.toThrow();

internal/datastore/src/__test__/letter-repository.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ function createLetter(
2727
updatedAt: now,
2828
source: "/data-plane/letter-rendering/pdf",
2929
subject: `client/1/letter-request/${letterId}`,
30+
billingRef: "specification1",
3031
};
3132
}
3233

@@ -112,6 +113,7 @@ describe("LetterRepository", () => {
112113
expect(letter.reasonCode).toBeUndefined();
113114
expect(letter.reasonText).toBeUndefined();
114115
expect(letter.subject).toBe(`client/1/letter-request/${letterId}`);
116+
expect(letter.billingRef).toBe("specification1");
115117
assertTtl(letter.ttl, before, after);
116118
});
117119

@@ -443,7 +445,7 @@ describe("LetterRepository", () => {
443445
test("should batch write letters to the database", async () => {
444446
const before = Date.now();
445447

446-
await letterRepository.putLetterBatch([
448+
await letterRepository.unsafePutLetterBatch([
447449
createLetter("supplier1", "letter1"),
448450
createLetter("supplier1", "letter2"),
449451
createLetter("supplier1", "letter3"),
@@ -483,7 +485,7 @@ describe("LetterRepository", () => {
483485

484486
const sendSpy = jest.spyOn(db.docClient, "send");
485487

486-
await letterRepository.putLetterBatch(letters);
488+
await letterRepository.unsafePutLetterBatch(letters);
487489

488490
expect(sendSpy).toHaveBeenCalledTimes(3);
489491

@@ -497,7 +499,7 @@ describe("LetterRepository", () => {
497499
letters[0] = createLetter("supplier1", "letter1");
498500
letters[2] = createLetter("supplier1", "letter3");
499501

500-
await letterRepository.putLetterBatch(letters);
502+
await letterRepository.unsafePutLetterBatch(letters);
501503

502504
await checkLetterStatus("supplier1", "letter1", "PENDING");
503505
await checkLetterStatus("supplier1", "letter3", "PENDING");
@@ -509,7 +511,7 @@ describe("LetterRepository", () => {
509511
lettersTableName: "nonexistent-table",
510512
});
511513
await expect(
512-
misconfiguredRepository.putLetterBatch([
514+
misconfiguredRepository.unsafePutLetterBatch([
513515
createLetter("supplier1", "letter1"),
514516
]),
515517
).rejects.toThrow("Cannot do operations on a non-existent table");

internal/datastore/src/letter-repository.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class LetterRepository {
7070
return LetterSchema.parse(letterDb);
7171
}
7272

73-
async putLetterBatch(letters: InsertLetter[]): Promise<void> {
73+
async unsafePutLetterBatch(letters: InsertLetter[]): Promise<void> {
7474
let lettersDb: Letter[] = [];
7575
for (let i = 0; i < letters.length; i++) {
7676
const letter = letters[i];
@@ -109,8 +109,8 @@ export class LetterRepository {
109109
new GetCommand({
110110
TableName: this.config.lettersTableName,
111111
Key: {
112-
supplierId,
113112
id: letterId,
113+
supplierId,
114114
},
115115
}),
116116
);
@@ -194,8 +194,8 @@ export class LetterRepository {
194194
new UpdateCommand({
195195
TableName: this.config.lettersTableName,
196196
Key: {
197-
supplierId: letterToUpdate.supplierId,
198197
id: letterToUpdate.id,
198+
supplierId: letterToUpdate.supplierId,
199199
},
200200
UpdateExpression: updateExpression,
201201
ConditionExpression: "attribute_exists(id)", // Ensure letter exists

internal/datastore/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export const LetterSchema = LetterSchemaBase.extend({
5050
ttl: z.int(),
5151
source: z.string(),
5252
subject: z.string(),
53+
billingRef: z.string(),
5354
}).describe("Letter");
5455

5556
/**

internal/events/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@
5050
"typecheck": "tsc --noEmit"
5151
},
5252
"types": "dist/index.d.ts",
53-
"version": "1.0.5"
53+
"version": "1.0.6"
5454
}

internal/events/schemas/examples/letter.ACCEPTED.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"data": {
3+
"billingRef": "1y3q9v1zzzz",
34
"domainId": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
45
"groupId": "client_template",
56
"origin": {

internal/events/schemas/examples/letter.FORWARDED.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"data": {
3+
"billingRef": "1y3q9v1zzzz",
34
"domainId": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
45
"groupId": "client_template",
56
"origin": {

internal/events/schemas/examples/letter.RETURNED.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"data": {
3+
"billingRef": "1y3q9v1zzzz",
34
"domainId": "f47ac10b-58cc-4372-a567-0e02b2c3d479",
45
"groupId": "client_template",
56
"origin": {

0 commit comments

Comments
 (0)