1- import { User , Assembly } from '@prisma/client' ;
2- import { isAdmin , isGuest , isProject , LinkCreateArgs , LinkType , Project , WbsNumber , wbsPipe } from 'shared' ;
1+ import { Material_Type , User , Assembly } from '@prisma/client' ;
2+ import { isAdmin , isGuest , isLeadership , isProject , LinkCreateArgs , LinkType , Project , WbsNumber , wbsPipe } from 'shared' ;
33import projectQueryArgs from '../prisma-query-args/projects.query-args' ;
44import prisma from '../prisma/prisma' ;
55import projectTransformer from '../transformers/projects.transformer' ;
@@ -10,7 +10,8 @@ import {
1010 AccessDeniedException ,
1111 HttpException ,
1212 NotFoundException ,
13- DeletedException
13+ DeletedException ,
14+ AccessDeniedException
1415} from '../utils/errors.utils' ;
1516import {
1617 addDescriptionBullets ,
@@ -656,4 +657,57 @@ export default class ProjectsService {
656657
657658 return assembly ;
658659 }
660+
661+ * Creates a new Manufacturer
662+ * @param submitter the user who 's creating the manufacturer
663+ * @param name the name of the manufacturer
664+ * @returns the newly created manufacturer
665+ * @throws if the submitter is a guest or the given manufacturer name already exists
666+ * /
667+ static async createManufacturer ( submitter : User , name : string ) {
668+ if ( isGuest ( submitter . role ) ) throw new AccessDeniedGuestException ( 'create manufacturers' ) ;
669+
670+ const manufacturer = await prisma . manufacturer . findUnique ( {
671+ where : {
672+ name
673+ }
674+ } ) ;
675+
676+ if ( manufacturer ) throw new HttpException ( 400 , `${ name } already exists as a manufacturer!` ) ;
677+
678+ const newManufacturer = await prisma . manufacturer . create ( {
679+ data : { name, dateCreated : new Date ( ) , creatorId : submitter . userId }
680+ } ) ;
681+
682+ return newManufacturer ;
683+ }
684+
685+ /**
686+ * Create a new material type
687+ * @param name the name of the new material type
688+ * @param submitter the user who is creating the material type
689+ * @throws if the submitter is not a leader or the material type with the given name already exists
690+ */
691+ static async createMaterialType ( name : string , submitter : User ) : Promise < Material_Type > {
692+ if ( ! isLeadership ( submitter . role ) )
693+ throw new AccessDeniedException ( 'Only leadership or above can create a material type' ) ;
694+
695+ const materialType = await prisma . material_Type . findUnique ( {
696+ where : {
697+ name
698+ }
699+ } ) ;
700+
701+ if ( ! ! materialType ) throw new HttpException ( 400 , `The following material type already exists: ${ name } ` ) ;
702+
703+ const newMaterialType = await prisma . material_Type . create ( {
704+ data : {
705+ name,
706+ dateCreated : new Date ( ) ,
707+ creatorId : submitter . userId
708+ }
709+ } ) ;
710+
711+ return newMaterialType ;
712+ }
659713}
0 commit comments