@@ -627,14 +627,14 @@ export default class ProjectsService {
627627 * @param manufacturerName the name of the material's manufacturer
628628 * @param manufacturerPartNumber the manufacturer part number for the material
629629 * @param quantity the quantity of material as a number
630- * @param unitName the name of the Quantity Unit the quantity is measured in
631630 * @param price the price of the material in whole cents
632631 * @param subtotal the subtotal of the price for the material in whole cents
633632 * @param linkUrl the url for the material's link as a string
634633 * @param notes any notes about the material as a string
635634 * @param wbsNumber the WBS number of the project associated with this material
636635 * @param assemblyId the id of the Assembly for the material
637636 * @param pdmFileName the name of the pdm file for the material
637+ * @param unitName the name of the Quantity Unit the quantity is measured in
638638 * @returns the created material
639639 */
640640 static async createMaterial (
@@ -645,14 +645,14 @@ export default class ProjectsService {
645645 manufacturerName : string ,
646646 manufacturerPartNumber : string ,
647647 quantity : number ,
648- unitName : string ,
649648 price : number ,
650649 subtotal : number ,
651650 linkUrl : string ,
652651 notes : string ,
653652 wbsNumber : WbsNumber ,
654653 assemblyId ?: string ,
655- pdmFileName ?: string
654+ pdmFileName ?: string ,
655+ unitName ?: string
656656 ) : Promise < Material > {
657657 const project = await prisma . project . findFirst ( {
658658 where : {
@@ -682,10 +682,12 @@ export default class ProjectsService {
682682 } ) ;
683683 if ( ! manufacturer ) throw new NotFoundException ( 'Manufacturer' , manufacturerName ) ;
684684
685- const unit = await prisma . unit . findFirst ( {
686- where : { name : unitName }
687- } ) ;
688- if ( ! unit ) throw new NotFoundException ( 'Unit' , unitName ) ;
685+ if ( unitName ) {
686+ const unit = await prisma . unit . findFirst ( {
687+ where : { name : unitName }
688+ } ) ;
689+ if ( ! unit ) throw new NotFoundException ( 'Unit' , unitName ) ;
690+ }
689691
690692 const perms = isLeadership ( creator . role ) || isUserPartOfTeams ( project . teams , creator ) ;
691693
@@ -872,4 +874,109 @@ export default class ProjectsService {
872874
873875 return deletedMaterialType ;
874876 }
877+
878+ /**
879+ * Update a material
880+ * @param submitter the submitter of the request
881+ * @param materialId the material id of the material being edited
882+ * @param name the name of the edited material
883+ * @param status the status of the edited material
884+ * @param materialTypeName the material type of the edited material
885+ * @param manufacturerName the manufacturerName of the edited material
886+ * @param manufacturerPartNumber the manufacturerPartNumber of the edited material
887+ * @param quantity the quantity of the edited material
888+ * @param price the price of the edited material
889+ * @param subtotal the subtotal of the edited material
890+ * @param linkUrl the linkUrl of the edited material
891+ * @param notes the notes of the edited material
892+ * @param unitName the unit name of the edited material
893+ * @param assemblyId the assembly id of the edited material
894+ * @param pdmFileName the pdm file name of the edited material
895+ * @throws if permission denied or material's wbsElement is undefined/deleted
896+ * @returns the updated material
897+ */
898+ static async editMaterial (
899+ submitter : User ,
900+ materialId : string ,
901+ name : string ,
902+ status : Material_Status ,
903+ materialTypeName : string ,
904+ manufacturerName : string ,
905+ manufacturerPartNumber : string ,
906+ quantity : number ,
907+ price : number ,
908+ subtotal : number ,
909+ linkUrl : string ,
910+ notes : string ,
911+ unitName ?: string ,
912+ assemblyId ?: string ,
913+ pdmFileName ?: string
914+ ) : Promise < Material > {
915+ const material = await prisma . material . findUnique ( {
916+ where : {
917+ materialId
918+ }
919+ } ) ;
920+
921+ if ( ! material ) throw new NotFoundException ( 'Material' , materialId ) ;
922+ if ( material . dateDeleted ) throw new DeletedException ( 'Material' , materialId ) ;
923+
924+ const project = await prisma . project . findFirst ( {
925+ where : {
926+ wbsElementId : material . wbsElementId
927+ } ,
928+ ...projectQueryArgs
929+ } ) ;
930+
931+ if ( ! project ) throw new NotFoundException ( 'Project' , material . wbsElementId ) ;
932+ if ( project . wbsElement . dateDeleted ) throw new DeletedException ( 'Project' , project . projectId ) ;
933+
934+ if ( assemblyId ) {
935+ const assembly = await prisma . assembly . findFirst ( { where : { assemblyId } } ) ;
936+ if ( ! assembly ) throw new NotFoundException ( 'Assembly' , assemblyId ) ;
937+ }
938+
939+ const materialType = await prisma . material_Type . findFirst ( {
940+ where : { name : materialTypeName }
941+ } ) ;
942+ if ( ! materialType ) throw new NotFoundException ( 'Material Type' , materialTypeName ) ;
943+
944+ const manufacturer = await prisma . manufacturer . findFirst ( {
945+ where : { name : manufacturerName }
946+ } ) ;
947+ if ( ! manufacturer ) throw new NotFoundException ( 'Manufacturer' , manufacturerName ) ;
948+
949+ if ( unitName ) {
950+ const unit = await prisma . unit . findFirst ( {
951+ where : { name : unitName }
952+ } ) ;
953+ if ( ! unit ) throw new NotFoundException ( 'Unit' , unitName ) ;
954+ }
955+
956+ const perms = isLeadership ( submitter . role ) || isUserPartOfTeams ( project . teams , submitter ) ;
957+
958+ if ( ! perms ) throw new AccessDeniedException ( 'update material' ) ;
959+
960+ const updatedMaterial = await prisma . material . update ( {
961+ where : { materialId } ,
962+ data : {
963+ name,
964+ status,
965+ materialTypeName,
966+ manufacturerName,
967+ manufacturerPartNumber,
968+ quantity,
969+ unitName,
970+ price,
971+ subtotal,
972+ linkUrl,
973+ notes,
974+ wbsElementId : project . wbsElementId ,
975+ assemblyId,
976+ pdmFileName
977+ }
978+ } ) ;
979+
980+ return updatedMaterial ;
981+ }
875982}
0 commit comments