Skip to content

Commit f7893a4

Browse files
committed
feat: assign coalition points when donating points to the pool
1 parent 4adbba2 commit f7893a4

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ model CodamCoalition {
5353

5454
// Fixed type of score for the coalition system
5555
model CodamCoalitionFixedType {
56-
type String @id // should not contain spaces. e.g. "project", "eval", "logtime", "exam", "basic_event", "intermediate_event", "advanced_event"
56+
type String @id // should not contain spaces. e.g. "project", "eval", "point_donated", "logtime", "exam", "basic_event", "intermediate_event", "advanced_event"
5757
description String
5858
point_amount Int
5959

src/routes/hooks.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Express, Response } from 'express';
33
import { handleLocationCloseWebhook, Location } from './hooks/locations';
44
import { handleProjectsUserUpdateWebhook, ProjectUser } from './hooks/projects_users';
55
import { handleScaleTeamUpdateWebhook, ScaleTeam } from './hooks/scale_teams';
6+
import { handlePointGivenWebhook, PointGiven } from './hooks/pools';
67

78
export interface WebhookHeaders {
89
modelType: string;
@@ -79,6 +80,9 @@ export const setupWebhookRoutes = function(app: Express, prisma: PrismaClient):
7980
case "scale_team": // scale team (evaluation) update
8081
const scaleTeam: ScaleTeam = req.body as ScaleTeam;
8182
return await handleScaleTeamUpdateWebhook(prisma, scaleTeam, res, webhookHeaders.deliveryId);
83+
case "pool": // pool point_given
84+
const pointGiven: PointGiven = req.body as PointGiven;
85+
return await handlePointGivenWebhook(prisma, pointGiven, res, webhookHeaders.deliveryId);
8286
default:
8387
console.warn("Unknown model type", webhookHeaders.modelType);
8488
return await respondWebHookHandledStatus(prisma, webhookHeaders.deliveryId, res, WebhookHandledStatus.Error);

src/routes/hooks/pools.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { Response } from 'express';
2+
import { PrismaClient } from '@prisma/client';
3+
import { WebhookHandledStatus, respondWebHookHandledStatus } from '../hooks';
4+
import { handleFixedPointScore } from '../../handlers/points';
5+
6+
export interface PointGiven {
7+
id: number;
8+
campus_id: number;
9+
cursus_id: number;
10+
given_by: {
11+
id: number;
12+
first_name: string;
13+
last_name: string;
14+
usual_first_name: string | null;
15+
email: string;
16+
login: string;
17+
correction_points: number;
18+
} | null;
19+
points: {
20+
old: number;
21+
current: number;
22+
},
23+
max_points: number;
24+
};
25+
26+
export const handlePointGivenWebhook = async function(prisma: PrismaClient, pointGiven: PointGiven, res: Response | null = null, webhookDeliveryId: string | null = null): Promise<Response | null> {
27+
try {
28+
// Get fixed point type
29+
const fixedPointType = await prisma.codamCoalitionFixedType.findFirst({
30+
where: {
31+
type: 'point_donated',
32+
},
33+
});
34+
if (!fixedPointType || fixedPointType.point_amount === 0) {
35+
console.warn("No fixed point type found for point_donated or point amount is set to 0, skipping...");
36+
return (res ? respondWebHookHandledStatus(prisma, webhookDeliveryId, res, WebhookHandledStatus.Skipped) : null);
37+
}
38+
if (!pointGiven.given_by) {
39+
return (res ? respondWebHookHandledStatus(prisma, webhookDeliveryId, res, WebhookHandledStatus.Skipped) : null);
40+
}
41+
42+
// Calculate the score
43+
const actualPointsGiven = pointGiven.points.current - pointGiven.points.old;
44+
const points = actualPointsGiven * fixedPointType.point_amount;
45+
46+
// Create a score
47+
const score = await handleFixedPointScore(prisma, fixedPointType, pointGiven.id, pointGiven.given_by.id, points,
48+
`Donated ${actualPointsGiven} points to the pool`);
49+
if (!score) {
50+
console.warn("Refused or failed to create score, skipping...");
51+
return (res ? respondWebHookHandledStatus(prisma, webhookDeliveryId, res, WebhookHandledStatus.Skipped) : null);
52+
}
53+
return (res ? respondWebHookHandledStatus(prisma, webhookDeliveryId, res, WebhookHandledStatus.Ok) : null);
54+
}
55+
catch (error) {
56+
console.error("Failed to handle pool point_given webhook", error);
57+
return (res ? respondWebHookHandledStatus(prisma, webhookDeliveryId, res, WebhookHandledStatus.Error) : null);
58+
}
59+
};

src/sync/fixed_point_types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export const initCodamCoalitionFixedTypes = async function(): Promise<void> {
2929
desc: "Each evaluation given will grant the student with this amount of points",
3030
points: 20, // recommended
3131
},
32+
{
33+
type: "point_donated",
34+
desc: "Each point donated to the pool will grant the student with this amount of points",
35+
points: 20, // recommended (to be the same as evaluation)
36+
},
3237
{
3338
type: "logtime",
3439
desc: "Every logtime hour will grant the student with this amount of points",

0 commit comments

Comments
 (0)