Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions react-ystemandchess/src/core/services/activitiesApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { environment } from "../../environments/environment";

/**
* Marks a specific activity as completed for the given user via
* PUT /activities/:username/activity.
*
* @param {string} username - User's own username (must match the JWT)
* @param {string} token - Bearer token (e.g. from the 'login' cookie)
* @param {string} activityId - ID of the activity being completed
* @returns {Promise<any>} Updated activities payload
* @throws {Error} If the API request fails
*/
export async function completeActivity(username: string, token: string, activityName: string) {
const res = await fetch(
`${environment.urls.middlewareURL}/activities/${username}/activity`,
{
method: "PUT",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ activityName }),
}
);
if (!res.ok) throw new Error("Failed to update activity");
return res.json();
}
29 changes: 29 additions & 0 deletions react-ystemandchess/src/core/services/badgesApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,32 @@ export async function getUserBadges(userId: string, token: string) {
if (!res.ok) throw new Error("Failed to fetch earned badges");
return (await res.json()).earned;
}

/**
* Triggers a server-side check of the user's current stats and awards
* any badges they've newly qualified for. Server-authoritative — it
* recomputes stats itself and does not accept client-supplied predicates.
*
* The backend only permits a caller to check/award their own badges
* (:userId must match the authenticated JWT's username), so token is
* required here.
*
* @param {string} userId - User's own username (must match the JWT)
* @param {string} token - Bearer token (e.g. from the 'login' cookie)
* @returns {Promise<Array>} Array of newly-awarded badge objects
* @throws {Error} If the API request fails
*/
export async function checkAndAward(userId: string, token: string) {
// Auth is carried by the Bearer header, not cookies — omit
// credentials: "include" for the same reason noted in getBadgeCatalog
// above (wildcard-origin CORS rejects credentialed requests).
const res = await fetch(
`${environment.urls.middlewareURL}/badges/${userId}/check-and-award`,
{
method: "POST",
headers: { Authorization: `Bearer ${token}` },
}
);
if (!res.ok) throw new Error("Failed to check and award badges");
return (await res.json()).awarded;
}
Loading