Skip to content

Commit

Permalink
Fixed some org functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGElCOgecko committed Apr 11, 2024
1 parent 2852fb3 commit b8bba93
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 146 deletions.
157 changes: 17 additions & 140 deletions backend/services/org.services.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ const { randomUUID } = require('crypto');
exports.addOrgService = async (params, body) => {
const orgId = randomUUID();
try {
const org = await mdb.orgs.findOne({ name: body. name });
if (org) {
console.log("addUserService Error: Org already exists");
return { status: "ERROR", data: "Org already exists" };
}
const result = await mdb.orgs.insertOne({
orgId: orgId,
...body,
Expand Down Expand Up @@ -102,18 +107,15 @@ exports.updateOrgService = async (params, body) => {

let query = {
'orgId': params.orgId,
'name': body.name || org.data.name,
'description': body.description || org.data.description,
'tags': body.tags || org.data.tags,
'membershipIds': body.membershipIds || org.data.membershipIds,
...body
}

try {
const result = await mdb.orgs.updateOne({ orgId: params.orgId }, { $set: query });
console.log("updateorgByIdService Success", result);
console.log("updateOrgByIdService Success", result);
return { status: "OK", data: query };
} catch (error) {
console.log("updateorgByIdService Error", error);
console.log("updateOrgByIdService Error", error);
return { status: "ERROR", data: error };
}
}
Expand Down Expand Up @@ -204,156 +206,31 @@ exports.removeMemberOrgService = async (params, body) => {
/*********************************
************ TESTING ************
*********************************/
/* COPIED FUNCTIONS */
addOrgService = async (params, body) => {
const orgId = randomUUID();
try {
const result = await mdb.orgs.insertOne({
orgId: orgId,
...body,
});
await exports.addMemberOrgService({ orgId: orgId }, { membershipId: body.userId });
console.log("addOrgService Success:", result);
return { status: "OK", data: body };
} catch (error) {
console.log("addOrgService Error:", error);
return { status: "ERROR", data: error };
}
}
getOrgByIdService = async (params, body) => {
try {
const org = await mdb.orgs.findOne({ orgId: params.orgId });
if (!org) {
console.log("getOrgByIdService Error: Org not found")
return { status: "ERROR", data: "Org not found" };
}
console.log("getOrgByIdService Success:", org);
return { status: "OK", data: org };
} catch (error) {
console.log("getOrgByIdService Error:", error);
return { status: "ERROR", data: error };
}
}
getAllOrgsService = async (params, body) => {
try {
const orgs = await mdb.orgs.find({}).toArray();
console.log("getAllOrgsService Success:", orgs);
return { status: "OK", data: orgs };
} catch (error) {
console.log("getAllOrgsService Error:", error);
return { status: "ERROR", data: error };
}
}
updateOrgService = async (params, body) => {
let org = {};
console.log("updateOrgByIdService - calling getOrgByIdService");
try {
org = await exports.getOrgByIdService(params, body);
} catch (error) {
console.log("updateOrgByIdService Error", error);
return { status: "ERROR", data: error };
}
console.log("updateOrgByIdService", org);
if (org.status != "OK") {
return { status: "ERROR", data: org.data };
}
if (!org.data) {
return { status: "ERROR", data: "org does not exist" };
}

let query = {
'orgId': params.orgId,
'name': body.name || org.data.name,
'description': body.description || org.data.description,
'tags': body.tags || org.data.tags,
'membershipIds': body.membershipIds || org.data.membershipIds,
}

try {
const result = await mdb.orgs.updateOne({ orgId: params.orgId }, { $set: query });
console.log("updateorgByIdService Success", result);
return { status: "OK", data: query };
} catch (error) {
console.log("updateorgByIdService Error", error);
return { status: "ERROR", data: error };
}
}
deleteOrgService = async (params) => {
try {
const org = await mdb.orgs.findOne({ orgId: params.orgId });
if (!org) {
console.log("deleteOrgByIdService Error: org not found");
return { status: "ERROR", data: "org not found" };
}
const result = await mdb.orgs.deleteOne({ orgId: params.orgId });
console.log("deleteOrgByIdService Success:", result);
return { status: "OK", data: org };
} catch (error) {
console.log("deleteOrgByIdService Error:", error);
return { status: "ERROR", data: error };
}
}
addMemberOrgService = async (params, body) => {
let org = await exports.getOrgByIdService(params, body);
if (org.status != "OK") {
return { status: "ERROR", data: org.data }
}
if (!org.data) {
return { status: "ERROR", data: "org does not exist" }
}
if (org.data.membershipIds.includes(body.membershipId)) {
return { status: "ERROR", data: "membership already exists in org" }
} else {
org.data.membershipIds.push(body.membershipId);
}
let result = await exports.updateOrgService(params, org.data);
if (result.status != "OK") {
return { status: "ERROR", data: result.data }
}
}
removeMemberOrgService = async (params, body) => {
let org = await exports.getOrgByIdService(params, body);
if (org.status != "OK") {
return { status: "ERROR", data: org.data }
}
if (!org.data) {
return { status: "ERROR", data: "org does not exist" }
}
if (org.data.membershipIds.includes(body.membershipId)) {
org.data.membershipIds = org.data.membershipIds.filter(id => id != body.membershipId);
} else {
return { status: "ERROR", data: "membership doesn't exist in org" }
}
let result = await exports.updateOrgService(params, org.data);
if (result.status != "OK") {
return { status: "ERROR", data: result.data }
}
}
/* TESTING */
setTimeout(function() { // second set of tests to run (user, org, roommate)
console.log("I am org testing, I am running");
let body1 = {
'name': "Geico",
'email': "[email protected]",
'description': "15 minutes could save you 15% or more on car insurance",
'tags': ["cars", "insurance"],
'membershipId': []
'membershipId': [],
'orgId': "9801258058"
}
let params1 = {
'userId': 9801258058
'orgId': "9801258058"
}
let body2 = {
'name': "Math",
'email': "[email protected]",
'description': "1 + 1 = 3",
'tags': ["addition", "subtraction"],
'tags': ["addition", "subtraction", "PEMDAS"],
'membershipId': []
}
//console.log(getAllOrgsService());
//let org = addUserService(body1, params1); console.log("Add org: ", org);
//let org_no = addUserService(body1, params1); console.log("Do not add existing org: ", org_no);
//let findOrg = getUserByIdService(body1, params1); console.log("Find added org", findOrg);
//let updateOrg = updateUserByIdService(body2, params1); console.log("Update org: ", updateOrg);
//let deleteOrg = deleteUserByIdService(body2, params1); console.log("Delete org: ", deleteOrg);
//let org = exports.addOrgService({'orgId': "192038"}, body2); console.log("Add org: ", org);
//let org_no = exports.addOrgService(params1, body1); console.log("Do not add existing org: ", org_no);
//let findOrg = exports.getOrgByIdService(params1, {}); console.log("Find added org", findOrg);
//let updateOrg = exports.updateOrgService(params1, body2); console.log("Update org: ", updateOrg);
//let deleteOrg = exports.deleteOrgService(params1, body2); console.log("Delete org: ", deleteOrg);

}, 10000);
13 changes: 7 additions & 6 deletions backend/services/user.services.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ setTimeout(function() { // first set of tests to run (user, org, roommate)
let body1 = {
'name': "Martin",
'email': "[email protected]",
'phoneNumber': "911"
'phoneNumber': "911",
'userId': 102930
}
let params1 = {
'userId': 102930
Expand All @@ -250,9 +251,9 @@ setTimeout(function() { // first set of tests to run (user, org, roommate)
'email': "[email protected]",
'phoneNumber': "0"
}
//let user = addUserService(body1, params1); console.log("Add User: ", user);
//let user_no = addUserService(body1, params1); console.log("Do not add existing user: ", user_no);
//let findUser = getUserByIdService(body1, params1); console.log("Find added user", findUser);
//let updateUser = updateUserByIdService(body2, params1); console.log("Update user: ", updateUser);
//let deleteUser = deleteUserByIdService(body2, params1); console.log("Delete user: ", deleteUser);
//let user = addUserService(params1, body1); console.log("Add User: ", user);
//let user_no = addUserService(params1, body1); console.log("Do not add existing user: ", user_no);
//let findUser = getUserByIdService(params1, body1); console.log("Find added user", findUser);
//let updateUser = updateUserByIdService(params1, body2); console.log("Update user: ", updateUser);
//let deleteUser = deleteUserByIdService(params1, body2); console.log("Delete user: ", deleteUser);
}, 5000);

0 comments on commit b8bba93

Please sign in to comment.