From 558544e46187685ff0aafea819cc6fe65b149357 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 25 May 2026 08:29:01 +0000 Subject: [PATCH] feat: team indeling endpoint + proposed_teams field --- db.cjs | 10 ++++++++-- server.js | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/db.cjs b/db.cjs index d5522ae..ba50555 100644 --- a/db.cjs +++ b/db.cjs @@ -286,11 +286,11 @@ function updateMatch(id, match) { // Build SET dynamically to only update provided fields const fields = []; const values = []; - for (const key of ['status', 'responses', 'proposed_at', 'date', 'start', 'end', 'location', 'day', 'score', 'booker_id', 'booker_name']) { + for (const key of ['status', 'responses', 'proposed_at', 'date', 'start', 'end', 'location', 'day', 'score', 'booker_id', 'booker_name', 'proposed_teams']) { if (match[key] !== undefined) { fields.push(`${key}=?`); let val = merged[key]; - if (key === 'responses' || (key === 'score' && typeof val === 'object' && val !== null)) { + if (key === 'responses' || key === 'score' || key === 'proposed_teams') { val = JSON.stringify(val); } values.push(key === 'booker_id' || key === 'booker_name' ? String(val) : val); @@ -446,6 +446,11 @@ function rowToMatch(r) { try { score = JSON.parse(r.score); } catch (e) { /* ignore */ } } + let proposed_teams = null; + if (r.proposed_teams) { + try { proposed_teams = JSON.parse(r.proposed_teams); } catch (e) { /* ignore */ } + } + return { id: r.id, status: r.status, @@ -457,6 +462,7 @@ function rowToMatch(r) { location: r.location, day: r.day, score: score, + proposed_teams: proposed_teams, booker_id: r.booker_id || null, booker_name: r.booker_name || null, players: players diff --git a/server.js b/server.js index a429b5e..13914e5 100644 --- a/server.js +++ b/server.js @@ -363,6 +363,29 @@ app.delete("/api/matches/:id", (req, res) => { res.json({ ok: true }); }); +// ─── TEAM INDELING (voorgesteld) ─── +app.post("/api/matches/:id/teams", (req, res) => { + let m = db.getMatch(String(req.params.id)); + if (!m) return res.status(404).json({ error: "match not found" }); + const { team1, team2 } = req.body; + if (!Array.isArray(team1) || !Array.isArray(team2)) + return res.status(400).json({ error: "team1 and team2 must be arrays" }); + const allIds = [...team1, ...team2]; + if (allIds.length !== 4) + return res.status(400).json({ error: "expected 4 players total" }); + db.updateMatch(String(req.params.id), { proposed_teams: { team1, team2 } }); + m = db.getMatch(String(req.params.id)); + const playerIds = (m.players || []).map(p => p.id); + res.json({ ...m, players: playerIds, players_array: m.players }); +}); + +app.get("/api/matches/:id/teams", (req, res) => { + const m = db.getMatch(String(req.params.id)); + if (!m) return res.status(404).json({ error: "match not found" }); + const teams = m.proposed_teams || null; + res.json({ teams, match: { id: m.id, date: m.date, start: m.start, status: m.status } }); +}); + // ─── SCORES ─── app.post("/api/matches/:id/score", (req, res) => { let m = db.getMatch(String(req.params.id));