Spaces:
Running
Running
Expose admin billing usage counts
Browse files- src/server.ts +8 -6
src/server.ts
CHANGED
|
@@ -201,16 +201,13 @@ app.get("/api/plan-billing", async (req, res, next) => {
|
|
| 201 |
const current = await authenticatedUser(req, res);
|
| 202 |
if (!current) return res.status(401).json({ error: "Authentication required." });
|
| 203 |
const userId = current.user.id;
|
| 204 |
-
|
| 205 |
-
res.set("Cache-Control", "no-store");
|
| 206 |
-
return res.json({ subscription: null, usage: { players: 0, sessions: 0 }, billingHistoryAvailable: false, isAdmin: true });
|
| 207 |
-
}
|
| 208 |
const assignmentResult = await supabaseAdmin.from("users_subscriptions")
|
| 209 |
.select("subscription_id,startdate,enddate,num_players,num_sessions").eq("user_id", userId).maybeSingle();
|
| 210 |
if (assignmentResult.error) throw new Error(`Unable to load plan assignment: ${assignmentResult.error.message}`);
|
| 211 |
const assignment = assignmentResult.data;
|
| 212 |
let subscription = null;
|
| 213 |
-
if (assignment?.subscription_id) {
|
| 214 |
const { data: plan, error } = await supabaseAdmin.from("subscriptions")
|
| 215 |
.select("subscription_id,plan,price,player_limit,session_limit").eq("subscription_id", assignment.subscription_id).maybeSingle();
|
| 216 |
if (error) throw new Error(`Unable to load plan: ${error.message}`);
|
|
@@ -229,7 +226,12 @@ app.get("/api/plan-billing", async (req, res, next) => {
|
|
| 229 |
}
|
| 230 |
}
|
| 231 |
res.set("Cache-Control", "no-store");
|
| 232 |
-
return res.json({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
} catch (error) { next(error); }
|
| 234 |
});
|
| 235 |
|
|
|
|
| 201 |
const current = await authenticatedUser(req, res);
|
| 202 |
if (!current) return res.status(401).json({ error: "Authentication required." });
|
| 203 |
const userId = current.user.id;
|
| 204 |
+
const isAdmin = await userIsAdmin(userId);
|
|
|
|
|
|
|
|
|
|
| 205 |
const assignmentResult = await supabaseAdmin.from("users_subscriptions")
|
| 206 |
.select("subscription_id,startdate,enddate,num_players,num_sessions").eq("user_id", userId).maybeSingle();
|
| 207 |
if (assignmentResult.error) throw new Error(`Unable to load plan assignment: ${assignmentResult.error.message}`);
|
| 208 |
const assignment = assignmentResult.data;
|
| 209 |
let subscription = null;
|
| 210 |
+
if (!isAdmin && assignment?.subscription_id) {
|
| 211 |
const { data: plan, error } = await supabaseAdmin.from("subscriptions")
|
| 212 |
.select("subscription_id,plan,price,player_limit,session_limit").eq("subscription_id", assignment.subscription_id).maybeSingle();
|
| 213 |
if (error) throw new Error(`Unable to load plan: ${error.message}`);
|
|
|
|
| 226 |
}
|
| 227 |
}
|
| 228 |
res.set("Cache-Control", "no-store");
|
| 229 |
+
return res.json({
|
| 230 |
+
subscription: isAdmin ? null : subscription,
|
| 231 |
+
usage: { players: assignment?.num_players ?? 0, sessions: Number(assignment?.num_sessions ?? 0) },
|
| 232 |
+
billingHistoryAvailable: false,
|
| 233 |
+
isAdmin,
|
| 234 |
+
});
|
| 235 |
} catch (error) { next(error); }
|
| 236 |
});
|
| 237 |
|