Skip to content

Commit 9bde0ba

Browse files
committed
fix: implement recalculating point_donated scores
1 parent f33b581 commit 9bde0ba

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

src/routes/admin/points.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,34 @@ export const setupAdminPointsRoutes = function(app: Express, prisma: PrismaClien
327327
return res.status(404).json({ error: 'Score not found' });
328328
}
329329

330+
if (score.fixed_type_id === 'point_donated') {
331+
// For points donated, there is no Intra object to retrigger the webhook with, so we have to recalculate the score here manually
332+
const newFixedPointType = await prisma.codamCoalitionFixedType.findFirst({
333+
where: {
334+
type: 'point_donated',
335+
}
336+
});
337+
if (!newFixedPointType) {
338+
return res.status(501).json({ error: 'No point_donated fixed type found, cannot recalculate' });
339+
}
340+
341+
// Parse amount of points donated from the reason... no better way ATM
342+
const pointsDonated = parseInt(score.reason.split(' ')[1]);
343+
if (isNaN(pointsDonated)) {
344+
return res.status(400).json({ error: 'Invalid amount of points donated in the reason field' });
345+
}
346+
const newAmount = pointsDonated * newFixedPointType.point_amount;
347+
await prisma.codamCoalitionScore.update({
348+
where: {
349+
id: score.id,
350+
},
351+
data: {
352+
amount: newAmount,
353+
},
354+
});
355+
return res.status(200).json({ status: 'ok', new_amount: newAmount });
356+
}
357+
330358
if (score.fixed_type_id === null || score.type_intra_id === null) {
331359
return res.status(400).json({ error: 'Score is not based on a fixed type, cannot recalculate' });
332360
}

0 commit comments

Comments
 (0)