Skip to content

Commit

Permalink
expand public response schema
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarkowsky committed Mar 19, 2024
1 parent 9afc4fe commit 9cd053f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
28 changes: 16 additions & 12 deletions express-api/src/controllers/agencies/agenciesController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Request, Response } from 'express';
import { NextFunction, Request, Response } from 'express';
import * as agencyService from '@/services/agencies/agencyServices';
import { AgencyFilterSchema, AgencyPublicResponseSchema } from '@/services/agencies/agencySchema';
import { z } from 'zod';
Expand All @@ -11,25 +11,29 @@ import { Roles } from '@/constants/roles';
* @param {Response} res Outgoing response
* @returns {Response} A 200 status with a list of agencies.
*/
export const getAgencies = async (req: Request, res: Response) => {
export const getAgencies = async (req: Request, res: Response, next: NextFunction) => {
/**
* #swagger.tags = ['Agencies - Admin']
* #swagger.description = 'Gets a paged list of agencies.'
* #swagger.security = [{
"bearerAuth": []
}]
*/
const kcUser = req.user as KeycloakUser;
const filter = AgencyFilterSchema.safeParse(req.query);
if (filter.success) {
const agencies = await agencyService.getAgencies(filter.data);
if (!kcUser.client_roles || !kcUser.client_roles.includes(Roles.ADMIN)) {
const trimmed = AgencyPublicResponseSchema.array().parse(agencies);
return res.status(200).send(trimmed);
try {
const kcUser = req.user as KeycloakUser;
const filter = AgencyFilterSchema.safeParse(req.query);
if (filter.success) {
const agencies = await agencyService.getAgencies(filter.data);
if (!kcUser.client_roles || !kcUser.client_roles.includes(Roles.ADMIN)) {
const trimmed = AgencyPublicResponseSchema.array().parse(agencies);
return res.status(200).send(trimmed);
}
return res.status(200).send(agencies);
} else {
return res.status(400).send('Could not parse filter.');
}
return res.status(200).send(agencies);
} else {
return res.status(400).send('Could not parse filter.');
} catch (e) {
next(e);
}
};

Expand Down
1 change: 1 addition & 0 deletions express-api/src/services/agencies/agencySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const AgencyPublicResponseSchema = z.object({
Code: z.string(),
Description: z.string().nullable(),
IsDisabled: z.boolean(),
ParentId: z.number().int().nullable(),
});

export type Agency = z.infer<typeof AgencyCreationSchema>;
Expand Down

0 comments on commit 9cd053f

Please sign in to comment.