Skip to content

Commit

Permalink
Updated function
Browse files Browse the repository at this point in the history
  • Loading branch information
hanahhleee committed Oct 13, 2023
1 parent d5a392d commit fc7b43c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 60 deletions.
49 changes: 48 additions & 1 deletion backend/api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
}
}
},
"/clusters/{minDate}/{maxDate}/": {
"/clusters/clusterData/dates/{minDate}/{maxDate}/": {
"get": {
"description": "",
"parameters": [
Expand All @@ -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"
}
}
}
}
}
}
36 changes: 12 additions & 24 deletions backend/src/customers/controllers.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
};

/**
Expand Down Expand Up @@ -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
};

/**
Expand Down Expand Up @@ -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
Expand All @@ -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 },
Expand All @@ -133,7 +121,7 @@ const getAllDocsByClusterIDs = async (
for await (const doc of cursor2) {
datas.push(doc);
}
console.log(datas);

return datas;
};

Expand All @@ -149,5 +137,5 @@ export default {
getDataByNetId,
insertData,
deleteData,
getAllDocsByClusterIDs,
getAllDocsByClusterIDs
};
69 changes: 36 additions & 33 deletions backend/src/customers/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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;
2 changes: 1 addition & 1 deletion backend/src/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const doc = {
description:
"Documentation automatically generated by the <b>swagger-autogen</b> module.",
},
host: "localhost:8000", // NOTE: changed this from 3000 -> 8000
host: "localhost:8000",
basePath: "/",
schemes: ["http", "https"],
consumes: ["application/json"],
Expand Down
1 change: 0 additions & 1 deletion backend/tests/customer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit fc7b43c

Please sign in to comment.