Skip to content

Commit c487606

Browse files
committed
CCM-11602: GET Letters endpoint
1 parent 740a199 commit c487606

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

internal/datastore/src/letter-repository.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,25 @@ export class LetterRepository {
131131
return LetterSchema.parse(result.Attributes);
132132
}
133133

134-
async getLettersBySupplier(supplierId: string, status: string, size: number, cursor?: string): Promise<Letter[]> {
134+
async getLettersBySupplier(supplierId: string, status: string, size: number, cursor?: string): Promise<{
135+
nextCursor: string;
136+
letters: Letter[]}> {
137+
const supplierStatus = `${supplierId}#${status}`;
135138
const result = await this.ddbClient.send(new QueryCommand({
136139
TableName: this.config.lettersTableName,
137140
IndexName: 'supplierStatus-index',
138141
KeyConditionExpression: 'supplierStatus = :supplierStatus',
139142
Limit: size,
140143
ExpressionAttributeValues: {
141-
':supplierStatus': `${supplierId}#${status}`
142-
}
144+
':supplierStatus': supplierStatus
145+
},
146+
...(cursor != undefined ? {
147+
LastEvaluatedKey: {
148+
id: cursor,
149+
supplierStatus,
150+
supplierId,
151+
}
152+
} : {})
143153
}));
144154
this.log.info({
145155
description: 'items',
@@ -149,6 +159,9 @@ export class LetterRepository {
149159
cursor,
150160
result,
151161
})
152-
return z.array(LetterSchema).parse(result.Items);
162+
return {
163+
nextCursor: result.LastEvaluatedKey.id,
164+
letters: z.array(LetterSchema).parse(result.Items)
165+
};
153166
}
154167
}

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ export const getLetters: APIGatewayProxyHandler = async (event) => {
3535

3636
const cursor = event.queryStringParameters?.cursor;
3737

38-
const letters = await getLettersForSupplier(supplierId, status, Number(size), letterRepo, cursor);
38+
const {nextCursor, letters} = await getLettersForSupplier(supplierId, status, Number(size), letterRepo, cursor);
3939

40-
const response = createGetLettersResponse(event.path, letters, supplierId);
40+
const response = createGetLettersResponse(event.path, letters, supplierId, status, size, cursor, nextCursor);
4141

4242
return {
4343
statusCode: 200,
@@ -54,7 +54,7 @@ export const getLetters: APIGatewayProxyHandler = async (event) => {
5454
interface GetLettersLinks {
5555
self: string;
5656
first: string;
57-
last: string;
57+
last?: string;
5858
next?: string;
5959
prev?: string;
6060
}
@@ -77,14 +77,18 @@ function createGetLettersResponse(
7777
baseUrl: string,
7878
letters: Letter[],
7979
supplierId: string,
80+
status: string,
81+
size: string,
82+
cursor?: string,
83+
nextCursor?: string
8084
): GetLettersResponse {
85+
const cursorParam = cursor != undefined ? `&cursor=${cursor}` : '';
86+
const nextCursorParam = nextCursor != undefined ? `&cursor=${nextCursor}` : '';
8187
return {
8288
links: {
83-
self: `${baseUrl}?page=1`,
84-
first: `${baseUrl}?page=1`,
85-
last: `${baseUrl}?page=1`,
86-
next: `${baseUrl}?page=1`,
87-
prev: `${baseUrl}?page=1`
89+
self: `${baseUrl}?status=${status}&size=${size}${cursorParam}`,
90+
first: `${baseUrl}?status=${status}&size=${size}`,
91+
next: `${baseUrl}?status=${status}&size=${size}${nextCursorParam}`
8892
},
8993
data: {
9094
type: 'Letters',

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import { LetterRepository } from '../../../../internal/datastore/src'
1+
import { Letter, LetterRepository } from '../../../../internal/datastore/src'
22
import { NotFoundError, ValidationError } from '../errors';
33
import { LetterApiResource, LetterApiDocument } from '../contracts/letter-api';
44
import { toApiLetter } from '../mappers/letter-mapper';
55

66

7-
export const getLettersForSupplier = async (supplierId: string, status: string, size: number, letterRepo: LetterRepository, cursor?: string): Promise<string[]> => {
7+
export const getLettersForSupplier = async (supplierId: string, status: string, size: number, letterRepo: LetterRepository, cursor?: string): Promise<{
8+
nextCursor: string;
9+
letters: Letter[]}> => {
810

911
return await letterRepo.getLettersBySupplier(supplierId, status, size, cursor);
1012
}

0 commit comments

Comments
 (0)