Skip to content

Commit

Permalink
Merge pull request #12 from cornellh4i/delete-doc
Browse files Browse the repository at this point in the history
Fixed deletion functions
  • Loading branch information
jfeuerstein authored Oct 18, 2023
2 parents 6c37021 + e5df728 commit a21a79e
Showing 1 changed file with 48 additions and 25 deletions.
73 changes: 48 additions & 25 deletions backend/src/customers/controllers.ts
Original file line number Diff line number Diff line change
@@ -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 */
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 */

Expand Down

0 comments on commit a21a79e

Please sign in to comment.