@@ -616,14 +616,14 @@ export default class ProjectsService {
616616 * @param manufacturerName the name of the material's manufacturer
617617 * @param manufacturerPartNumber the manufacturer part number for the material
618618 * @param quantity the quantity of material as a number
619- * @param unitName the name of the Quantity Unit the quantity is measured in
620619 * @param price the price of the material in whole cents
621620 * @param subtotal the subtotal of the price for the material in whole cents
622621 * @param linkUrl the url for the material's link as a string
623622 * @param notes any notes about the material as a string
624623 * @param wbsNumber the WBS number of the project associated with this material
625624 * @param assemblyId the id of the Assembly for the material
626625 * @param pdmFileName the name of the pdm file for the material
626+ * @param unitName the name of the Quantity Unit the quantity is measured in
627627 * @returns the created material
628628 */
629629 static async createMaterial (
@@ -634,14 +634,14 @@ export default class ProjectsService {
634634 manufacturerName : string ,
635635 manufacturerPartNumber : string ,
636636 quantity : number ,
637- unitName : string ,
638637 price : number ,
639638 subtotal : number ,
640639 linkUrl : string ,
641640 notes : string ,
642641 wbsNumber : WbsNumber ,
643642 assemblyId ?: string ,
644- pdmFileName ?: string
643+ pdmFileName ?: string ,
644+ unitName ?: string
645645 ) : Promise < Material > {
646646 const project = await prisma . project . findFirst ( {
647647 where : {
@@ -671,10 +671,12 @@ export default class ProjectsService {
671671 } ) ;
672672 if ( ! manufacturer ) throw new NotFoundException ( 'Manufacturer' , manufacturerName ) ;
673673
674- const unit = await prisma . unit . findFirst ( {
675- where : { name : unitName }
676- } ) ;
677- if ( ! unit ) throw new NotFoundException ( 'Unit' , unitName ) ;
674+ if ( unitName ) {
675+ const unit = await prisma . unit . findFirst ( {
676+ where : { name : unitName }
677+ } ) ;
678+ if ( ! unit ) throw new NotFoundException ( 'Unit' , unitName ) ;
679+ }
678680
679681 const perms = isLeadership ( creator . role ) || isUserPartOfTeams ( project . teams , creator ) ;
680682
@@ -829,4 +831,109 @@ export default class ProjectsService {
829831
830832 return newMaterialType ;
831833 }
834+
835+ /**
836+ * Update a material
837+ * @param submitter the submitter of the request
838+ * @param materialId the material id of the material being edited
839+ * @param name the name of the edited material
840+ * @param status the status of the edited material
841+ * @param materialTypeName the material type of the edited material
842+ * @param manufacturerName the manufacturerName of the edited material
843+ * @param manufacturerPartNumber the manufacturerPartNumber of the edited material
844+ * @param quantity the quantity of the edited material
845+ * @param price the price of the edited material
846+ * @param subtotal the subtotal of the edited material
847+ * @param linkUrl the linkUrl of the edited material
848+ * @param notes the notes of the edited material
849+ * @param unitName the unit name of the edited material
850+ * @param assemblyId the assembly id of the edited material
851+ * @param pdmFileName the pdm file name of the edited material
852+ * @throws if permission denied or material's wbsElement is undefined/deleted
853+ * @returns the updated material
854+ */
855+ static async editMaterial (
856+ submitter : User ,
857+ materialId : string ,
858+ name : string ,
859+ status : Material_Status ,
860+ materialTypeName : string ,
861+ manufacturerName : string ,
862+ manufacturerPartNumber : string ,
863+ quantity : number ,
864+ price : number ,
865+ subtotal : number ,
866+ linkUrl : string ,
867+ notes : string ,
868+ unitName ?: string ,
869+ assemblyId ?: string ,
870+ pdmFileName ?: string
871+ ) : Promise < Material > {
872+ const material = await prisma . material . findUnique ( {
873+ where : {
874+ materialId
875+ }
876+ } ) ;
877+
878+ if ( ! material ) throw new NotFoundException ( 'Material' , materialId ) ;
879+ if ( material . dateDeleted ) throw new DeletedException ( 'Material' , materialId ) ;
880+
881+ const project = await prisma . project . findFirst ( {
882+ where : {
883+ wbsElementId : material . wbsElementId
884+ } ,
885+ ...projectQueryArgs
886+ } ) ;
887+
888+ if ( ! project ) throw new NotFoundException ( 'Project' , material . wbsElementId ) ;
889+ if ( project . wbsElement . dateDeleted ) throw new DeletedException ( 'Project' , project . projectId ) ;
890+
891+ if ( assemblyId ) {
892+ const assembly = await prisma . assembly . findFirst ( { where : { assemblyId } } ) ;
893+ if ( ! assembly ) throw new NotFoundException ( 'Assembly' , assemblyId ) ;
894+ }
895+
896+ const materialType = await prisma . material_Type . findFirst ( {
897+ where : { name : materialTypeName }
898+ } ) ;
899+ if ( ! materialType ) throw new NotFoundException ( 'Material Type' , materialTypeName ) ;
900+
901+ const manufacturer = await prisma . manufacturer . findFirst ( {
902+ where : { name : manufacturerName }
903+ } ) ;
904+ if ( ! manufacturer ) throw new NotFoundException ( 'Manufacturer' , manufacturerName ) ;
905+
906+ if ( unitName ) {
907+ const unit = await prisma . unit . findFirst ( {
908+ where : { name : unitName }
909+ } ) ;
910+ if ( ! unit ) throw new NotFoundException ( 'Unit' , unitName ) ;
911+ }
912+
913+ const perms = isLeadership ( submitter . role ) || isUserPartOfTeams ( project . teams , submitter ) ;
914+
915+ if ( ! perms ) throw new AccessDeniedException ( 'update material' ) ;
916+
917+ const updatedMaterial = await prisma . material . update ( {
918+ where : { materialId } ,
919+ data : {
920+ name,
921+ status,
922+ materialTypeName,
923+ manufacturerName,
924+ manufacturerPartNumber,
925+ quantity,
926+ unitName,
927+ price,
928+ subtotal,
929+ linkUrl,
930+ notes,
931+ wbsElementId : project . wbsElementId ,
932+ assemblyId,
933+ pdmFileName
934+ }
935+ } ) ;
936+
937+ return updatedMaterial ;
938+ }
832939}
0 commit comments