Skip to content

Commit

Permalink
fix: check for last achievement
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasFalkowsky committed Jan 22, 2024
1 parent 4642507 commit 8598903
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
9 changes: 6 additions & 3 deletions common/achievement/create.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -88,6 +88,10 @@ async function createNextUserAchievement<ID extends ActionID>(
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) {
Expand All @@ -99,13 +103,12 @@ async function createNextUserAchievement<ID extends ActionID>(
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 };
5 changes: 1 addition & 4 deletions common/achievement/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,7 @@ async function checkUserAchievement<ID extends ActionID>(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 },
Expand Down

0 comments on commit 8598903

Please sign in to comment.