Skip to content

Commit

Permalink
admin management modal can no longer duplicate users
Browse files Browse the repository at this point in the history
  • Loading branch information
qiandrewj committed Sep 26, 2024
1 parent 4361073 commit 6502c60
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 32 deletions.
6 changes: 1 addition & 5 deletions server/src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
findAdminUsers,
removeAdminPrivilege,
grantAdminPrivilege,
createNewAdminUser,
approveAllReviews,
} from './admin.data-access';
import {
Expand Down Expand Up @@ -237,13 +236,10 @@ export const removeAdmin = async ({auth, id}: VerifyManageAdminType) => {
* @param {string} id: String identifying the user by netid
* @returns The user with updated admin privilege if operation was successful, null otherwise
*/
export const addOrUpdateAdmin = async ({auth, id}: VerifyManageAdminType) => {
export const addAdmin = async ({auth, id}: VerifyManageAdminType) => {
const userIsAdmin = await verifyTokenAdmin({ auth });
if (userIsAdmin) {
let res = await grantAdminPrivilege(id);
if (res.nModified === 0) {
res = await createNewAdminUser(id);
}
return res;
}
}
Expand Down
25 changes: 1 addition & 24 deletions server/src/admin/admin.data-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,29 +131,6 @@ export const removeAdminPrivilege = async (id: string) => {
}

export const grantAdminPrivilege = async (id: string) => {
const res = await Students.updateOne({ netId: id }, { $set: {privilege: "admin"} }).exec()
return res
}

/*
* If there is an attempt to grant admin privilege to someone not in the database,
* a new user will be created with the given netid and added to the database.
*/

export const createNewAdminUser = async (id: string) => {

const admin: InsertStudentType = {
_id: shortid.generate(),
firstName: '',
lastName: '',
netId: id,
affiliation: '',
token: '',
privilege: 'admin',
};

const newAdmin = new Students(admin);
const res = await newAdmin.save();

const res = await Students.updateOne({ netId: id }, { $set: {privilege: 'admin'} }).exec()
return res
}
5 changes: 2 additions & 3 deletions server/src/admin/admin.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
AdminReviewRequestType,
AdminRequestType,
AdminUserRequestType,
RaffleWinnerRequestType,
AdminAddSemesterRequestType,
ReportReviewRequestType,
} from './admin.type';
Expand All @@ -24,7 +23,7 @@ import {
reportReview,
getAdminUsers,
removeAdmin,
addOrUpdateAdmin,
addAdmin,
approveReviews
} from './admin.controller';

Expand Down Expand Up @@ -248,7 +247,7 @@ adminRouter.post('/users/add', async (req, res) => {

try {
const auth = new Auth({ token });
const result = await addOrUpdateAdmin({ auth: auth, id: userId})
const result = await addAdmin({ auth: auth, id: userId})

if (result) {
return res.status(200).json({
Expand Down

0 comments on commit 6502c60

Please sign in to comment.