Spaces:
Running
Running
Accept separate player names
Browse files- src/server.ts +13 -6
src/server.ts
CHANGED
|
@@ -80,6 +80,16 @@ const playerProfileSchema = z.object({
|
|
| 80 |
position: z.string().trim().max(50),
|
| 81 |
gender: z.union([z.enum(["male", "female"]), z.literal("")]),
|
| 82 |
}).strict();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
function ageFromBirthday(birthday: string | null): string {
|
| 85 |
if (!birthday) return "";
|
|
@@ -263,16 +273,13 @@ app.post("/api/players", async (req, res, next) => {
|
|
| 263 |
const current = await requireActiveSubscription(req, res);
|
| 264 |
if (!current) return;
|
| 265 |
if (!await enforcePlayerQuota(current.user.id, current.subscription.player_limit == null ? null : Number(current.subscription.player_limit), res)) return;
|
| 266 |
-
const input =
|
| 267 |
const age = input.age ? Number(input.age) : null;
|
| 268 |
const height = input.heightCm ? Number(input.heightCm) : null;
|
| 269 |
const weight = input.weightKg ? Number(input.weightKg) : null;
|
| 270 |
if ((age !== null && (age < 4 || age > 60)) || (height !== null && (height < 100 || height > 230)) || (weight !== null && (weight < 20 || weight > 150))) {
|
| 271 |
return res.status(400).json({ error: "Check the player's age, height and weight." });
|
| 272 |
}
|
| 273 |
-
const [fname, ...lastParts] = input.name.split(/\s+/);
|
| 274 |
-
const lname = lastParts.join(" ");
|
| 275 |
-
if (fname.length > 100 || lname.length > 100) return res.status(400).json({ error: "Player first and last names must be 100 characters or fewer." });
|
| 276 |
let birthday: string | null = null;
|
| 277 |
if (age !== null) {
|
| 278 |
const date = new Date();
|
|
@@ -281,8 +288,8 @@ app.post("/api/players", async (req, res, next) => {
|
|
| 281 |
}
|
| 282 |
const { data, error } = await supabaseAdmin.from("players").insert({
|
| 283 |
user_id: current.user.id,
|
| 284 |
-
fname,
|
| 285 |
-
lname,
|
| 286 |
bdate: birthday,
|
| 287 |
height,
|
| 288 |
weight,
|
|
|
|
| 80 |
position: z.string().trim().max(50),
|
| 81 |
gender: z.union([z.enum(["male", "female"]), z.literal("")]),
|
| 82 |
}).strict();
|
| 83 |
+
const playerCreateSchema = z.object({
|
| 84 |
+
fname: z.string().trim().min(1).max(100),
|
| 85 |
+
lname: z.string().trim().min(1).max(100),
|
| 86 |
+
age: z.string().regex(/^(|\d{1,2})$/),
|
| 87 |
+
heightCm: z.string().regex(/^(|\d{2,3}(?:\.\d{1,2})?)$/),
|
| 88 |
+
weightKg: z.string().regex(/^(|\d{1,3}(?:\.\d{1,2})?)$/),
|
| 89 |
+
club: z.string().trim().max(150),
|
| 90 |
+
position: z.string().trim().max(50),
|
| 91 |
+
gender: z.union([z.enum(["male", "female"]), z.literal("")]),
|
| 92 |
+
}).strict();
|
| 93 |
|
| 94 |
function ageFromBirthday(birthday: string | null): string {
|
| 95 |
if (!birthday) return "";
|
|
|
|
| 273 |
const current = await requireActiveSubscription(req, res);
|
| 274 |
if (!current) return;
|
| 275 |
if (!await enforcePlayerQuota(current.user.id, current.subscription.player_limit == null ? null : Number(current.subscription.player_limit), res)) return;
|
| 276 |
+
const input = playerCreateSchema.parse(req.body);
|
| 277 |
const age = input.age ? Number(input.age) : null;
|
| 278 |
const height = input.heightCm ? Number(input.heightCm) : null;
|
| 279 |
const weight = input.weightKg ? Number(input.weightKg) : null;
|
| 280 |
if ((age !== null && (age < 4 || age > 60)) || (height !== null && (height < 100 || height > 230)) || (weight !== null && (weight < 20 || weight > 150))) {
|
| 281 |
return res.status(400).json({ error: "Check the player's age, height and weight." });
|
| 282 |
}
|
|
|
|
|
|
|
|
|
|
| 283 |
let birthday: string | null = null;
|
| 284 |
if (age !== null) {
|
| 285 |
const date = new Date();
|
|
|
|
| 288 |
}
|
| 289 |
const { data, error } = await supabaseAdmin.from("players").insert({
|
| 290 |
user_id: current.user.id,
|
| 291 |
+
fname: input.fname,
|
| 292 |
+
lname: input.lname,
|
| 293 |
bdate: birthday,
|
| 294 |
height,
|
| 295 |
weight,
|