From e58388911167ef25a1c0f4d321a3bf13150936ab Mon Sep 17 00:00:00 2001 From: Courtney Myers Date: Fri, 18 Aug 2023 16:34:48 -0400 Subject: [PATCH] Update NCES web service endpoint to account for no NCES ID provided and only perform the search if NCES ID is exactly seven characters --- app/server/app/routes/formioNCES.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/app/server/app/routes/formioNCES.js b/app/server/app/routes/formioNCES.js index a56522be..f28fc29a 100644 --- a/app/server/app/routes/formioNCES.js +++ b/app/server/app/routes/formioNCES.js @@ -11,7 +11,7 @@ const data = require("../content/nces.json"); const router = express.Router(); // --- Search the NCES data with the provided NCES ID and return a match -router.get("/:searchText", async (req, res) => { +router.get("/:searchText?", async (req, res) => { const { searchText } = req.params; // const s3BucketUrl = `https://${S3_PUBLIC_BUCKET}.s3-${S3_PUBLIC_REGION}.amazonaws.com`; @@ -22,11 +22,25 @@ router.get("/:searchText", async (req, res) => { // : axios.get(`${s3BucketUrl}/content/nces.json`).then((res) => res.data)) // ); + if (!searchText) { + const logMessage = `No NCES ID passed to NCES data lookup.`; + log({ level: "info", message: logMessage, req }); + + return res.json({}); + } + + if (searchText.length !== 7) { + const logMessage = `Invalid NCES ID '${searchText}' passed to NCES data lookup.`; + log({ level: "info", message: logMessage, req }); + + return res.json({}); + } + const result = data.find((item) => item["NCES District ID"] === searchText); const logMessage = `NCES data searched with NCES ID '${searchText}' resulting in ` + - `${result ? "a match" : "no results"}.`; + `${result ? "a match" : "no matches"}.`; log({ level: "info", message: logMessage, req }); return res.json({ ...result });