Skip to content

Commit

Permalink
moved approve all reviews to backend endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
qiandrewj committed May 7, 2024
1 parent 54ce412 commit 46f7594
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 53 deletions.
54 changes: 23 additions & 31 deletions client/src/modules/Admin/Components/Admin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,20 @@ export const Admin = () => {
// pending (awaiting approval), and reported (hidden and awaiting approval)
useEffect(() => {
async function loadReviews() {
const pending = await axios.post('/api/admin/reviews/pending', {
const pending = await axios.post('/api/admin/reviews/get/pending', {
token: token,
})
if (pending.status === 200) {
setPendingReviews(pending.data.result)
}

}


axios
.post('/api/fetchPendingReviews', { token: token })
.then((response) => {
const result = response.data.result
if (response.status === 200) {
setPendingReviews(result)
}
})
axios
.post('/api/fetchReportedReviews', { token: token })
.then((response) => {
const result = response.data.result
if (response.status === 200) {
setReportedReviews(result)
}
const reported = await axios.post('/api/admin/reviews/get/reported', {
token: token
})
if (reported.status === 200) {
setReportedReviews(reported.data.result);
}
}
loadReviews()
}, [token, isAuthenticating])

// Helper function to remove a review from a list of reviews and
Expand All @@ -93,7 +81,7 @@ export const Admin = () => {
// Call when user asks to approve a review. Accesses the Reviews database
// and changes the review with this id to visible.
async function approveReview(review: Review) {
const response = await axios.post('/api/makeReviewVisible', {
const response = await axios.post('/api/admin/reviews/approve', {
review: review,
token: token,
})
Expand All @@ -108,7 +96,7 @@ export const Admin = () => {
// and deletes the review with this id.
async function removeReview(review: Review, isUnapproved: boolean) {
try {
const response = await axios.post('/api/removeReview', {
const response = await axios.post('/api/admin/reviews/remove', {
review: review,
token: token,
})
Expand All @@ -133,17 +121,20 @@ export const Admin = () => {
}

// Call when admin would like to mass-approve all of the currently pending reviews.
function approveAllReviews(reviews: Review[]) {
reviews.forEach((review: Review) => {
approveReview(review)
})
async function approveAllReviews(reviews: Review[]) {
const response = await axios.post('/api/admin/reviews/approve/all', {token: token})
if (response.status === 200) {
setPendingReviews([])
} else {
alert('Could not approve all reviews')
}
}

// Call when user asks to un-report a reported review. Accesses the Reviews database
// and changes the reported flag for this review to false.
function unReportReview(review: Review) {
axios
.post('/api/undoReportReview', {
.post('/api/reviews/unreport', {
review: review,
token: token,
})
Expand All @@ -168,7 +159,7 @@ export const Admin = () => {
setLoadingSemester(1)

axios
.post('/api/addNewSemester', {
.post('/api/admin/semester/add', {
semester,
token: token,
})
Expand Down Expand Up @@ -198,7 +189,7 @@ export const Admin = () => {
setDisableInit(true)
setLoadingInit(1)

axios.post('/api/dbInit', { token: token }).then((response) => {
axios.post('/api/admin/db/initialize', { token: token }).then((response) => {
if (response.status === 200) {
setDisableInit(false)
setLoadingInit(2)
Expand All @@ -213,7 +204,7 @@ export const Admin = () => {
setDisableInit(true)
setLoadingProfs(1)

axios.post('/api/setProfessors', { token: token }).then((response) => {
axios.post('/api/admin/professors/add', { token: token }).then((response) => {
if (response.status === 200) {
console.log('Updated the professors')
setDisableInit(false)
Expand All @@ -229,7 +220,7 @@ export const Admin = () => {
setDisableInit(true)
setResettingProfs(1)

axios.post('/api/resetProfessors', { token: token }).then((response) => {
axios.post('/api/admin/professors/reset', { token: token }).then((response) => {
if (response.status === 200) {
console.log('Reset all the professors to empty arrays')
setDisableInit(false)
Expand Down Expand Up @@ -398,6 +389,7 @@ export const Admin = () => {
)
})}
</div>
<br></br>
<h1>Reported Reviews</h1>
<div className="ReportedReviews">
{reportedReviews.map((review: Review) => {
Expand Down
6 changes: 3 additions & 3 deletions client/src/modules/Admin/Components/ManageAdminModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const ManageAdminModal = ({token, open, setOpen}: Props) => {
*/
useEffect(() => {
axios
.post('/api/getAdmins', {token: token})
.post('/api/admin/users/get', {token: token})
.then((response) => {
const result = response.data.result
if (response.status === 200) {
Expand All @@ -43,7 +43,7 @@ const ManageAdminModal = ({token, open, setOpen}: Props) => {

function removeAdmin(user: Student) {
axios
.post('/api/removeAdmin', {
.post('/api/admin/users/remove', {
userId: user.netId,
token: token
})
Expand All @@ -64,7 +64,7 @@ const ManageAdminModal = ({token, open, setOpen}: Props) => {

function addAdminByNetId(_netId: string) {
axios
.post('/api/grantAdmin', {
.post('/api/admin/users/add', {
userId: _netId,
token: token
})
Expand Down
4 changes: 2 additions & 2 deletions client/src/modules/Admin/Components/Stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const Stats = ({token}: StatsProps) => {
*/
useEffect(() => {
axios
.post('/api/countReviews', {token: token})
.post('/api/admin/reviews/count', {token: token})
.then((response) => {
const result = response.data.result
if (response.status === 200) {
Expand All @@ -41,7 +41,7 @@ const Stats = ({token}: StatsProps) => {
let csv = ""

await axios
.post('/api/getCourseCSV', {token: token})
.post('/api/admin/reviews/csv', {token: token})
.then((response) => {
const result = response.data.result
if (response.status === 200) {
Expand Down
2 changes: 1 addition & 1 deletion server/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { courseRouter } from "./src/course";
import { adminRouter } from "./src/admin";

export const configure = (app: Express) => {
app.use("/api/admin", adminRouter)
app.use(
"/api",
authRouter,
searchRouter,
profileRouter,
reviewRouter,
courseRouter,
adminRouter,
);
};
8 changes: 8 additions & 0 deletions server/src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
removeAdminPrivilege,
grantAdminPrivilege,
createNewAdminUser,
approveAllReviews,
} from './admin.data-access';
import {
AdminAddSemesterType,
Expand Down Expand Up @@ -110,6 +111,13 @@ export const editReviewVisibility = async ({
return false;
};

export const approveReviews = async ({ auth }: VerifyAdminType) => {
const userIsAdmin = await verifyTokenAdmin({ auth });
if (userIsAdmin) {
return approveAllReviews();
}
}

/**
* Removes a review from db by mongo generated id.
*
Expand Down
4 changes: 4 additions & 0 deletions server/src/admin/admin.data-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ export const updateReviewVisibility = async (
await Reviews.updateOne({ _id: reviewId }, { $set: { visible, reported } });
};

export const approveAllReviews = async () => {
await Reviews.updateMany({ visible: 0, reported: 0 }, { $set: { visible: 1 } });
};

/*
* Functions for the manage admin functionality. Enables grant admin privilege to a
* user, removing admin privilege from a user, and retrieving all priviliged users.
Expand Down
53 changes: 37 additions & 16 deletions server/src/admin/admin.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import {
getAdminUsers,
removeAdmin,
addOrUpdateAdmin,
approveReviews
} from './admin.controller';

export const adminRouter = express.Router();

adminRouter.post('/reportReview', async (req, res) => {
adminRouter.post('/reviews/report', async (req, res) => {
try {
const { id }: ReportReviewRequestType = req.body;
const result = await reportReview({ id });
Expand All @@ -52,7 +53,7 @@ adminRouter.post('/reportReview', async (req, res) => {
/*
* Check if a token is for an admin
*/
adminRouter.post('/tokenIsAdmin', async (req, res) => {
adminRouter.post('/validate/token', async (req, res) => {
try {
const { token } = req.body;
const auth: Auth = new Auth({ token });
Expand All @@ -65,7 +66,7 @@ adminRouter.post('/tokenIsAdmin', async (req, res) => {
}
});

adminRouter.post('/makeReviewVisible', async (req, res) => {
adminRouter.post('/reviews/approve', async (req, res) => {
try {
const { token, review }: AdminReviewRequestType = req.body;
const auth = new Auth({ token });
Expand Down Expand Up @@ -98,7 +99,27 @@ adminRouter.post('/makeReviewVisible', async (req, res) => {
}
});

adminRouter.post('/fetchPendingReviews', async (req, res) => {
adminRouter.post('/reviews/approve/all', async (req, res) => {
try {
const { token }: AdminReviewRequestType = req.body;
const auth = new Auth({ token });

const response = await approveReviews({auth: auth})
if (response !== null) {
return res.status(200).json({
message: `All pending reviews have been approved`
})
} else {
return res.status(400).json({
error: `User is not an admin`
})
}
} catch (err) {
return res.status(500).json({ error: `Internal Server Error: ${err}` });
}
});

adminRouter.post('/reviews/get/pending', async (req, res) => {
try {
const { token }: AdminRequestType = req.body;
const auth = new Auth({ token });
Expand All @@ -118,7 +139,7 @@ adminRouter.post('/fetchPendingReviews', async (req, res) => {
}
});

adminRouter.post('/fetchReportedReviews', async (req, res) => {
adminRouter.post('/reviews/get/reported', async (req, res) => {
try {
const { token }: AdminRequestType = req.body;
const auth = new Auth({ token });
Expand All @@ -138,7 +159,7 @@ adminRouter.post('/fetchReportedReviews', async (req, res) => {
}
});

adminRouter.post('/countReviews', async (req, res) => {
adminRouter.post('/reviews/count', async (req, res) => {
try {
const { token }: AdminRequestType = req.body;
const auth = new Auth({ token });
Expand All @@ -159,7 +180,7 @@ adminRouter.post('/countReviews', async (req, res) => {
}
})

adminRouter.post('/getCourseCSV', async (req, res) => {
adminRouter.post('/reviews/csv', async (req, res) => {
try {
const { token }: AdminRequestType = req.body;
const auth = new Auth({ token });
Expand All @@ -180,7 +201,7 @@ adminRouter.post('/getCourseCSV', async (req, res) => {
}
})

adminRouter.post('/getAdmins', async (req, res) => {
adminRouter.post('/users/get', async (req, res) => {
try {
const { token }: AdminRequestType = req.body;
const auth = new Auth({ token });
Expand All @@ -201,7 +222,7 @@ adminRouter.post('/getAdmins', async (req, res) => {
}
})

adminRouter.post('/removeAdmin', async (req, res) => {
adminRouter.post('/users/remove', async (req, res) => {
const {token, userId}: AdminUserRequestType = req.body;

try {
Expand All @@ -222,7 +243,7 @@ adminRouter.post('/removeAdmin', async (req, res) => {
}
})

adminRouter.post('/grantAdmin', async (req, res) => {
adminRouter.post('/users/add', async (req, res) => {
const {token, userId}: AdminUserRequestType = req.body;

try {
Expand All @@ -243,7 +264,7 @@ adminRouter.post('/grantAdmin', async (req, res) => {
}
})

adminRouter.post('/addNewSemester', async (req, res) => {
adminRouter.post('/semester/add', async (req, res) => {
const { semester, token }: AdminAddSemesterRequestType = req.body;
try {
const auth = new Auth({ token });
Expand All @@ -270,7 +291,7 @@ adminRouter.post('/addNewSemester', async (req, res) => {
}
});

adminRouter.post('/undoReportReview', async (req, res) => {
adminRouter.post('/reviews/unreport', async (req, res) => {
const { review, token }: AdminReviewRequestType = req.body;
try {
const auth = new Auth({ token });
Expand All @@ -296,7 +317,7 @@ adminRouter.post('/undoReportReview', async (req, res) => {
}
});

adminRouter.post('/removeReview', async (req, res) => {
adminRouter.post('/reviews/remove', async (req, res) => {
const { review, token }: AdminReviewRequestType = req.body;

try {
Expand All @@ -318,7 +339,7 @@ adminRouter.post('/removeReview', async (req, res) => {
}
});

adminRouter.post('/setProfessors', async (req, res) => {
adminRouter.post('/professors/add', async (req, res) => {
const { token }: AdminRequestType = req.body;
try {
const auth = new Auth({ token });
Expand All @@ -334,7 +355,7 @@ adminRouter.post('/setProfessors', async (req, res) => {
}
});

adminRouter.post('/resetProfessors', async (req, res) => {
adminRouter.post('/professors/reset', async (req, res) => {
const { token }: AdminRequestType = req.body;
try {
const auth = new Auth({ token });
Expand All @@ -355,7 +376,7 @@ adminRouter.post('/resetProfessors', async (req, res) => {
}
});

adminRouter.post('/dbInit', async (req, res) => {
adminRouter.post('/db/initialize', async (req, res) => {
const { token }: AdminRequestType = req.body;
try {
const auth = new Auth({ token });
Expand Down

0 comments on commit 46f7594

Please sign in to comment.