|
| 1 | +# Badge assignment API |
| 2 | + |
| 3 | +Use these endpoints from the **Badge Management → Badge Assignment** UI. |
| 4 | + |
| 5 | +## Authentication |
| 6 | + |
| 7 | +- Send **Authorization** header with a valid JWT. |
| 8 | +- The backend sets `requestor` on `req.body` from the token (you do not need to send it in the body). |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## Option 1: Single user (one request per user) |
| 13 | + |
| 14 | +**PUT** `/api/badge/assign/:userId` |
| 15 | + |
| 16 | +- **URL:** Replace `:userId` with one user’s MongoDB ObjectId (e.g. `PUT /api/badge/assign/507f1f77bcf86cd799439011`). |
| 17 | +- **Body (JSON):** |
| 18 | + ```json |
| 19 | + { |
| 20 | + "badgeCollection": [ |
| 21 | + { "badge": "<badgeId>", "count": 1, "featured": false } |
| 22 | + ] |
| 23 | + } |
| 24 | + ``` |
| 25 | +- **Success:** 201, body = user profile id. |
| 26 | +- **Errors:** 400 (bad request), 403 (forbidden), 500 (server error). Response body is a string message. |
| 27 | + |
| 28 | +To assign to **multiple users**, call this endpoint **once per user** with the same `badgeCollection`. |
| 29 | + |
| 30 | +--- |
| 31 | + |
| 32 | +## Option 2: Bulk (multiple users in one request) |
| 33 | + |
| 34 | +**POST** `/api/badge/assign/bulk` |
| 35 | + |
| 36 | +- **Body (JSON):** |
| 37 | + ```json |
| 38 | + { |
| 39 | + "userIds": ["<userId1>", "<userId2>"], |
| 40 | + "badgeCollection": [ |
| 41 | + { "badge": "<badgeId>", "count": 1, "featured": false } |
| 42 | + ] |
| 43 | + } |
| 44 | + ``` |
| 45 | +- **Success:** 201, body = `{ "assigned": ["id1", "id2"], "failed": [] }`. |
| 46 | +- **Errors:** 400/403/500 with JSON `{ "error": "..." }`. Partial success still returns 201 with `assigned` and `failed` arrays. |
| 47 | + |
| 48 | +Use this when the UI sends **one request** with multiple selected users. |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## Frontend checklist |
| 53 | + |
| 54 | +1. **URL** |
| 55 | + - Single: `PUT /api/badge/assign/{userId}` (one call per user). |
| 56 | + - Bulk: `POST /api/badge/assign/bulk` with `userIds` and `badgeCollection` in the body. |
| 57 | + |
| 58 | +2. **Headers** |
| 59 | + - `Authorization: <JWT>` |
| 60 | + - `Content-Type: application/json` |
| 61 | + |
| 62 | +3. **Body** |
| 63 | + - Always send `badgeCollection` as an array of `{ badge, count, ... }`. |
| 64 | + - For bulk, also send `userIds` as an array of user id strings. |
| 65 | + |
| 66 | +4. **Error handling** |
| 67 | + - Do not show a generic “Oops” for every failure. Use the **response status** and **response body** (string or `error` field) to show a specific message (e.g. “User not found”, “Badge collection is required”). |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## Debugging |
| 72 | + |
| 73 | +- Restart the backend after code changes. |
| 74 | +- In the **server console** you should see lines like: |
| 75 | + - `[Badge Assign] PUT /badge/assign called. userId=...` |
| 76 | + - or `[Badge Assign Bulk] POST /badge/assign/bulk called. userIds=...` |
| 77 | +- If you never see these when clicking Assign, the request is not reaching this API (check frontend URL and Network tab). |
| 78 | +- In the browser **Network** tab, inspect the failing request: URL, method, request payload, and response status/body. |
0 commit comments