From b8f3ae3b7946bfa6c78f98ae137c0bfe1a5a4eeb Mon Sep 17 00:00:00 2001 From: Graham Stewart Date: Mon, 18 Mar 2024 16:56:32 -0700 Subject: [PATCH] Implemented an endpoint for classifications and predominate uses --- .../controllers/lookup/lookupController.ts | 28 +++++++++++++++++-- .../src/controllers/lookup/lookupSchema.ts | 14 ++++++++++ express-api/src/routes/lookupRouter.ts | 2 ++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 express-api/src/controllers/lookup/lookupSchema.ts diff --git a/express-api/src/controllers/lookup/lookupController.ts b/express-api/src/controllers/lookup/lookupController.ts index 8a68c8eec..0cffdcffa 100644 --- a/express-api/src/controllers/lookup/lookupController.ts +++ b/express-api/src/controllers/lookup/lookupController.ts @@ -1,5 +1,12 @@ +import { AppDataSource } from '@/appDataSource'; +import { PropertyClassification } from '@/typeorm/Entities/PropertyClassification'; import { stubResponse } from '@/utilities/stubResponse'; import { Request, Response } from 'express'; +import { + ClassificationPublicResponseSchema, + PredominateUsePublicResponseSchema, +} from './lookupSchema'; +import { BuildingPredominateUse } from '@/typeorm/Entities/BuildingPredominateUse'; // TODO: What controllers here could just be replaced by existing GET requests? @@ -56,8 +63,25 @@ export const lookupPropertyClassifications = async (req: Request, res: Response) }] */ - // TODO: Replace stub response with controller logic - return stubResponse(res); + const classifications = await AppDataSource.getRepository(PropertyClassification).find(); + const filtered = classifications.filter((c) => !c.IsDisabled); + const parsed = ClassificationPublicResponseSchema.array().safeParse(filtered); + if (parsed.success) { + return res.status(200).send(parsed.data); + } else { + return res.status(400).send('Something went wrong.'); + } +}; + +export const lookupBuildingPredominateUse = async (req: Request, res: Response) => { + const uses = await AppDataSource.getRepository(BuildingPredominateUse).find(); + const filtered = uses.filter((u) => !u.IsDisabled); + const parsed = PredominateUsePublicResponseSchema.array().safeParse(filtered); + if (parsed.success) { + return res.status(200).send(parsed.data); + } else { + return res.status(400).send('Something went wrong.'); + } }; /** diff --git a/express-api/src/controllers/lookup/lookupSchema.ts b/express-api/src/controllers/lookup/lookupSchema.ts new file mode 100644 index 000000000..815d6ffa4 --- /dev/null +++ b/express-api/src/controllers/lookup/lookupSchema.ts @@ -0,0 +1,14 @@ +import { z } from 'zod'; + +export const ClassificationPublicResponseSchema = z.object({ + Name: z.string(), + Id: z.number(), + SortOrder: z.number(), + IsVisible: z.boolean(), +}); + +export const PredominateUsePublicResponseSchema = z.object({ + Name: z.string(), + Id: z.number(), + SortOrder: z.number(), +}); diff --git a/express-api/src/routes/lookupRouter.ts b/express-api/src/routes/lookupRouter.ts index ad1cece4d..9d0ff9a4f 100644 --- a/express-api/src/routes/lookupRouter.ts +++ b/express-api/src/routes/lookupRouter.ts @@ -1,4 +1,5 @@ import controllers from '@/controllers'; +import { lookupBuildingPredominateUse } from '@/controllers/lookup/lookupController'; import express from 'express'; const router = express.Router(); @@ -15,6 +16,7 @@ const { router.route('/agencies').get(lookupAgencies); router.route('/roles').get(lookupRoles); router.route('/property/classifications').get(lookupPropertyClassifications); +router.route('/property/predominateUses').get(lookupBuildingPredominateUse); router.route('/project/tier/levels').get(lookupProjectTierLevels); router.route('/project/risks').get(lookupProjectRisks); router.route('/all').get(lookupAll);