Skip to content

Commit c6e3d33

Browse files
committed
be able to create multiple ghost achievement rows
1 parent d52992e commit c6e3d33

File tree

1 file changed

+46
-28
lines changed

1 file changed

+46
-28
lines changed

common/achievement/derive.ts

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@ import {
77
pupil_screening_status_enum,
88
} from '@prisma/client';
99
import { User, getPupil, getStudent } from '../user';
10-
// TODO: Fix import when other PR is in
1110
import { prisma } from '../prisma';
1211
import { achievement_with_template } from './types';
1312
import { getAchievementTemplates, TemplateSelectEnum } from './template';
13+
import { createRelation, EventRelationType } from './relation';
14+
15+
const PupilNewMatchGroup = 'pupil_new_match';
16+
const PupilNewMatchGroupOrder = 3;
1417

1518
const GhostAchievements: { [key: string]: achievement_template } = {
1619
pupil_new_match_1: {
1720
id: -1,
1821
templateFor: AchievementTemplateFor.Match,
19-
group: 'pupil_new_match',
22+
group: PupilNewMatchGroup,
2023
groupOrder: 1,
2124
type: AchievementType.SEQUENTIAL,
2225
image: 'gamification/achievements/release/finish_onboarding/two_pieces/step_1.png',
@@ -40,7 +43,7 @@ const GhostAchievements: { [key: string]: achievement_template } = {
4043
pupil_new_match_2: {
4144
id: -1,
4245
templateFor: AchievementTemplateFor.Match,
43-
group: 'pupil_new_match',
46+
group: PupilNewMatchGroup,
4447
groupOrder: 2,
4548
type: AchievementType.SEQUENTIAL,
4649
image: 'gamification/achievements/release/finish_onboarding/two_pieces/step_2.png',
@@ -82,8 +85,7 @@ export async function deriveAchievements(user: User, realAchievements: achieveme
8285
}
8386

8487
if (user.studentId) {
85-
const student = await getStudent(user);
86-
88+
// const student = await getStudent(user);
8789
// await deriveStudentOnboarding(student, result);
8890
// await deriveStudentMatching(student, result);
8991
// ...
@@ -96,23 +98,18 @@ export function deriveAchievementTemplates(group: string): achievement_template[
9698
return Object.values(GhostAchievements).filter((row) => row.group === group);
9799
}
98100

99-
async function derivePupilMatching(user: User, pupil: Pupil, result: achievement_with_template[], userAchievements: achievement_with_template[]) {
100-
const hasRequest = pupil.openMatchRequestCount > 0;
101-
// const hasOpenScreening = (await prisma.pupil_screening.count({ where: { pupilId: pupil.id, status: 'pending', invalidated: false } })) > 0;
102-
const hasSuccessfulScreening = await prisma.pupil_screening.count({
103-
where: { pupilId: pupil.id, status: pupil_screening_status_enum.success, invalidated: false },
104-
});
105-
106-
const userAchievement = userAchievements.find((row) => row.template.group === 'pupil_new_match');
107-
const matches = await prisma.match.findMany({ where: { pupilId: pupil.id } });
108-
109-
// if (!userAchievement && matches.length > 0) {
110-
// return [];
111-
// }
112-
113-
if (!userAchievement) {
101+
async function generatePupilMatching(
102+
achievement: achievement_with_template | null,
103+
user: User,
104+
hasRequest: boolean,
105+
hasSuccessfulScreening: boolean
106+
): Promise<achievement_with_template[]> {
107+
const result: achievement_with_template[] = [];
108+
// Generating a ramdom relation to be able to show multiple sequences of this kind in parallel
109+
const randomRelation = createRelation(EventRelationType.Match, Math.random()) + '-tmp';
110+
if (!achievement) {
114111
const groups = await getAchievementTemplates(TemplateSelectEnum.BY_GROUP);
115-
if (!groups.has('pupil_new_match') || groups.get('pupil_new_match').length === 0) {
112+
if (!groups.has(PupilNewMatchGroup) || groups.get(PupilNewMatchGroup).length === 0) {
116113
throw new Error('group template not found!');
117114
}
118115
// If there is no real achievement yet, we have to fake the first one in the row as well
@@ -121,11 +118,11 @@ async function derivePupilMatching(user: User, pupil: Pupil, result: achievement
121118
templateId: -1,
122119
userId: user.userID,
123120
isSeen: true,
124-
template: groups.get('pupil_new_match')[0],
121+
template: groups.get(PupilNewMatchGroup)[0],
125122
context: {},
126123
recordValue: null,
127124
achievedAt: null,
128-
relation: null,
125+
relation: randomRelation,
129126
});
130127
}
131128

@@ -137,9 +134,8 @@ async function derivePupilMatching(user: User, pupil: Pupil, result: achievement
137134
template: GhostAchievements.pupil_new_match_1,
138135
context: {},
139136
recordValue: null,
140-
achievedAt: hasRequest || userAchievement ? new Date() : null,
141-
// achievedAt: new Date(),
142-
relation: userAchievement?.relation ?? null,
137+
achievedAt: hasRequest || achievement ? new Date() : null,
138+
relation: achievement?.relation ?? randomRelation,
143139
});
144140

145141
result.push({
@@ -150,7 +146,29 @@ async function derivePupilMatching(user: User, pupil: Pupil, result: achievement
150146
template: GhostAchievements.pupil_new_match_2,
151147
context: {},
152148
recordValue: null,
153-
achievedAt: hasSuccessfulScreening || userAchievement ? new Date() : null,
154-
relation: userAchievement?.relation ?? null,
149+
achievedAt: hasSuccessfulScreening || achievement ? new Date() : null,
150+
relation: achievement?.relation ?? randomRelation,
155151
});
152+
return result;
153+
}
154+
155+
async function derivePupilMatching(user: User, pupil: Pupil, result: achievement_with_template[], userAchievements: achievement_with_template[]) {
156+
const hasRequest = pupil.openMatchRequestCount > 0;
157+
// const hasOpenScreening = (await prisma.pupil_screening.count({ where: { pupilId: pupil.id, status: 'pending', invalidated: false } })) > 0;
158+
const hasSuccessfulScreening = await prisma.pupil_screening.count({
159+
where: { pupilId: pupil.id, status: pupil_screening_status_enum.success, invalidated: false },
160+
});
161+
162+
const newMatchAchievements = userAchievements.filter(
163+
(row) => row.template.group === PupilNewMatchGroup && row.template.groupOrder === PupilNewMatchGroupOrder
164+
);
165+
for (let i = 0; i < pupil.openMatchRequestCount; i++) {
166+
const ghosts = await generatePupilMatching(null, user, hasRequest, hasSuccessfulScreening > 0);
167+
result.push(...ghosts);
168+
}
169+
170+
for (const userAchievement of newMatchAchievements) {
171+
const ghosts = await generatePupilMatching(userAchievement, user, hasRequest, hasSuccessfulScreening > 0);
172+
result.push(...ghosts);
173+
}
156174
}

0 commit comments

Comments
 (0)