Skip to content

Commit 18a1edb

Browse files
committed
update functions
1 parent b19291b commit 18a1edb

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/api/routes/roomRequests.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ const roomRequestRoutes: FastifyPluginAsync = async (fastify, _options) => {
239239
});
240240
}
241241
const items = response.Items.map((x) => {
242+
if (!request.query.select.includes("status")) {
243+
return unmarshall(x);
244+
}
242245
const item = unmarshall(x) as {
243246
host: string;
244247
title: string;
@@ -418,20 +421,29 @@ const roomRequestRoutes: FastifyPluginAsync = async (fastify, _options) => {
418421
example: "sp25",
419422
}),
420423
}),
424+
querystring: z.object(
425+
getDefaultFilteringQuerystring({
426+
defaultSelect: ["requestId", "title"],
427+
}),
428+
),
421429
}),
422430
),
423431
onRequest: fastify.authorizeFromSchema,
424432
},
425433
async (request, reply) => {
426434
const requestId = request.params.requestId;
427435
const semesterId = request.params.semesterId;
436+
const { ProjectionExpression, ExpressionAttributeNames } =
437+
generateProjectionParams({ userFields: request.query.select });
428438
let command;
429439
if (request.userRoles?.has(AppRoles.BYPASS_OBJECT_LEVEL_AUTH)) {
430440
command = new QueryCommand({
431441
TableName: genericConfig.RoomRequestsTableName,
432442
IndexName: "RequestIdIndex",
433443
KeyConditionExpression: "requestId = :requestId",
434444
FilterExpression: "semesterId = :semesterId",
445+
ProjectionExpression,
446+
ExpressionAttributeNames,
435447
ExpressionAttributeValues: {
436448
":requestId": { S: requestId },
437449
":semesterId": { S: semesterId },
@@ -441,6 +453,7 @@ const roomRequestRoutes: FastifyPluginAsync = async (fastify, _options) => {
441453
} else {
442454
command = new QueryCommand({
443455
TableName: genericConfig.RoomRequestsTableName,
456+
ProjectionExpression,
444457
KeyConditionExpression:
445458
"semesterId = :semesterId AND #userIdRequestId = :userRequestId",
446459
ExpressionAttributeValues: {
@@ -449,6 +462,7 @@ const roomRequestRoutes: FastifyPluginAsync = async (fastify, _options) => {
449462
},
450463
ExpressionAttributeNames: {
451464
"#userIdRequestId": "userId#requestId",
465+
...ExpressionAttributeNames,
452466
},
453467
Limit: 1,
454468
});

src/common/utils.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,20 @@ export const generateProjectionParams = ({ userFields }: GenerateProjectionParam
3737
};
3838

3939

40-
export const nonEmptyCommaSeparatedStringSchema = z.preprocess(
41-
(val) => String(val).split(',').map(item => item.trim()),
42-
z.array(z.string()).nonempty()
43-
);
40+
export const nonEmptyCommaSeparatedStringSchema = z
41+
.string({ invalid_type_error: "Filter expression must be a string." })
42+
.min(1, { message: "Filter expression must be at least 1 character long." })
43+
.transform((val) => val.split(',').map(item => item.trim()))
44+
.pipe(z.array(z.string()).nonempty());
4445

45-
type GettDefaultFilteringQuerystringInput = {
46+
type GetDefaultFilteringQuerystringInput = {
4647
defaultSelect: string[];
4748
}
48-
export const getDefaultFilteringQuerystring = ({ defaultSelect }: GettDefaultFilteringQuerystringInput) => {
49+
export const getDefaultFilteringQuerystring = ({ defaultSelect }: GetDefaultFilteringQuerystringInput) => {
4950
return {
5051
select: z.optional(nonEmptyCommaSeparatedStringSchema).default(defaultSelect.join(',')).openapi({
5152
description: "Comma-seperated list of attributes to return",
53+
...(defaultSelect.length === 0 ? { default: "<ALL ATTRIBUTES>" } : {}),
5254
})
5355
}
5456
}

0 commit comments

Comments
 (0)