Sohail: Phase 1 - Fix Personal Max Record badge#2116
Sohail: Phase 1 - Fix Personal Max Record badge#2116one-community merged 1 commit intodevelopmentfrom
Conversation
|
Anusha-Gali
left a comment
There was a problem hiding this comment.
Hi Sohail,
I tried to review your PR locally and i was able to perform the first test sucessfully however for the second test when i have updated the lastWeekTangibleHrs = 20, and ran the postman and the npm test, the personalBestMaxHrs did not update to 20 from 10. As you cam see in the below test 2 screenshots, i had provided the before test run and after test run mongo db screenshots, even though the batches got assigned, the value did not change in personalBestMaxHrs.
Hi @Anusha-Gali , In my logic, the record comparison uses In your screenshot/values, you assigned
To test this correctly, please ensure that the |
|
Thank you all, merging! |







































Description
This PR fixes critical issues with the Personal Max badge assignment and update logic that were causing the badge to never/incorrectly update when a user broke their personal weekly hours record.
Fixes: Personal Max badge assignment and update logic (Priority: High)
Original PR: #1193
Issues Fixed:
personalBestMaxHrsto be updated by$maxearlier in the weekly flow beforecheckPersonalMaxran, meaning the comparisonlastWeek > personalBestalways evaluated to false and the badge never updated.earnedDatewas being appended to rather than replaced when a new record was set.lastSaved > lastWeekwas always false since both values are set to the sametimeSpentin the same atomic DB operation.Main changes explained:
src/helpers/userHelper.js: RewrotecheckPersonalMaxfunction with correct badge assignment logicearnedDateis replaced with the current date andpersonalBestMaxHrsis updated in the same atomic DB operationMath.max(...savedTangibleHrs.slice(0, -1))to derive the previous best from history, bypassing the race condition withpersonalBestMaxHrssrc/helpers/__tests__/checkPersonalMax.spec.js: Unit test file covering 12 edge cases, all passingHow to test:
npm installto ensure dependencies are up to datenpm run devto start the server locally/api/login, save the response auth token as a header in PostmansavedTangibleHrs(For assuming hours for the most recent week, assignlastWeekTangibleHours)/api/badge/awardNewBadgesImportant
Replace
awardNewBadgesinsrc/helpers/userHelper.jswith this before sending an API call to ensure you only test for your dev account:Test Scenario 1: New User — First Week Logging Hours
savedTangibleHrs= [10],lastWeekTangibleHrs= 10,personalBestMaxHrs= 0earnedDate= today,personalBestMaxHrs= 10Test Scenario 2: Record Broken
savedTangibleHrs= [10, 8, 15, 12, 20],lastWeekTangibleHrs= 20earnedDateshould update to today,personalBestMaxHrs= 20Test Scenario 3: Record Not Broken / Tied
lastWeekTangibleHrsto a value equal to or less than the max of prior weeksTest Scenario 4: Zero Hours Logged
lastWeekTangibleHrs= 0Test Scenario 5: Duplicate Badge Cleanup
Expected Results:
earnedDateis replaced (not appended) when a new record is setpersonalBestMaxHrsis updated atomically alongside the badgeNote:
awardNewBadges) runs every Sunday at 12 AM and callscheckPersonalMaxfor each active usersavedTangibleHrshistory (last 200 weeks) excluding the current week, not the storedpersonalBestMaxHrsfield, to avoid the race condition with the$maxupdate earlier in the weekly flow