From a0dd79ba651819ba742afb8fb88dbadd09aa00a3 Mon Sep 17 00:00:00 2001 From: Kaden Lei Date: Mon, 16 Oct 2023 17:09:00 -0400 Subject: [PATCH] Fixed deletion functions --- backend/src/customers/controllers.ts | 75 ++++++++++++++++++---------- 1 file changed, 49 insertions(+), 26 deletions(-) diff --git a/backend/src/customers/controllers.ts b/backend/src/customers/controllers.ts index be7743a..83983c4 100644 --- a/backend/src/customers/controllers.ts +++ b/backend/src/customers/controllers.ts @@ -1,5 +1,12 @@ 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 */ @@ -17,17 +24,6 @@ const getClusters = async () => ClusterModel.find({}); const insertCluster = async (location: [number]) => ClusterModel.create(new Cluster(location)); -/** - * Removes cluster by id from DB - * @param id cluster id - * @returns doc containg bool acknowledged and number deletedCount - */ -const deleteCluster = async (id: string) => { - ClusterModel.deleteOne({ _id: new mongoose.Types.ObjectId(id) }); - NetModel.deleteMany({ clusterID: id }); // FIX: does not delete associated net docs - // TODO: delete associated data docs -}; - /** * Finds all net docs in DB * @returns promise with all net docs or error @@ -51,17 +47,6 @@ const getNetByClusterId = async (id: string) => const insertNet = async (clusterID: string, type: string) => NetModel.create(new Net(clusterID, type)); -/** - * Removes net by id from DB - * @param id net id - * @returns doc containg bool acknowledged and number deletedCount - */ -const deleteNet = async (id: string) => { - console.log(id); - NetModel.deleteOne({ _id: new mongoose.Types.ObjectId(id) }); - DataModel.deleteMany({ netID: id }); // FIX: does not delete associated data docs -}; - /** * Finds all data docs in DB * @returns promise with all data docs or error @@ -90,8 +75,46 @@ const insertData = async (netID: string, date: Date, water_collected: number) => * @param id data id * @returns doc containg bool acknowledged and number deletedCount */ -const deleteData = async (id: string) => - DataModel.deleteOne({ _id: new mongoose.Types.ObjectId(id) }); +const deleteData = async (id: string) => { + const deleteDataResult = await DataModel.deleteOne({ + _id: new mongoose.Types.ObjectId(id), + }); + return { deleteDataResult }; +}; + +/** + * Removes net by id from DB + * @param id net id + * @returns doc containg bool acknowledged and number deletedCount + */ +const deleteNet = async (id: string) => { + const deleteNetResult = await NetModel.deleteOne({ + _id: new mongoose.Types.ObjectId(id), + }); + const deleteDataResult = await DataModel.deleteMany({ netID: id }); + return { deleteNetResult, deleteDataResult }; +}; + +/** + * Removes cluster by id from DB + * @param id cluster id + * @returns doc containg bool acknowledged and number deletedCount + */ +const deleteCluster = async (id: string) => { + const nets = await NetModel.find({ clusterID: id }); + let netClusterIDs: mongoose.Types.ObjectId[] = []; + nets.forEach((net) => { + netClusterIDs.push(net["_id"]); + }); + console.log(netClusterIDs); + const deleteClusterResult = await ClusterModel.deleteOne({ + _id: new mongoose.Types.ObjectId(id), + }); + netClusterIDs.forEach(async (n) => await deleteNet(n.toString())); + return { + deleteClusterResult, + }; +}; /** NOTE: END OF TEMPORARY FUNCTIONS */ @@ -137,5 +160,5 @@ export default { getDataByNetId, insertData, deleteData, - getAllDocsByClusterIDs + getAllDocsByClusterIDs, };