Skip to content

Commit 433a1c8

Browse files
committed
#1412 - enforcing shit, using the right hook
1 parent dee8578 commit 433a1c8

5 files changed

Lines changed: 26 additions & 30 deletions

File tree

src/backend/src/services/work-packages.services.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ export default class WorkPackagesService {
154154
throw new HttpException(400, 'A Work Package cannot have its own project as a blocker');
155155
}
156156

157+
blockedBy.forEach((dep: WbsNumber) => {
158+
if (dep.workPackageNumber === 0) {
159+
throw new HttpException(400, 'A Project cannot be a Blocker');
160+
}
161+
});
162+
157163
const wbsElem = await prisma.wBS_Element.findUnique({
158164
where: {
159165
wbsNumber: {
@@ -287,6 +293,12 @@ export default class WorkPackagesService {
287293
// verify user is allowed to edit work packages
288294
if (isGuest(user.role)) throw new AccessDeniedGuestException('edit work packages');
289295

296+
blockedBy.forEach((dep: WbsNumber) => {
297+
if (dep.workPackageNumber === 0) {
298+
throw new HttpException(400, 'A Project cannot be a Blocker');
299+
}
300+
});
301+
290302
const { userId } = user;
291303

292304
// get the original work package so we can compare things

src/frontend/src/hooks/work-packages.hooks.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,6 @@ export const useSingleWorkPackage = (wbsNum: WbsNumber) => {
3737
});
3838
};
3939

40-
/**
41-
* Custom React Hook to supply multiple work packages
42-
*
43-
* @param wbsNums WBS numbers of the requested work packages
44-
*/
45-
export const useManyWorkPackages = (wbsNums: WbsNumber[]) => {
46-
return useQuery<WorkPackage[], Error>(['work packages', wbsNums], async () => {
47-
const workPackagePromises = wbsNums.map(async (wbsNum) => {
48-
const { data } = await getSingleWorkPackage(wbsNum);
49-
return data;
50-
});
51-
const workPackages = await Promise.all(workPackagePromises);
52-
return workPackages;
53-
});
54-
};
55-
5640
/**
5741
* Custom React Hook to create a new work package.
5842
*

src/frontend/src/pages/ProjectDetailPage/ProjectViewContainer/WorkPackageSummary.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Grid from '@mui/material/Grid';
1818
import { useTheme } from '@mui/material';
1919
import DetailDisplay from '../../../components/DetailDisplay';
2020
import WorkPackageStageChip from '../../../components/WorkPackageStageChip';
21-
import { useManyWorkPackages } from '../../../hooks/work-packages.hooks';
21+
import { useGetBlockingWorkPackages } from '../../../hooks/work-packages.hooks';
2222
import LoadingIndicator from '../../../components/LoadingIndicator';
2323
import ErrorPage from '../../ErrorPage';
2424

@@ -37,7 +37,7 @@ const WorkPackageSummary: React.FC<WorkPackageSummaryProps> = ({ workPackage })
3737
</ul>
3838
);
3939

40-
const { data: dependencies, isError, isLoading, error } = useManyWorkPackages(workPackage.blockedBy);
40+
const { data: dependencies, isError, isLoading, error } = useGetBlockingWorkPackages(workPackage.wbsNum);
4141
const theme = useTheme();
4242

4343
if (!dependencies || isLoading) return <LoadingIndicator />;

src/frontend/src/pages/WorkPackageDetailPage/WorkPackageViewContainer/WorkPackageViewContainer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import KeyboardDoubleArrowUpIcon from '@mui/icons-material/KeyboardDoubleArrowUp
2222
import DoneOutlineIcon from '@mui/icons-material/DoneOutline';
2323
import Delete from '@mui/icons-material/Delete';
2424
import DeleteWorkPackage from '../DeleteWorkPackageModalContainer/DeleteWorkPackage';
25-
import { useManyWorkPackages } from '../../../hooks/work-packages.hooks';
25+
import { useGetBlockingWorkPackages } from '../../../hooks/work-packages.hooks';
2626
import PageLayout from '../../../components/PageLayout';
2727
import LoadingIndicator from '../../../components/LoadingIndicator';
2828
import ErrorPage from '../../ErrorPage';
@@ -52,7 +52,7 @@ const WorkPackageViewContainer: React.FC<WorkPackageViewContainerProps> = ({
5252
const [showStageGateModal, setShowStageGateModal] = useState<boolean>(false);
5353
const [showDeleteModal, setShowDeleteModal] = useState<boolean>(false);
5454
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
55-
const { data: dependencies, isError, isLoading, error } = useManyWorkPackages(workPackage.blockedBy);
55+
const { data: dependencies, isError, isLoading, error } = useGetBlockingWorkPackages(workPackage.wbsNum);
5656
const dropdownOpen = Boolean(anchorEl);
5757
const wbsNum = wbsPipe(workPackage.wbsNum);
5858

src/frontend/src/tests/pages/WorkPackageDetailPage/WorkPackagePage.test.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { UseQueryResult } from 'react-query';
77
import { AuthenticatedUser, WorkPackage } from 'shared';
88
import { render, screen, routerWrapperBuilder, act, fireEvent } from '../../test-support/test-utils';
99
import { Auth } from '../../../utils/types';
10-
import { useManyWorkPackages, useSingleWorkPackage } from '../../../hooks/work-packages.hooks';
10+
import { useGetBlockingWorkPackages, useSingleWorkPackage } from '../../../hooks/work-packages.hooks';
1111
import { useAuth } from '../../../hooks/auth.hooks';
1212
import { mockAuth, mockUseQueryResult } from '../../test-support/test-data/test-utils.stub';
1313
import { exampleDesignWorkPackage, exampleResearchWorkPackage } from '../../test-support/test-data/work-packages.stub';
@@ -25,10 +25,10 @@ const mockSingleWPHook = (isLoading: boolean, isError: boolean, data?: WorkPacka
2525
mockedUseSingleWorkPackage.mockReturnValue(mockUseQueryResult<WorkPackage>(isLoading, isError, data, error));
2626
};
2727

28-
const mockedUseManyWorkPackages = useManyWorkPackages as jest.Mock<UseQueryResult<WorkPackage[]>>;
28+
const mockedGetBlockingWorkPackages = useGetBlockingWorkPackages as jest.Mock<UseQueryResult<WorkPackage[]>>;
2929

30-
const mockManyWorkPackagesHook = (isLoading: boolean, isError: boolean, data?: WorkPackage[], error?: Error) => {
31-
mockedUseManyWorkPackages.mockReturnValue(mockUseQueryResult<WorkPackage[]>(isLoading, isError, data, error));
30+
const mockGetBlockingWorkPackagesHook = (isLoading: boolean, isError: boolean, data?: WorkPackage[], error?: Error) => {
31+
mockedGetBlockingWorkPackages.mockReturnValue(mockUseQueryResult<WorkPackage[]>(isLoading, isError, data, error));
3232
};
3333

3434
vi.mock('../../../hooks/auth.hooks');
@@ -63,7 +63,7 @@ describe('work package container', () => {
6363
mockSingleWPHook(true, false);
6464
mockAuthHook();
6565
mockCurrentUserHook();
66-
mockManyWorkPackagesHook(true, false);
66+
mockGetBlockingWorkPackagesHook(true, false);
6767
renderComponent();
6868

6969
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
@@ -74,7 +74,7 @@ describe('work package container', () => {
7474
mockSingleWPHook(false, false, exampleResearchWorkPackage);
7575
mockAuthHook();
7676
mockCurrentUserHook();
77-
mockManyWorkPackagesHook(false, false, [exampleDesignWorkPackage]);
77+
mockGetBlockingWorkPackagesHook(false, false, [exampleDesignWorkPackage]);
7878
renderComponent();
7979

8080
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
@@ -87,7 +87,7 @@ describe('work package container', () => {
8787
mockSingleWPHook(false, true, undefined, new Error('404 could not find the requested work package'));
8888
mockAuthHook();
8989
mockCurrentUserHook();
90-
mockManyWorkPackagesHook(false, false);
90+
mockGetBlockingWorkPackagesHook(false, false);
9191
renderComponent();
9292

9393
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
@@ -99,7 +99,7 @@ describe('work package container', () => {
9999
mockSingleWPHook(false, true);
100100
mockAuthHook();
101101
mockCurrentUserHook();
102-
mockManyWorkPackagesHook(false, false);
102+
mockGetBlockingWorkPackagesHook(false, false);
103103
renderComponent();
104104

105105
expect(screen.queryByText('Loading...')).not.toBeInTheDocument();
@@ -111,7 +111,7 @@ describe('work package container', () => {
111111
mockSingleWPHook(false, false, exampleResearchWorkPackage);
112112
mockAuthHook(exampleAdminUser);
113113
mockCurrentUserHook();
114-
mockManyWorkPackagesHook(false, false, [exampleDesignWorkPackage]);
114+
mockGetBlockingWorkPackagesHook(false, false, [exampleDesignWorkPackage]);
115115
renderComponent();
116116

117117
act(() => {
@@ -124,7 +124,7 @@ describe('work package container', () => {
124124
mockSingleWPHook(false, false, exampleResearchWorkPackage);
125125
mockAuthHook(exampleGuestUser);
126126
mockCurrentUserHook(exampleGuestUser);
127-
mockManyWorkPackagesHook(false, false, [exampleDesignWorkPackage]);
127+
mockGetBlockingWorkPackagesHook(false, false, [exampleDesignWorkPackage]);
128128
renderComponent();
129129

130130
act(() => {

0 commit comments

Comments
 (0)