Skip to content

Commit

Permalink
Merge pull request #1569 from Northeastern-Electric-Racing/#1568-fina…
Browse files Browse the repository at this point in the history
…nce-members-all-permissions

#1568 - all finance members have all perms
  • Loading branch information
RChandler234 authored Sep 30, 2023
2 parents c284b2e + 2c62cf0 commit e777fde
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 30 deletions.
5 changes: 2 additions & 3 deletions src/backend/src/services/reimbursement-requests.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
removeDeletedReceiptPictures,
updateReimbursementProducts,
validateReimbursementProducts,
validateUserIsHeadOfFinanceTeam,
validateUserIsPartOfFinanceTeam
} from '../utils/reimbursement-requests.utils';
import {
Expand Down Expand Up @@ -334,7 +333,7 @@ export default class ReimbursementRequestService {
* @returns reimbursement requests with no advisor approved reimbursement status
*/
static async getPendingAdvisorList(requester: User): Promise<ReimbursementRequest[]> {
await validateUserIsHeadOfFinanceTeam(requester);
await validateUserIsPartOfFinanceTeam(requester);

const requestsPendingAdvisors = await prisma.reimbursement_Request.findMany({
where: {
Expand All @@ -360,7 +359,7 @@ export default class ReimbursementRequestService {
* @param saboNumbers the sabo numbers of the reimbursement requests to send
*/
static async sendPendingAdvisorList(sender: User, saboNumbers: number[]) {
await validateUserIsHeadOfFinanceTeam(sender);
await validateUserIsPartOfFinanceTeam(sender);

if (saboNumbers.length === 0) throw new HttpException(400, 'Need to send at least one Sabo #!');

Expand Down
12 changes: 0 additions & 12 deletions src/backend/src/utils/reimbursement-requests.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,3 @@ export const isAuthUserHeadOfFinance = (user: Prisma.UserGetPayload<typeof authU
const isTeamIdInList = (teamId: string, teamsList: Team[]) => {
return teamsList.map((team) => team.teamId).includes(teamId);
};

export const validateUserIsHeadOfFinanceTeam = async (user: User) => {
const financeTeam = await prisma.team.findUnique({
where: { teamId: process.env.FINANCE_TEAM_ID }
});

if (!financeTeam) throw new HttpException(500, 'Finance team does not exist!');

if (!(financeTeam.headId === user.userId)) {
throw new AccessDeniedException('You are not the head of the finance team!');
}
};
15 changes: 6 additions & 9 deletions src/backend/tests/reimbursement-requests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,21 @@ describe('Reimbursement Requests', () => {
// assert
expect(prismaFindTeamSpy).toBeCalledTimes(1);
expect(prismaFindTeamSpy).toBeCalledWith({
where: { teamId: process.env.FINANCE_TEAM_ID }
where: { teamId: process.env.FINANCE_TEAM_ID },
include: { head: true, leads: true, members: true }
});
});

test('fails if user is not head of finance team', async () => {
test('fails if user is not on finance team', async () => {
// mock prisma calls
const prismaGetManySpy = vi.spyOn(prisma.reimbursement_Request, 'findMany').mockResolvedValue([findManyResult]);
const prismaFindTeamSpy = vi.spyOn(prisma.team, 'findUnique').mockResolvedValue(prismaTeam1);
vi.spyOn(prisma.team, 'findUnique').mockResolvedValue({ ...primsaTeam2, headId: 1 });

// act
const action = async () => await ReimbursementRequestService.getPendingAdvisorList(batman);
await expect(action).rejects.toEqual(new AccessDeniedException('You are not the head of the finance team!'));
const action = async () => await ReimbursementRequestService.getPendingAdvisorList(alfred);
await expect(action).rejects.toEqual(new AccessDeniedException(`You are not a member of the finance team!`));

// assert
expect(prismaFindTeamSpy).toBeCalledTimes(1);
expect(prismaFindTeamSpy).toBeCalledWith({
where: { teamId: process.env.FINANCE_TEAM_ID }
});
expect(prismaGetManySpy).toBeCalledTimes(0);
});
});
Expand Down
12 changes: 6 additions & 6 deletions src/frontend/src/pages/FinancePage/FinancePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const FinancePage = () => {
error: allPendingAdvisorListError
} = useGetPendingAdvisorList();

const { isFinance, isHeadOfFinance } = user;
const { isFinance } = user;

const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);

Expand All @@ -60,16 +60,16 @@ const FinancePage = () => {

if (isFinance && allReimbursementRequestsIsError) return <ErrorPage message={allReimbursementRequestsError?.message} />;
if (userReimbursementRequestIsError) return <ErrorPage message={userReimbursementRequestError?.message} />;
if (isHeadOfFinance && allPendingAdvisorListIsError) return <ErrorPage message={allPendingAdvisorListError?.message} />;
if (isFinance && allPendingAdvisorListIsError) return <ErrorPage message={allPendingAdvisorListError?.message} />;
if (
(isFinance && (allReimbursementRequestsIsLoading || !allReimbursementRequests)) ||
userReimbursementRequestIsLoading ||
!userReimbursementRequests ||
(isHeadOfFinance && !allPendingAdvisorList)
(isFinance && !allPendingAdvisorList)
)
return <LoadingIndicator />;

if (isHeadOfFinance && (!allPendingAdvisorList || allPendingAdvisorListIsLoading)) return <LoadingIndicator />;
if (isFinance && (!allPendingAdvisorList || allPendingAdvisorListIsLoading)) return <LoadingIndicator />;

const handleClick = (event: React.MouseEvent<HTMLElement>) => {
setAnchorEl(event.currentTarget);
Expand Down Expand Up @@ -112,7 +112,7 @@ const FinancePage = () => {
handleDropdownClose();
setShowPendingAdvisorListModal(true);
}}
disabled={!isHeadOfFinance}
disabled={!isFinance}
>
<ListItemIcon>
<ListAltIcon fontSize="small" />
Expand All @@ -131,7 +131,7 @@ const FinancePage = () => {

return (
<PageLayout title="Finance" headerRight={financeActionsDropdown}>
{isHeadOfFinance && (
{isFinance && (
<PendingAdvisorModal
open={showPendingAdvisorListModal}
saboNumbers={allPendingAdvisorList!.map((reimbursementRequest) => reimbursementRequest.saboId!)}
Expand Down

0 comments on commit e777fde

Please sign in to comment.