Skip to content

Commit 40fbb3f

Browse files
committed
CCM-13906 Urgent Letter Priority
1 parent b642dac commit 40fbb3f

5 files changed

Lines changed: 57 additions & 15 deletions

File tree

infrastructure/terraform/components/api/ddb_table_letter_queue.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ resource "aws_dynamodb_table" "letter_queue" {
1212

1313
local_secondary_index {
1414
name = "queueSortOrder-index"
15-
range_key = "queueTimestamp"
15+
range_key = "queueSortOrder-SK"
1616
projection_type = "ALL"
1717
}
1818

@@ -27,7 +27,7 @@ resource "aws_dynamodb_table" "letter_queue" {
2727
}
2828

2929
attribute {
30-
name = "queueTimestamp"
30+
name = "queueSortOrder-SK"
3131
type = "S"
3232
}
3333

internal/datastore/src/__test__/db.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export async function setupDynamoDBContainer() {
2222
accessKeyId: "fakeMyKeyId",
2323
secretAccessKey: "fakeSecretAccessKey",
2424
},
25+
maxAttempts: 1,
2526
});
2627

2728
const docClient = DynamoDBDocumentClient.from(ddbClient);
@@ -132,7 +133,7 @@ const createLetterQueueTableCommand = new CreateTableCommand({
132133
IndexName: "queueSortOrder-index",
133134
KeySchema: [
134135
{ AttributeName: "supplierId", KeyType: "HASH" }, // Partition key for LSI
135-
{ AttributeName: "queueTimestamp", KeyType: "RANGE" }, // Sort key for LSI
136+
{ AttributeName: "queueSortOrderSk", KeyType: "RANGE" }, // Sort key for LSI
136137
],
137138
Projection: {
138139
ProjectionType: "ALL",

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

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ import { LetterAlreadyExistsError } from "../letter-already-exists-error";
1212
import { createTestLogger } from "./logs";
1313
import { LetterDoesNotExistError } from "../letter-does-not-exist-error";
1414

15-
function createLetter(letterId = "letter1"): InsertPendingLetter {
15+
function createLetter(
16+
overrides: Partial<InsertPendingLetter> = {},
17+
): InsertPendingLetter {
1618
return {
17-
letterId,
19+
letterId: "letter1",
1820
supplierId: "supplier1",
1921
specificationId: "specification1",
2022
groupId: "group1",
23+
priority: 10,
24+
...overrides,
2125
};
2226
}
2327

@@ -54,9 +58,11 @@ describe("LetterQueueRepository", () => {
5458
});
5559

5660
describe("putLetter", () => {
57-
it("adds a letter to the database", async () => {
61+
beforeEach(() => {
5862
jest.useFakeTimers().setSystemTime(new Date("2026-03-04T13:15:45.000Z"));
63+
});
5964

65+
it("adds a letter to the database", async () => {
6066
const pendingLetter =
6167
await letterQueueRepository.putLetter(createLetter());
6268

@@ -65,9 +71,32 @@ describe("LetterQueueRepository", () => {
6571
"2026-03-04T13:15:45.000Z",
6672
);
6773
expect(pendingLetter.ttl).toBe(1_772_633_745);
74+
expect(pendingLetter.queueSortOrderSk).toBe(
75+
"10-2026-03-04T13:15:45.000Z",
76+
);
6877
expect(await letterExists(db, "supplier1", "letter1")).toBe(true);
6978
});
7079

80+
it("left-pads the priority with zeros in the sort key", async () => {
81+
const pendingLetter = await letterQueueRepository.putLetter(
82+
createLetter({ priority: 5 }),
83+
);
84+
85+
expect(pendingLetter.queueSortOrderSk).toBe(
86+
"05-2026-03-04T13:15:45.000Z",
87+
);
88+
});
89+
90+
it("defaults a missing priority to 10 in the sort key", async () => {
91+
const pendingLetter = await letterQueueRepository.putLetter(
92+
createLetter({ priority: undefined }),
93+
);
94+
95+
expect(pendingLetter.queueSortOrderSk).toBe(
96+
"10-2026-03-04T13:15:45.000Z",
97+
);
98+
});
99+
71100
it("throws LetterAlreadyExistsError when creating a letter which already exists", async () => {
72101
await letterQueueRepository.putLetter(createLetter());
73102

@@ -122,16 +151,21 @@ describe("LetterQueueRepository", () => {
122151
});
123152
});
124153

125-
async function letterExists(
126-
db: DBContext,
127-
supplierId: string,
128-
letterId: string,
129-
): Promise<boolean> {
154+
async function getLetter(db: DBContext, supplierId: string, letterId: string) {
130155
const result = await db.docClient.send(
131156
new GetCommand({
132157
TableName: db.config.letterQueueTableName,
133158
Key: { supplierId, letterId },
134159
}),
135160
);
136-
return result.Item !== undefined;
161+
return result.Item;
162+
}
163+
164+
async function letterExists(
165+
db: DBContext,
166+
supplierId: string,
167+
letterId: string,
168+
): Promise<boolean> {
169+
const letter = await getLetter(db, supplierId, letterId);
170+
return letter !== undefined;
137171
}

internal/datastore/src/letter-queue-repository.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,20 @@ export default class LetterQueueRepository {
2424
readonly config: LetterQueueRepositoryConfig,
2525
) {}
2626

27+
private readonly defaultPriority = 10;
28+
2729
async putLetter(
2830
insertPendingLetter: InsertPendingLetter,
2931
): Promise<PendingLetter> {
3032
// needs to be an ISO timestamp as Db sorts alphabetically
3133
const now = new Date().toISOString();
32-
34+
const priority = String(insertPendingLetter.priority ?? 10);
35+
const queueSortOrderSk = `${priority.padStart(2, "0")}-${now}`;
3336
const pendingLetter: PendingLetter = {
3437
...insertPendingLetter,
3538
queueTimestamp: now,
3639
visibilityTimestamp: now,
40+
queueSortOrderSk,
3741
ttl: Math.floor(
3842
Date.now() / 1000 + 60 * 60 * this.config.letterQueueTtlHours,
3943
),

internal/datastore/src/types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export const LetterSchemaBase = z.object({
4343
export const LetterSchema = LetterSchemaBase.extend({
4444
supplierId: idRef(SupplierSchema, "id"),
4545
eventId: z.string().optional(),
46+
priority: z.int().min(0).max(99).optional(), // A lower number represents a higher priority
4647
url: z.url(),
4748
createdAt: z.string(),
4849
updatedAt: z.string(),
@@ -79,18 +80,20 @@ export type UpdateLetter = {
7980
export const PendingLetterSchema = z.object({
8081
supplierId: idRef(SupplierSchema, "id"),
8182
letterId: idRef(LetterSchema, "id"),
82-
queueTimestamp: z.string().describe("Secondary index SK"),
83+
queueTimestamp: z.string(),
8384
visibilityTimestamp: z.string(),
85+
queueSortOrderSk: z.string().describe("Secondary index SK"),
8486
specificationId: z.string(),
8587
groupId: z.string(),
88+
priority: z.int().min(0).max(99).optional(),
8689
ttl: z.int(),
8790
});
8891

8992
export type PendingLetter = z.infer<typeof PendingLetterSchema>;
9093

9194
export type InsertPendingLetter = Omit<
9295
PendingLetter,
93-
"ttl" | "queueTimestamp" | "visibilityTimestamp"
96+
"ttl" | "queueTimestamp" | "visibilityTimestamp" | "queueSortOrderSk"
9497
>;
9598

9699
export const MISchemaBase = z.object({

0 commit comments

Comments
 (0)