Skip to content

Commit

Permalink
Merge pull request #22 from cornellh4i/create-delete-user
Browse files Browse the repository at this point in the history
Create add and delete user functions
  • Loading branch information
jfeuerstein authored Oct 30, 2023
2 parents d06779e + 9b7444c commit c750cbc
Show file tree
Hide file tree
Showing 10 changed files with 1,329 additions and 52 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.env
secrets/
node_modules/
serviceAccountKey.json
92 changes: 92 additions & 0 deletions backend/api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@
"properties": {
"location": {
"example": "any"
},
"nets_in_cluster": {
"example": "any"
},
"community": {
"example": "any"
},
"population": {
"example": "any"
}
}
}
Expand Down Expand Up @@ -200,6 +209,9 @@
"date": {
"example": "any"
},
"created_at": {
"example": "any"
},
"water_collected": {
"example": "any"
}
Expand Down Expand Up @@ -321,6 +333,33 @@
}
}
},
"/clusters/get-clusters-in-range/": {
"post": {
"description": "",
"parameters": [
{
"name": "body",
"in": "body",
"schema": {
"type": "object",
"properties": {
"lower_left": {
"example": "any"
},
"upper_right": {
"example": "any"
}
}
}
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/clusters/netModelData/{modelName}/{modelCost}/dates/{minDate}/{maxDate}/": {
"get": {
"description": "",
Expand Down Expand Up @@ -440,6 +479,59 @@
}
}
}
},
"/users/": {
"get": {
"description": "",
"parameters": [],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/users/add/{number}/{claim}": {
"post": {
"description": "",
"parameters": [
{
"name": "number",
"in": "path",
"required": true,
"type": "string"
},
{
"name": "claim",
"in": "path",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/users/delete/{number}": {
"delete": {
"description": "",
"parameters": [
{
"name": "number",
"in": "path",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
}
}
}
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"body-parser": "^1.20.1",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"firebase-admin": "^11.11.0",
"jest": "^29.3.1",
"mongoose": "^6.8.4",
"swagger-autogen": "^2.23.1",
Expand Down
30 changes: 9 additions & 21 deletions backend/src/customers/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,6 @@ const getNetByClusterId = async (id: string) =>
const insertNet = async (clusterID: string, model: Model) =>
NetModel.create(new Net(clusterID, model));

/**
* 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 @@ -157,14 +146,14 @@ const getAllDocsByNetIDs = async (
) => {
const cursor = DataModel.find({
netID: { $in: netIds },
date: { $gte: minDate, $lte: maxDate }
date: { $gte: minDate, $lte: maxDate },
});
const datas = [];
for await (const doc of cursor) {
datas.push(doc);
}

return datas
return datas;
};

/**
Expand All @@ -185,7 +174,7 @@ const getAllDocsByClusterIDs = async (
netIds.push(doc.id);
}

return getAllDocsByNetIDs(netIds, minDate, maxDate)
return getAllDocsByNetIDs(netIds, minDate, maxDate);
};

/**
Expand All @@ -205,17 +194,16 @@ const getAllDocsByModelAndClusterIDs = async (
const cursor = NetModel.find({
clusterID: { $in: clusterIds },
"model.name": netModel.name,
"model.cost": netModel.cost
"model.cost": netModel.cost,
});
const netIds = [];
for await (const doc of cursor) {
netIds.push(doc.id);
}

return getAllDocsByNetIDs(netIds, minDate, maxDate)
return getAllDocsByNetIDs(netIds, minDate, maxDate);
};


/**
* Finds all clusters within specified coordinates
* @param lower_left coordinates of lower left binding of location range
Expand All @@ -231,9 +219,9 @@ const getClustersByLocation = async (
{ "location.0": { $gte: lower_left[0] } },
{ "location.0": { $lte: upper_right[0] } },
{ "location.1": { $gte: lower_left[1] } },
{ "location.1": { $lte: upper_right[1] } }
]
})
{ "location.1": { $lte: upper_right[1] } },
],
});

export default {
getClusters,
Expand All @@ -249,5 +237,5 @@ export default {
deleteData,
getClustersByLocation,
getAllDocsByClusterIDs,
getAllDocsByModelAndClusterIDs
getAllDocsByModelAndClusterIDs,
};
15 changes: 1 addition & 14 deletions backend/src/customers/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Model {

@prop()
public name!: string;

@prop()
public cost!: number;
}
Expand Down Expand Up @@ -39,19 +39,6 @@ class Data {
public water_collected!: number;
}

class Model {
constructor(name: string, cost: number) {
this.name = name;
this.cost = cost;
}

@prop()
public name!: string;

@prop()
public cost!: number;
}

class Net {
constructor(cluster_id: string, model: Model) {
this.cluster_id = cluster_id;
Expand Down
1 change: 1 addition & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from "express";
import bodyParser from "body-parser";
import DocRouter from "./customers/views";
import userRouter from "./users/views";
import swaggerUI from "swagger-ui-express";
import spec from "../api-spec.json";
import { dbConnect } from "./database";
Expand Down
67 changes: 67 additions & 0 deletions backend/src/users/firebase-functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var admin = require("firebase-admin");

var serviceAccount = require("./serviceAccountKey.json");

admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});

/**
* Create a new user given their phone number; number should have +(country code) prepended, e.g. +16071234567
*/
export function createUser(number: string, claim: number) {
let customClaims = {};
switch (claim) {
case 0: {
customClaims = { "mobile-entry": true };
break;
}
case 1: {
customClaims = { "web-entry": true };
break;
}
case 2: {
customClaims = { evaluator: true };
break;
}
case 3: {
customClaims = { admin: true };
break;
}
}
admin
.auth()
.createUser({
phoneNumber: number,
})
.then((userRecord: any) => {
admin.auth().setCustomUserClaims(userRecord.uid, customClaims);
console.log("Successfully created new user:", userRecord.uid);
})
.catch((error: any) => {
console.log("Error creating new user: ", error);
});
}

/**
* Delete a user given their phone number; number should have +(country code) prepended, e.g. +16071234567
*/
export function deleteUser(number: string) {
const auth = admin.auth();
auth
.getUserByPhoneNumber(number)
.then((userRecord: any) => {
console.log(`Successfully fetched user data: ${userRecord.toJSON()}`);
auth
.deleteUser(userRecord.uid)
.then(() => {
console.log("Successfully deleted user");
})
.catch((error: any) => {
console.log("Error deleting user:", error);
});
})
.catch((error: any) => {
console.log("Error fetching user data:", error);
});
}
9 changes: 8 additions & 1 deletion backend/src/users/views.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// use Controllers here, just as we did in ../users/controllers.ts

import { Router } from "express";
import { createUser, deleteUser } from "./firebase-functions";
import { successJson } from "../utils/jsonResponses";
import UserController from "./controllers";

Expand All @@ -10,7 +11,13 @@ userRouter.get("/", (req, res) => {
res.send("Hello from a subrouter");
});

userRouter.post("/", (req, res) => {
userRouter.post("/add/:number/:claim", (req, res) => {
createUser(req.params.number, +req.params.claim);
res.send(req.body);
});

userRouter.delete("/delete/:number", (req, res) => {
deleteUser(req.params.number);
res.send(req.body);
});

Expand Down
Loading

0 comments on commit c750cbc

Please sign in to comment.