@@ -104,10 +104,6 @@ export const validateReimbursementProducts = async (
104104 if ( material . wbsElementId !== wbsElement . wbsElementId ) {
105105 throw new HttpException ( 400 , `Material does not belong to project ${ wbsPipe ( wbsNum ) } ` ) ;
106106 }
107-
108- if ( ! product . id && material . reimbursementRequestId ) {
109- throw new HttpException ( 400 , `Material is already linked to another reimbursement request` ) ;
110- }
111107 }
112108
113109 return {
@@ -185,22 +181,27 @@ export const updateReimbursementProducts = async (
185181} ;
186182
187183/**
188- * Unlinks materials from products that are being deleted
184+ * Unlinks materials from products that are being deleted, reverting status if no other active products remain
189185 * @param products the products being deleted
190186 */
191187const unlinkMaterialsFromDeletedProducts = async ( products : Reimbursement_Product [ ] ) => {
188+ const deletedProductIds = products . map ( ( p ) => p . reimbursementProductId ) ;
192189 const materialIds = products . filter ( ( p ) => p . materialId ) . map ( ( p ) => p . materialId ! ) ;
193190
194- if ( materialIds . length > 0 ) {
195- await prisma . material . updateMany ( {
191+ for ( const materialId of materialIds ) {
192+ const otherProductCount = await prisma . reimbursement_Product . count ( {
196193 where : {
197- materialId : { in : materialIds }
198- } ,
199- data : {
200- reimbursementRequestId : null ,
201- status : 'NOT_READY_TO_ORDER'
194+ materialId,
195+ dateDeleted : null ,
196+ reimbursementProductId : { notIn : deletedProductIds }
202197 }
203198 } ) ;
199+ if ( otherProductCount === 0 ) {
200+ await prisma . material . update ( {
201+ where : { materialId } ,
202+ data : { status : 'NOT_READY_TO_ORDER' }
203+ } ) ;
204+ }
204205 }
205206} ;
206207
@@ -237,15 +238,24 @@ const updateExistingProducts = async (
237238 } ) ;
238239
239240 if ( currentProduct ?. materialId && currentProduct . materialId !== product . materialId ) {
240- await prisma . material . update ( {
241- where : { materialId : currentProduct . materialId } ,
242- data : { reimbursementRequestId : null , status : 'NOT_READY_TO_ORDER' }
241+ const otherProductCount = await prisma . reimbursement_Product . count ( {
242+ where : {
243+ materialId : currentProduct . materialId ,
244+ dateDeleted : null ,
245+ reimbursementProductId : { not : product . id }
246+ }
243247 } ) ;
248+ if ( otherProductCount === 0 ) {
249+ await prisma . material . update ( {
250+ where : { materialId : currentProduct . materialId } ,
251+ data : { status : 'NOT_READY_TO_ORDER' }
252+ } ) ;
253+ }
244254 }
245255
246256 await prisma . material . update ( {
247257 where : { materialId : product . materialId } ,
248- data : { status : 'READY_TO_ORDER' , reimbursementRequestId : currentProduct ?. reimbursementRequestId }
258+ data : { status : 'READY_TO_ORDER' }
249259 } ) ;
250260 } else {
251261 await prisma . reimbursement_Product . update ( {
@@ -262,10 +272,19 @@ const updateExistingProducts = async (
262272 } ) ;
263273
264274 if ( currentProduct ?. materialId ) {
265- await prisma . material . update ( {
266- where : { materialId : currentProduct . materialId } ,
267- data : { reimbursementRequestId : null , status : 'NOT_READY_TO_ORDER' }
275+ const otherProductCount = await prisma . reimbursement_Product . count ( {
276+ where : {
277+ materialId : currentProduct . materialId ,
278+ dateDeleted : null ,
279+ reimbursementProductId : { not : product . id }
280+ }
268281 } ) ;
282+ if ( otherProductCount === 0 ) {
283+ await prisma . material . update ( {
284+ where : { materialId : currentProduct . materialId } ,
285+ data : { status : 'NOT_READY_TO_ORDER' }
286+ } ) ;
287+ }
269288 }
270289 }
271290 }
@@ -400,10 +419,7 @@ export const createReimbursementProducts = async (
400419 if ( product . materialId ) {
401420 await prisma . material . update ( {
402421 where : { materialId : product . materialId } ,
403- data : {
404- status : 'READY_TO_ORDER' ,
405- reimbursementRequestId
406- }
422+ data : { status : 'READY_TO_ORDER' }
407423 } ) ;
408424 }
409425 return reimbursementProduct ;
@@ -606,13 +622,16 @@ export const validateRefund = async (user: User, refundAmount: number, organizat
606622 * @param reimbursementRequestId the id of the reimbursement request
607623 */
608624export const updateMaterialStatusesOnPayment = async ( reimbursementRequestId : string ) : Promise < void > => {
609- await prisma . material . updateMany ( {
610- where : {
611- reimbursementRequestId,
612- dateDeleted : null
613- } ,
614- data : {
615- status : 'ORDERED'
616- }
625+ const products = await prisma . reimbursement_Product . findMany ( {
626+ where : { reimbursementRequestId, dateDeleted : null , materialId : { not : null } }
617627 } ) ;
628+
629+ const materialIds = products . map ( ( p ) => p . materialId ! ) ;
630+
631+ if ( materialIds . length > 0 ) {
632+ await prisma . material . updateMany ( {
633+ where : { materialId : { in : materialIds } , dateDeleted : null , status : 'READY_TO_ORDER' } ,
634+ data : { status : 'ORDERED' }
635+ } ) ;
636+ }
618637} ;
0 commit comments