From fc7b43c73a990c2e756f3bbe5906293e532ff184 Mon Sep 17 00:00:00 2001 From: hanahhleee Date: Fri, 13 Oct 2023 18:00:06 -0400 Subject: [PATCH] Updated function --- backend/api-spec.json | 49 +++++++++++++++++++- backend/src/customers/controllers.ts | 36 +++++---------- backend/src/customers/views.ts | 69 +++++++++++++++------------- backend/src/swagger.ts | 2 +- backend/tests/customer.test.ts | 1 - 5 files changed, 97 insertions(+), 60 deletions(-) diff --git a/backend/api-spec.json b/backend/api-spec.json index fc1c093..c0f610a 100644 --- a/backend/api-spec.json +++ b/backend/api-spec.json @@ -250,7 +250,7 @@ } } }, - "/clusters/{minDate}/{maxDate}/": { + "/clusters/clusterData/dates/{minDate}/{maxDate}/": { "get": { "description": "", "parameters": [ @@ -273,6 +273,53 @@ } } } + }, + "/clusters/clusterData/min/{minDate}": { + "get": { + "description": "", + "parameters": [ + { + "name": "minDate", + "in": "path", + "required": true, + "type": "string" + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, + "/clusters/clusterData/max/{maxDate}": { + "get": { + "description": "", + "parameters": [ + { + "name": "maxDate", + "in": "path", + "required": true, + "type": "string" + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, + "/clusters/clusterData/": { + "get": { + "description": "", + "parameters": [], + "responses": { + "200": { + "description": "OK" + } + } + } } } } \ No newline at end of file diff --git a/backend/src/customers/controllers.ts b/backend/src/customers/controllers.ts index 5f08c19..be7743a 100644 --- a/backend/src/customers/controllers.ts +++ b/backend/src/customers/controllers.ts @@ -1,15 +1,8 @@ import mongoose from "mongoose"; - -import { - Cluster, - ClusterModel, - Net, - NetModel, - Data, - DataModel, -} from "./models"; +import { Cluster, ClusterModel, Net, NetModel, Data, DataModel } from "./models"; import { type } from "os"; +/** NOTE: THESE FUNCTIONS ARE TEMPORARY AND FOR TESTING PURPOSES ONLY */ /** * Finds all cluster docs in DB * @returns promise with all cluster docs or error @@ -31,8 +24,8 @@ const insertCluster = async (location: [number]) => */ const deleteCluster = async (id: string) => { ClusterModel.deleteOne({ _id: new mongoose.Types.ObjectId(id) }); - NetModel.deleteMany({ clusterID: id }); // FIX: make sure all associated nets are deleted when cluster is deleted - // DataModel.deleteMany({netID : id}); // FIX: delete data docs associated with above nets (idk how to do this rn) + NetModel.deleteMany({ clusterID: id }); // FIX: does not delete associated net docs + // TODO: delete associated data docs }; /** @@ -66,7 +59,7 @@ const insertNet = async (clusterID: string, type: string) => const deleteNet = async (id: string) => { console.log(id); NetModel.deleteOne({ _id: new mongoose.Types.ObjectId(id) }); - DataModel.deleteMany({ netID: id }); // FIX: for some reason the data is not deleted + DataModel.deleteMany({ netID: id }); // FIX: does not delete associated data docs }; /** @@ -100,6 +93,8 @@ const insertData = async (netID: string, date: Date, water_collected: number) => const deleteData = async (id: string) => DataModel.deleteOne({ _id: new mongoose.Types.ObjectId(id) }); +/** NOTE: END OF TEMPORARY FUNCTIONS */ + /** * Finds all data docs from clusters in list clusterIds * @param clusterIds list of clusterIDs @@ -109,22 +104,15 @@ const deleteData = async (id: string) => */ const getAllDocsByClusterIDs = async ( clusterIds: string[], - minDate?: Date, - maxDate?: Date + minDate: Date, + maxDate: Date ) => { - console.log(clusterIds); - if (typeof minDate === undefined) { - minDate = new Date("2023-01-01"); - } - if (typeof maxDate === undefined) { - maxDate = new Date(new Date().toJSON().slice(0, 10)); - } const cursor1 = NetModel.find({ clusterID: { $in: clusterIds } }); const netIds = []; for await (const doc of cursor1) { netIds.push(doc.id); } - console.log(netIds); + const cursor2 = DataModel.find({ netID: { $in: netIds }, date: { $gte: minDate, $lte: maxDate }, @@ -133,7 +121,7 @@ const getAllDocsByClusterIDs = async ( for await (const doc of cursor2) { datas.push(doc); } - console.log(datas); + return datas; }; @@ -149,5 +137,5 @@ export default { getDataByNetId, insertData, deleteData, - getAllDocsByClusterIDs, + getAllDocsByClusterIDs }; diff --git a/backend/src/customers/views.ts b/backend/src/customers/views.ts index bf18f9b..4df7dbd 100644 --- a/backend/src/customers/views.ts +++ b/backend/src/customers/views.ts @@ -8,20 +8,19 @@ import { successJson, errorJson } from "../utils/jsonResponses"; const docRouter = Router(); +// NOTE: THESE ROUTES ARE TEMPORARY AND FOR TESTING PURPOSES ONLY docRouter.get("/", async (req, res) => { res.status(200).send(await DocController.getClusters()); }); docRouter.post("/", async (req, res) => { const { location } = req.body; - res - .status(201) - .send(successJson(await DocController.insertCluster(location))); + res.status(201).send(successJson(await DocController.insertCluster(location))); }); docRouter.delete("/delete/:id", async (req, res) => { const id = req.params.id; - res.status(201).send(successJson(await DocController.deleteCluster(id))); + res.status(201).send(successJson( await DocController.deleteCluster(id))); }); docRouter.get("/nets/", async (req, res) => { @@ -35,9 +34,7 @@ docRouter.get("/nets/:id", async (req, res) => { docRouter.post("/nets/", async (req, res) => { const { clusterID, type } = req.body; - res - .status(201) - .send(successJson(await DocController.insertNet(clusterID, type))); + res.status(201).send( successJson(await DocController.insertNet(clusterID, type))); }); docRouter.delete("/net/delete/:id", async (req, res) => { @@ -56,40 +53,46 @@ docRouter.get("/data/:id", async (req, res) => { docRouter.post("/data/", async (req, res) => { const { netID, date, water_collected } = req.body; - res - .status(201) - .send( - successJson(await DocController.insertData(netID, date, water_collected)) - ); + res.status(201).send(successJson(await DocController.insertData(netID, date, water_collected))); }); docRouter.delete("/data/delete/:id", async (req, res) => { const id = req.params.id; res.status(201).send(successJson(await DocController.deleteData(id))); }); +// NOTE: END OF TEMPORARY ROUTES + +const clusterIds = ["65258f01ba4c14948edd4526", "65258d88899b7e221c1d33eb"] // NOTE: temp hardcoded list of clusterIDs for testing +docRouter.get("/clusterData/dates/:minDate/:maxDate/", async (req, res) => { + let minDate = new Date(req.params.minDate); + let maxDate = new Date(req.params.maxDate); + res + .status(200) + .send(successJson(await DocController.getAllDocsByClusterIDs(clusterIds, minDate, maxDate))); +}); + +docRouter.get("/clusterData/min/:minDate", async (req, res) => { + let minDate = new Date(req.params.minDate); + let maxDate = new Date(new Date().toJSON().slice(0, 10)); + res + .status(200) + .send(successJson(await DocController.getAllDocsByClusterIDs(clusterIds, minDate, maxDate))); +}); + +docRouter.get("/clusterData/max/:maxDate", async (req, res) => { + let minDate = new Date("2023-01-01"); + let maxDate = new Date(req.params.maxDate); + res + .status(200) + .send(successJson(await DocController.getAllDocsByClusterIDs(clusterIds, minDate, maxDate))); +}); -const clusterIds = ["65258f01ba4c14948edd4526", "65258d88899b7e221c1d33eb"]; // NOTE: temp hardcoded list of clusterIDs for testing -docRouter.get("/:minDate?/:maxDate?/", async (req, res) => { - let minDate = new Date(); // FIX: right now minDate and maxDate are required fields but they should be able to be left empty - let maxDate = new Date(); - if (req.params.minDate) { - minDate = new Date(req.params.minDate); - } else { - minDate = new Date("2023-01-01"); - } - if (req.params.maxDate) { - maxDate = new Date(req.params.maxDate); - } // Assumes the date is valid with correct syntax - else { - maxDate = new Date(new Date().toJSON().slice(0, 10)); - } +docRouter.get("/clusterData/", async (req, res) => { + let minDate = new Date("2023-01-01"); + let maxDate = new Date(new Date().toJSON().slice(0, 10)); res .status(200) - .send( - successJson( - await DocController.getAllDocsByClusterIDs(clusterIds, minDate, maxDate) - ) - ); + .send(successJson(await DocController.getAllDocsByClusterIDs(clusterIds, minDate, maxDate))); }); -export default docRouter; +export default docRouter; \ No newline at end of file diff --git a/backend/src/swagger.ts b/backend/src/swagger.ts index eb3f000..cd527f1 100644 --- a/backend/src/swagger.ts +++ b/backend/src/swagger.ts @@ -7,7 +7,7 @@ const doc = { description: "Documentation automatically generated by the swagger-autogen module.", }, - host: "localhost:8000", // NOTE: changed this from 3000 -> 8000 + host: "localhost:8000", basePath: "/", schemes: ["http", "https"], consumes: ["application/json"], diff --git a/backend/tests/customer.test.ts b/backend/tests/customer.test.ts index 416d1ba..efbee60 100644 --- a/backend/tests/customer.test.ts +++ b/backend/tests/customer.test.ts @@ -10,7 +10,6 @@ afterAll(async () => { await dbDisconnect(); }); -// FIX: add comprehensive testing here describe("Cluster Retreival Tests", () => { test("Get all clusters", async () => { const allClusters = await ClusterController.getClusters();