Skip to content

Commit 0b47408

Browse files
committed
#1522: check if the material type already exists with test
1 parent ceb1366 commit 0b47408

4 files changed

Lines changed: 25 additions & 5 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ projectRouter.post(
4747
projectRouter.post('/:wbsNum/set-team', nonEmptyString(body('teamId')), validateInputs, ProjectsController.setProjectTeam);
4848
projectRouter.delete('/:wbsNum/delete', ProjectsController.deleteProject);
4949
projectRouter.post('/:wbsNum/favorite', ProjectsController.toggleFavorite);
50+
51+
/**************** BOM Section ****************/
5052
projectRouter.post('/bom/material-type/create', nonEmptyString(body('name')), ProjectsController.createMaterialType);
5153

5254
export default projectRouter;

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,18 +607,27 @@ export default class ProjectsService {
607607
* Create a new material type
608608
* @param name the name of the new material type
609609
* @param submitter the user who is creating the material type
610+
* @throws if the submitter is a leader or the material type with given name already exists
610611
*/
611612
static async createMaterialType(name: string, submitter: User): Promise<Material_Type> {
612613
if (!isLeadership(submitter.role)) throw new AccessDeniedAdminOnlyException('create material type');
613614

614-
const materialType = await prisma.material_Type.create({
615+
const materialType = await prisma.material_Type.findUnique({
616+
where: {
617+
name
618+
}
619+
});
620+
621+
if (!!materialType) throw new HttpException(400, `The following material type alraedy exists: ${name}`);
622+
623+
const newMaterialType = await prisma.material_Type.create({
615624
data: {
616625
name,
617626
dateCreated: new Date(),
618627
creatorId: submitter.userId
619628
}
620629
});
621630

622-
return materialType;
631+
return newMaterialType;
623632
}
624633
}

src/backend/tests/projects.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,20 @@ describe('Projects', () => {
260260
);
261261
});
262262

263+
test('Create material type fails if the material type with the given name already exists'),
264+
async () => {
265+
vi.spyOn(prisma.material_Type, 'findUnique').mockResolvedValue(toolMaterial);
266+
267+
await expect(ProjectsService.createMaterialType('NERSoftwareTools', batman)).rejects.toThrow(
268+
new HttpException(400, 'The following material type alraedy exists: NERSoftwareTools')
269+
);
270+
};
271+
263272
test('Create material type works', async () => {
264273
vi.spyOn(prisma.material_Type, 'create').mockResolvedValue(toolMaterial);
265274

266-
const materialType = await ProjectsService.createMaterialType('Tools', batman);
267-
expect(materialType.name).toBe('Tools');
275+
const materialType = await ProjectsService.createMaterialType('NERSoftwareTools', batman);
276+
expect(materialType.name).toBe('NERSoftwareTools');
268277
expect(prisma.material_Type.create).toBeCalledTimes(1);
269278
});
270279
});

src/backend/tests/test-data/projects.test-data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export const sharedProject1: SharedProject = {
104104
};
105105

106106
export const toolMaterial: PrismaMaterialType = {
107-
name: 'Tools',
107+
name: 'NERSoftwareTools',
108108
dateCreated: new Date(),
109109
creatorId: batman.userId
110110
};

0 commit comments

Comments
 (0)