Skip to content

Commit 6e39841

Browse files
committed
fixed refundsource issue
1 parent 779c86f commit 6e39841

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

src/backend/src/utils/reimbursement-requests.utils.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,21 @@ export const updateReimbursementProducts = async (
125125
}
126126

127127
//if a product has an id that means it existed before and was updated
128-
const updatedOtherExistingProducts = updatedOtherReimbursementProducts.filter((product) => product.id);
129-
130-
const updatedWbsExistingProducts = updatedWbsReimbursementProducts.filter((product) => product.id);
128+
const updatedOtherExistingProducts = updatedOtherReimbursementProducts.filter(
129+
(product): product is OtherReimbursementProductCreateArgs & { id: string } => !!product.id
130+
);
131131

132-
const updatedExistingProducts = (updatedOtherExistingProducts as ReimbursementProductCreateArgs[]).concat(
133-
updatedWbsExistingProducts as ReimbursementProductCreateArgs[]
132+
const updatedWbsExistingProducts = updatedWbsReimbursementProducts.filter(
133+
(product): product is WbsReimbursementProductCreateArgs & { id: string } => !!product.id
134134
);
135135

136+
const updatedExistingProducts = (
137+
updatedOtherExistingProducts as (ReimbursementProductCreateArgs & { id: string })[]
138+
).concat(updatedWbsExistingProducts as (ReimbursementProductCreateArgs & { id: string })[]);
139+
136140
validateUpdatedProductsExistInDatabase(currentReimbursementProducts, updatedExistingProducts);
137141

138-
const updatedExistingProductIds = updatedExistingProducts.map((product) => product.id!);
142+
const updatedExistingProductIds = updatedExistingProducts.map((product) => product.id);
139143

140144
//if the product does not have an id that means it is new
141145
const newOtherProducts = updatedOtherReimbursementProducts.filter((product) => !product.id);
@@ -159,26 +163,23 @@ export const updateReimbursementProducts = async (
159163
*
160164
* @param products the products to update
161165
*/
162-
const updateExistingProducts = async (products: ReimbursementProductCreateArgs[]) => {
166+
const updateExistingProducts = async (products: (ReimbursementProductCreateArgs & { id: string })[]) => {
163167
//updates the cost, name, and refund sources of the remaining products, which should be products that existed before that were not deleted
164168
// Does not update wbs element id because we are requiring the user on the frontend to delete it from the wbs number and then adding it to another one
165169
for (const product of products) {
166-
// Delete old refund sources for this product
167-
await prisma.refund_Source.deleteMany({
168-
where: { reimbursementProductId: product.id }
169-
});
170-
171170
const refundSources = product.refundSources.map((rs) => ({
172171
indexCode: { connect: { indexCodeId: rs.indexCode.indexCodeId } },
173172
amount: rs.amount
174173
}));
175174

175+
// Delete old refund sources and update product atomically
176176
await prisma.reimbursement_Product.update({
177177
where: { reimbursementProductId: product.id },
178178
data: {
179179
name: product.name,
180180
cost: product.cost,
181181
refundSources: {
182+
deleteMany: {},
182183
create: refundSources
183184
}
184185
}

0 commit comments

Comments
 (0)