From 8598903c51e061cf2fede8763f303c36ddb717d9 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 22 Jan 2024 11:32:36 +0100 Subject: [PATCH] fix: check for last achievement --- common/achievement/create.ts | 9 ++++++--- common/achievement/index.ts | 5 +---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/common/achievement/create.ts b/common/achievement/create.ts index 5fed0964b..71c1b7485 100644 --- a/common/achievement/create.ts +++ b/common/achievement/create.ts @@ -1,4 +1,4 @@ -import { Prisma } from '@prisma/client'; +import { Prisma, achievement_type_enum } from '@prisma/client'; import { Achievement_template, achievement_template_for_enum } from '../../graphql/generated'; import { ActionID, SpecificNotificationContext } from '../notification/actions'; import { prisma } from '../prisma'; @@ -88,6 +88,10 @@ async function createNextUserAchievement( return; } const nextStepTemplate = templatesForGroup[nextStepIndex]; + const achievedAt = + templatesForGroup.length === nextStepIndex && templatesForGroup[nextStepIndex].type === achievement_type_enum.SEQUENTIAL + ? JSON.stringify(new Date()) + : null; // Here a user template is created for the next template in the group. This is done to always have the data availible for the next step. // This could mean to, for example, have the name of a match partner that is not yet availible due to a unfinished matching process. if (nextStepTemplate) { @@ -99,13 +103,12 @@ async function createNextUserAchievement( context: context ? context : Prisma.JsonNull, template: { connect: { id: nextStepTemplate.id } }, recordValue: nextStepTemplate.type === 'STREAK' ? 0 : null, + achievedAt: achievedAt, }, select: { id: true, userId: true, context: true, template: true, achievedAt: true, recordValue: true }, }); return createdUserAchievement; } - const nextUserAchievement = await createNextUserAchievement(templatesForGroup, nextStepIndex + 1, userId, context); - return nextUserAchievement; } export { getOrCreateUserAchievement, createAchievement }; diff --git a/common/achievement/index.ts b/common/achievement/index.ts index 1fc41aa8b..14420a0a3 100644 --- a/common/achievement/index.ts +++ b/common/achievement/index.ts @@ -129,10 +129,7 @@ async function checkUserAchievement(userAchievement: UserAc const evaluationResultValue = typeof evaluationResult.resultObject[dataAggregationKey] === 'number' ? Number(evaluationResult.resultObject[dataAggregationKey]) : null; const awardedAchievement = await rewardUser(evaluationResultValue, userAchievement, event); - const newAchievement = await createAchievement(awardedAchievement.template, userAchievement.userId, event.context); - if (newAchievement) { - await checkUserAchievement(newAchievement, event); - } + await createAchievement(awardedAchievement.template, userAchievement.userId, event.context); } else { await prisma.user_achievement.update({ where: { id: userAchievement.id },