Skip to content

Commit 64b1950

Browse files
committed
#1528 - fix tests
1 parent 5c2e692 commit 64b1950

3 files changed

Lines changed: 21 additions & 22 deletions

File tree

src/backend/src/routes/projects.routes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ projectRouter.post('/bom/material-type/create', nonEmptyString(body('name')), Pr
6262
projectRouter.post(
6363
'/bom/assembly/:wbsNum/create',
6464
nonEmptyString(body('name')),
65-
nonEmptyString(body('pdmFileName')).optional(),
65+
nonEmptyString(body('pdmFileName').optional()),
6666
ProjectsController.createAssembly
6767
);
6868
projectRouter.post(
6969
'/bom/material/:materialId/assign-assembly',
70-
nonEmptyString(body('assemblyId')),
70+
nonEmptyString(body('assemblyId').optional()),
7171
validateInputs,
7272
ProjectsController.assignMaterialAssembly
7373
);

src/backend/src/services/projects.services.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -887,9 +887,10 @@ export default class ProjectsService {
887887
* @throws if the submitter does not have the relevant positions
888888
* @returns the updated material
889889
*/
890-
static async assignMaterialAssembly(submitter: User, materialId: string, assemblyId: undefined | string) {
890+
static async assignMaterialAssembly(submitter: User, materialId: string, assemblyId?: string) {
891891
const material = await prisma.material.findUnique({ where: { materialId } });
892892
if (!material) throw new NotFoundException('Material', materialId);
893+
893894
const project = await prisma.project.findFirst({
894895
where: {
895896
wbsElementId: material.wbsElementId
@@ -902,26 +903,22 @@ export default class ProjectsService {
902903

903904
// Permission: leadership and up, anyone on project team
904905
if (!(isLeadership(submitter.role) || isUserPartOfTeams(project.teams, submitter)))
905-
throw new AccessDeniedException('Only leadership or above can create a material type');
906-
907-
if (assemblyId === undefined) {
908-
// Unassign material from current assembly if assembly id is undefined
909-
const updatedMaterial = await prisma.material.update({ where: { materialId }, data: { assemblyId: undefined } });
910-
return updatedMaterial;
911-
}
906+
throw new AccessDeniedException('Only leadership or above can assign materials to assemblies');
912907

913-
const assembly = await prisma.assembly.findUnique({
914-
where: { assemblyId },
915-
include: { wbsElement: true }
916-
});
917-
if (!assembly) throw new NotFoundException('Assembly', assemblyId);
908+
if (assemblyId) {
909+
const assembly = await prisma.assembly.findUnique({
910+
where: { assemblyId },
911+
include: { wbsElement: true }
912+
});
913+
if (!assembly) throw new NotFoundException('Assembly', assemblyId);
918914

919-
// Confirm that the assembly's wbsElement is the same as the material's wbsElement
920-
if (material.wbsElementId !== assembly.wbsElementId)
921-
throw new HttpException(
922-
400,
923-
`The WBS element of the material (${material.wbsElementId}) and assembly (${assembly.wbsElementId}) do not match`
924-
);
915+
// Confirm that the assembly's wbsElement is the same as the material's wbsElement
916+
if (material.wbsElementId !== assembly.wbsElementId)
917+
throw new HttpException(
918+
400,
919+
`The WBS element of the material (${material.wbsElementId}) and assembly (${assembly.wbsElementId}) do not match`
920+
);
921+
}
925922

926923
// Assign a material on a project to a different assembly
927924
const updatedMaterial = await prisma.material.update({ where: { materialId }, data: { assemblyId } });

src/backend/tests/projects.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ describe('Projects', () => {
734734
vi.spyOn(prisma.project, 'findFirst').mockResolvedValue(project);
735735

736736
await expect(ProjectsService.assignMaterialAssembly(theVisitor, 'mid', 'aid')).rejects.toThrow(
737-
new AccessDeniedException('Only leadership or above can create a material type')
737+
new AccessDeniedException('Only leadership or above can assign materials to assemblies')
738738
);
739739
});
740740

@@ -798,6 +798,8 @@ describe('Projects', () => {
798798
});
799799

800800
test('Deleting assembly fails if assembly does not exist', async () => {
801+
vi.spyOn(prisma.assembly, 'findUnique').mockResolvedValue(null);
802+
801803
await expect(ProjectsService.deleteAssembly('New Assembly', batman)).rejects.toThrow(
802804
new NotFoundException('Assembly', 'New Assembly')
803805
);

0 commit comments

Comments
 (0)