Skip to content

Commit be976b9

Browse files
committed
refactor: migrate rooms.saveNotification endpoint to new API format with AJV validation
1 parent 3145c41 commit be976b9

File tree

2 files changed

+48
-27
lines changed

2 files changed

+48
-27
lines changed

apps/meteor/app/api/server/v1/rooms.ts

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -285,30 +285,54 @@ API.v1.addRoute(
285285
},
286286
);
287287

288-
API.v1.addRoute(
289-
'rooms.saveNotification',
290-
{ authRequired: true },
291-
{
292-
async post() {
293-
const { roomId, notifications } = this.bodyParams;
294-
295-
if (!roomId) {
296-
return API.v1.failure("The 'roomId' param is required");
297-
}
298-
299-
if (!notifications || Object.keys(notifications).length === 0) {
300-
return API.v1.failure("The 'notifications' param is required");
301-
}
302-
303-
await Promise.all(
304-
Object.entries(notifications as Notifications).map(async ([notificationKey, notificationValue]) =>
305-
saveNotificationSettingsMethod(this.userId, roomId, notificationKey as NotificationFieldType, notificationValue),
306-
),
307-
);
308-
309-
return API.v1.success();
310-
},
311-
},
288+
const saveNotificationBodySchema = ajv.compile<{
289+
roomId: string;
290+
notifications: Record<string, unknown>;
291+
}>({
292+
type: 'object',
293+
properties: {
294+
roomId: { type: 'string' },
295+
notifications: {
296+
type: 'object',
297+
minProperties: 1,
298+
additionalProperties: true,
299+
},
300+
},
301+
required: ['roomId', 'notifications'],
302+
additionalProperties: false,
303+
});
304+
305+
const saveNotificationResponseSchema = ajv.compile({
306+
type: 'object',
307+
properties: {
308+
success: { type: 'boolean', enum: [true] },
309+
},
310+
required: ['success'],
311+
additionalProperties: false,
312+
});
313+
314+
API.v1.post(
315+
'rooms.saveNotification',
316+
{
317+
authRequired: true,
318+
body: saveNotificationBodySchema,
319+
response: {
320+
200: saveNotificationResponseSchema,
321+
400: validateBadRequestErrorResponse,
322+
401: validateUnauthorizedErrorResponse,
323+
},
324+
},
325+
async function action() {
326+
const { roomId, notifications } = this.bodyParams;
327+
328+
await Promise.all(
329+
Object.entries(notifications as Notifications).map(async ([notificationKey, notificationValue]) =>
330+
saveNotificationSettingsMethod(this.userId, roomId, notificationKey as NotificationFieldType, notificationValue),
331+
),
332+
);
333+
334+
return API.v1.success();
335+
},
312336
);
313337

314338
API.v1.addRoute(

packages/rest-typings/src/v1/rooms.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -801,9 +801,6 @@ export type RoomsEndpoints = {
801801
}) => { message: IMessage | null };
802802
};
803803

804-
'/v1/rooms.saveNotification': {
805-
POST: (params: { roomId: string; notifications: Notifications }) => void;
806-
};
807804

808805
'/v1/rooms.favorite': {
809806
POST: (

0 commit comments

Comments
 (0)