-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): update profile when complete todo/daily/habit
- Loading branch information
1 parent
e89a0d0
commit 60c1290
Showing
4 changed files
with
102 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const Profile = require('../models/profile') | ||
|
||
// update profile based on the completed task | ||
async function updateProfileAfterTaskCompletion(userId, taskType) { | ||
// Fetch the user's profile | ||
const profile = await Profile.findOne({ userId }); | ||
if (!profile) { | ||
throw new Error('Profile not found'); | ||
} | ||
|
||
// Define the task rewards | ||
const taskRewards = { | ||
'goodHabit': { experience: 20, coins: 10, health: 0 }, | ||
'badHabit': { experience: 0, coins: 0, health: -10 }, | ||
'todo': { experience: 20, coins: 10, health: 0 }, | ||
'daily': { experience: 20, coins: 10, health: 0 }, | ||
}; | ||
|
||
const reward = taskRewards[taskType]; | ||
if (!reward) { | ||
throw new Error('Invalid task type'); | ||
} | ||
|
||
// Update the profile with rewards | ||
profile.health = Math.max(0, profile.health + reward.health); | ||
profile.coins += reward.coins; | ||
profile.experience += reward.experience; | ||
|
||
// Check for level up | ||
if (profile.experience >= 100) { | ||
profile.level += 1; | ||
profile.experience -= 100; | ||
profile.health = 100; | ||
} | ||
|
||
// Save changes to the database | ||
await profile.save(); | ||
|
||
return profile; | ||
} | ||
|
||
module.exports = updateProfileAfterTaskCompletion; |
60c1290
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report for backend
Caution
Test run failed
Test suite run failed
Failed tests: 1/7. Failed suites: 2/3.
Report generated by 🧪jest coverage report action from 60c1290
60c1290
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report for frontend
Report generated by 🧪jest coverage report action from 60c1290