@@ -887,9 +887,10 @@ export default class ProjectsService {
887887 * @throws if the submitter does not have the relevant positions
888888 * @returns the updated material
889889 */
890- static async assignMaterialAssembly ( submitter : User , materialId : string , assemblyId : undefined | string ) {
890+ static async assignMaterialAssembly ( submitter : User , materialId : string , assemblyId ?: string ) {
891891 const material = await prisma . material . findUnique ( { where : { materialId } } ) ;
892892 if ( ! material ) throw new NotFoundException ( 'Material' , materialId ) ;
893+
893894 const project = await prisma . project . findFirst ( {
894895 where : {
895896 wbsElementId : material . wbsElementId
@@ -902,26 +903,22 @@ export default class ProjectsService {
902903
903904 // Permission: leadership and up, anyone on project team
904905 if ( ! ( isLeadership ( submitter . role ) || isUserPartOfTeams ( project . teams , submitter ) ) )
905- throw new AccessDeniedException ( 'Only leadership or above can create a material type' ) ;
906-
907- if ( assemblyId === undefined ) {
908- // Unassign material from current assembly if assembly id is undefined
909- const updatedMaterial = await prisma . material . update ( { where : { materialId } , data : { assemblyId : undefined } } ) ;
910- return updatedMaterial ;
911- }
906+ throw new AccessDeniedException ( 'Only leadership or above can assign materials to assemblies' ) ;
912907
913- const assembly = await prisma . assembly . findUnique ( {
914- where : { assemblyId } ,
915- include : { wbsElement : true }
916- } ) ;
917- if ( ! assembly ) throw new NotFoundException ( 'Assembly' , assemblyId ) ;
908+ if ( assemblyId ) {
909+ const assembly = await prisma . assembly . findUnique ( {
910+ where : { assemblyId } ,
911+ include : { wbsElement : true }
912+ } ) ;
913+ if ( ! assembly ) throw new NotFoundException ( 'Assembly' , assemblyId ) ;
918914
919- // Confirm that the assembly's wbsElement is the same as the material's wbsElement
920- if ( material . wbsElementId !== assembly . wbsElementId )
921- throw new HttpException (
922- 400 ,
923- `The WBS element of the material (${ material . wbsElementId } ) and assembly (${ assembly . wbsElementId } ) do not match`
924- ) ;
915+ // Confirm that the assembly's wbsElement is the same as the material's wbsElement
916+ if ( material . wbsElementId !== assembly . wbsElementId )
917+ throw new HttpException (
918+ 400 ,
919+ `The WBS element of the material (${ material . wbsElementId } ) and assembly (${ assembly . wbsElementId } ) do not match`
920+ ) ;
921+ }
925922
926923 // Assign a material on a project to a different assembly
927924 const updatedMaterial = await prisma . material . update ( { where : { materialId } , data : { assemblyId } } ) ;
0 commit comments