Skip to content

Amaresh: Add POST /badge/assign endpoint for multi-user badge assignment - #2334

Open
amaresh2001 wants to merge 2 commits into
developmentfrom
amaresh/badge-assign-multiple-users
Open

Amaresh: Add POST /badge/assign endpoint for multi-user badge assignment#2334
amaresh2001 wants to merge 2 commits into
developmentfrom
amaresh/badge-assign-multiple-users

Conversation

@amaresh2001

Copy link
Copy Markdown

Description

The Badge Assignment feature on the frontend lets an admin select multiple users and multiple badges and assign them in one action. The frontend action assignBadgesToMultipleUserID sends POST /api/badge/assign with { userIds, selectedBadges }, but that route never existed on the backend; only the single-user PUT /api/badge/assign/:userId did. Every bulk assignment hit a 404, and the UI showed "Oops, something went wrong while assigning badges!".

This PR adds the missing endpoint.

Fixes the badge-assignment failure reported on frontend PR #4734

Related PRS (if any):

Paired with frontend PR OneCommunityGlobal/HighestGoodNetworkApp#4734 (branch Amalesh-3940-bugfix).
To test this backend PR you need to check out the #4734 frontend branch and run it against this backend branch.

Main changes explained:

  • Update src/controllers/badgeController.js: add assignBadgesToMultipleUsers:
    • Requires the assignBadges permission (403 otherwise)
    • Validates userIds and selectedBadges are non-empty arrays (400 otherwise)
    • For each user, for each selected badge: increments count and pushes a new earnedDate if the user already has that badge; otherwise, adds a new badgeCollection entry with count: 1 and featured: false; bumps badgeCount by selectedBadges.length; clears that user's cache
    • Per-user try/catch so one bad ID does not fail the whole batch; returns 200 with a per-user results array, and 400 only if every user failed
  • Update src/routes/badgeRouter.js: wire POST /badge/assign to controller.assignBadgesToMultipleUsers (added alongside the existing PUT /badge/assign/:userId)

How to test:

  1. Check out this branch (amaresh/badge-assign-multiple-users) in HGNRest, run npm install, then npm run dev (backend on http://localhost:4500)
  2. In HighestGoodNetworkApp, check out Amalesh-3940-bugfix, run npm install, then npm run start:local (frontend on http://localhost:5173)
  3. Clear site data / cache, log in as an Admin or Owner account (needs the assignBadges permission)
  4. Go to /badgemanagement → Badge Assignment tab
  5. Search a name, tick 2+ users → the status line should read N user(s) selected
  6. Click Assign Badge, tick 1+ badges → status line shows N badge(s) selected
  7. Click Confirm → expected: green success alert "Awesomesauce! ...for multiple users!" (not the red "Oops" error)
  8. Open one of the selected users' profiles → Featured Badges section shows the assigned badge with the correct count; assigning increments the count again rather than adding a duplicate entry
  9. Repeat as an Owner and as an Admin; both succeed. A user without assignBadges gets a 403.

Screenshots or videos of changes:

Screenshot 2026-09-05 at 1 48 28 PM Screenshot 2026-09-05 at 1 49 14 PM Screenshot 2026-09-05 at 1 49 28 PM

Note:

@sonarqubecloud

sonarqubecloud Bot commented Sep 5, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant