Skip to content

Commit

Permalink
Merge pull request #68 from PolkadotEducation/feature/user-xp-and-level
Browse files Browse the repository at this point in the history
adding xp to next level field to get user xp response
  • Loading branch information
keijionga authored Dec 23, 2024
2 parents 8d982b5 + a7629be commit 83f1f4b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/controllers/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ const calculateLevel = (exp: number): number => {
return level;
};

const calculateXpToNextLevel = (exp: number, currentLevel: number): number => {
const nextLevelExp = 10 * Math.pow(currentLevel, 2) + 100 * currentLevel + 150;
return nextLevelExp - exp;
};

export const getUserXPAndLevel = async (_req: Request, res: Response) => {
const userId = res.locals?.populatedUser?._id;

Expand Down Expand Up @@ -188,8 +193,9 @@ export const getUserXPAndLevel = async (_req: Request, res: Response) => {
}

const level = calculateLevel(exp);
const xpToNextLevel = calculateXpToNextLevel(exp, level);

return res.status(200).send({ exp, level });
return res.status(200).send({ level, xp: exp, xpToNextLevel });
} catch (e) {
console.error(`[ERROR][getUserXPAndLevel] ${e}`);
return res.status(500).send({ error: { message: "Internal server error" } });
Expand Down
6 changes: 4 additions & 2 deletions tests/progress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,12 @@ describe("Setting API Server up...", () => {
.get(`${API_URL}/progress/level`, { headers })
.then((r) => {
expect(r.status).toEqual(200);
expect(r.data).toHaveProperty("exp");
expect(r.data).toHaveProperty("xp");
expect(r.data).toHaveProperty("level");
expect(r.data.exp).toEqual(125);
expect(r.data).toHaveProperty("xpToNextLevel");
expect(r.data.xp).toEqual(125);
expect(r.data.level).toEqual(0);
expect(r.data.xpToNextLevel).toEqual(25);
})
.catch((e) => {
expect(e).toBeUndefined();
Expand Down

0 comments on commit 83f1f4b

Please sign in to comment.