Skip to content

Commit 19991ae

Browse files
committed
#1520: added get all material types endpoint with the tests
1 parent 7c5a0b4 commit 19991ae

6 files changed

Lines changed: 81 additions & 2 deletions

File tree

src/backend/src/controllers/projects.controllers.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Project, validateWBS, WbsNumber, wbsPipe } from 'shared';
22
import { NextFunction, Request, Response } from 'express';
3-
import { Manufacturer, User } from '@prisma/client';
3+
import { Manufacturer, Material_Type, User } from '@prisma/client';
44
import { getCurrentUser } from '../utils/auth.utils';
55
import ProjectsService from '../services/projects.services';
66

@@ -203,6 +203,16 @@ export default class ProjectsController {
203203
}
204204
}
205205

206+
static async getAllMaterialTypes(req: Request, res: Response, next: NextFunction) {
207+
try {
208+
const user = await getCurrentUser(res);
209+
const materialTypes: Material_Type[] = await ProjectsService.getAllMaterialTypes(user);
210+
return res.status(200).json(materialTypes);
211+
} catch (error: unknown) {
212+
next(error);
213+
}
214+
}
215+
206216
static async createMaterialType(req: Request, res: Response, next: NextFunction) {
207217
try {
208218
const { name } = req.body;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* This file is part of NER's FinishLine and licensed under GNU AGPLv3.
3+
* See the LICENSE file in the repository root folder for details.
4+
*/
5+
6+
import { Prisma } from '@prisma/client';
7+
8+
const materialTypeQueryArgs = Prisma.validator<Prisma.Material_TypeArgs>()({
9+
include: {
10+
materials: true
11+
}
12+
});
13+
14+
export default materialTypeQueryArgs;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ projectRouter.post(
5757
ProjectsController.createManufacturer
5858
);
5959
projectRouter.get('/bom/manufacturer', ProjectsController.getAllManufacturers);
60+
projectRouter.get('/bom/material-type', ProjectsController.getAllMaterialTypes);
6061
projectRouter.post('/bom/material-type/create', nonEmptyString(body('name')), ProjectsController.createMaterialType);
6162
projectRouter.post(
6263
'/bom/assembly/:wbsNum/create',

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ import {
2929
import linkQueryArgs from '../prisma-query-args/links.query-args';
3030
import linkTypeQueryArgs from '../prisma-query-args/link-types.query-args';
3131
import manufacturerQueryArgs from '../prisma-query-args/manufacturers.query-args';
32+
import materialTypeQueryArgs from '../prisma-query-args/material-type.query-args';
3233
import { linkTypeTransformer } from '../transformers/links.transformer';
3334
import { updateLinks, linkToChangeListValue } from '../utils/links.utils';
3435
import { manufacturerTransformer } from '../transformers/manufacturer.transformer';
3536
import { isUserPartOfTeams } from '../utils/teams.utils';
37+
import { materialTypeTransformer } from '../transformers/material-type.transformer';
3638

3739
export default class ProjectsService {
3840
/**
@@ -760,7 +762,7 @@ export default class ProjectsService {
760762
return assembly;
761763
}
762764

763-
/*
765+
/**
764766
* Creates a new Manufacturer
765767
* @param submitter the user who's creating the manufacturer
766768
* @param name the name of the manufacturer
@@ -787,6 +789,7 @@ export default class ProjectsService {
787789

788790
/**
789791
* Get all the manufacturers in the database.
792+
* @param submitter the user who's getting all manufacturers
790793
* @returns all the manufacturers
791794
*/
792795
static async getAllManufacturers(submitter: User): Promise<Manufacturer[]> {
@@ -801,6 +804,23 @@ export default class ProjectsService {
801804
).map(manufacturerTransformer);
802805
}
803806

807+
/**
808+
* Get all the material types in the database.
809+
* @param submitter the user who's getting all material types
810+
* @returns all the material types
811+
*/
812+
static async getAllMaterialTypes(submitter: User): Promise<Material_Type[]> {
813+
if (submitter.role === Role.GUEST) {
814+
throw new AccessDeniedGuestException('Get Material Types');
815+
}
816+
817+
return (
818+
await prisma.material_Type.findMany({
819+
...materialTypeQueryArgs
820+
})
821+
).map(materialTypeTransformer);
822+
}
823+
804824
/**
805825
* Create a new material type
806826
* @param name the name of the new material type
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* This file is part of NER's FinishLine and licensed under GNU AGPLv3.
3+
* See the LICENSE file in the repository root folder for details.
4+
*/
5+
6+
import { Material_Type, Prisma } from '@prisma/client';
7+
import materialTypeQueryArgs from '../prisma-query-args/material-type.query-args';
8+
9+
export const materialTypeTransformer = (
10+
materialType: Prisma.Material_TypeGetPayload<typeof materialTypeQueryArgs>
11+
): Material_Type => {
12+
return {
13+
name: materialType.name,
14+
dateCreated: materialType.dateCreated,
15+
creatorId: materialType.creatorId
16+
};
17+
};

src/backend/tests/projects.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,5 +682,22 @@ describe('Projects', () => {
682682
expect(materialType.name).toBe('NERSoftwareTools');
683683
expect(prisma.material_Type.create).toBeCalledTimes(1);
684684
});
685+
686+
test('Get all Material Types fails from guest', async () => {
687+
vi.spyOn(prisma.material_Type, 'findMany').mockResolvedValue([]);
688+
689+
await expect(ProjectsService.getAllMaterialTypes(theVisitor)).rejects.toThrow(
690+
new AccessDeniedGuestException('Get Material Types')
691+
);
692+
});
693+
694+
test('Get all Material Types works', async () => {
695+
vi.spyOn(prisma.material_Type, 'findMany').mockResolvedValue([]);
696+
697+
const res = await ProjectsService.getAllMaterialTypes(batman);
698+
699+
expect(prisma.material_Type.findMany).toHaveBeenCalledTimes(1);
700+
expect(res).toStrictEqual([]);
701+
});
685702
});
686703
});

0 commit comments

Comments
 (0)