feat: match_type (ranked/friendly) + /api/matches/:id/type endpoint
This commit is contained in:
@@ -75,6 +75,9 @@ function migrateSchema() {
|
|||||||
CREATE INDEX IF NOT EXISTS idx_matchp_match ON match_players(match_id);
|
CREATE INDEX IF NOT EXISTS idx_matchp_match ON match_players(match_id);
|
||||||
CREATE INDEX IF NOT EXISTS idx_matchp_player ON match_players(player_id);
|
CREATE INDEX IF NOT EXISTS idx_matchp_player ON match_players(player_id);
|
||||||
|
|
||||||
|
-- Migration: match type kolom
|
||||||
|
ALTER TABLE matches ADD COLUMN match_type TEXT NOT NULL DEFAULT 'friendly';
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS sessions (
|
CREATE TABLE IF NOT EXISTS sessions (
|
||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
player_id TEXT REFERENCES players(id) ON DELETE SET NULL,
|
player_id TEXT REFERENCES players(id) ON DELETE SET NULL,
|
||||||
@@ -249,8 +252,8 @@ function addMatch(match) {
|
|||||||
const db = getDb();
|
const db = getDb();
|
||||||
const id = match.id || String(Date.now());
|
const id = match.id || String(Date.now());
|
||||||
const stmt = db.prepare(`
|
const stmt = db.prepare(`
|
||||||
INSERT INTO matches (id, status, responses, proposed_at, date, start, end, location, day, score, booker_id, booker_name)
|
INSERT INTO matches (id, status, responses, proposed_at, date, start, end, location, day, score, booker_id, booker_name, match_type)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
`);
|
`);
|
||||||
stmt.run(id, match.status || 'proposed',
|
stmt.run(id, match.status || 'proposed',
|
||||||
JSON.stringify(match.responses || {}),
|
JSON.stringify(match.responses || {}),
|
||||||
@@ -260,7 +263,8 @@ function addMatch(match) {
|
|||||||
match.day || null,
|
match.day || null,
|
||||||
typeof match.score === 'object' ? JSON.stringify(match.score) : (match.score || null),
|
typeof match.score === 'object' ? JSON.stringify(match.score) : (match.score || null),
|
||||||
match.booker_id || null,
|
match.booker_id || null,
|
||||||
match.booker_name || null);
|
match.booker_name || null,
|
||||||
|
match.match_type || 'friendly');
|
||||||
|
|
||||||
if (Array.isArray(match.players)) {
|
if (Array.isArray(match.players)) {
|
||||||
const pstmt = db.prepare(`INSERT INTO match_players (match_id, player_id, team, score) VALUES (?, ?, ?, ?)`);
|
const pstmt = db.prepare(`INSERT INTO match_players (match_id, player_id, team, score) VALUES (?, ?, ?, ?)`);
|
||||||
@@ -286,7 +290,7 @@ function updateMatch(id, match) {
|
|||||||
// Build SET dynamically to only update provided fields
|
// Build SET dynamically to only update provided fields
|
||||||
const fields = [];
|
const fields = [];
|
||||||
const values = [];
|
const values = [];
|
||||||
for (const key of ['status', 'responses', 'proposed_at', 'date', 'start', 'end', 'location', 'day', 'score', 'booker_id', 'booker_name', 'proposed_teams']) {
|
for (const key of ['status', 'responses', 'proposed_at', 'date', 'start', 'end', 'location', 'day', 'score', 'booker_id', 'booker_name', 'proposed_teams', 'match_type']) {
|
||||||
if (match[key] !== undefined) {
|
if (match[key] !== undefined) {
|
||||||
fields.push(`${key}=?`);
|
fields.push(`${key}=?`);
|
||||||
let val = merged[key];
|
let val = merged[key];
|
||||||
@@ -463,6 +467,7 @@ function rowToMatch(r) {
|
|||||||
day: r.day,
|
day: r.day,
|
||||||
score: score,
|
score: score,
|
||||||
proposed_teams: proposed_teams,
|
proposed_teams: proposed_teams,
|
||||||
|
match_type: r.match_type || 'friendly',
|
||||||
booker_id: r.booker_id || null,
|
booker_id: r.booker_id || null,
|
||||||
booker_name: r.booker_name || null,
|
booker_name: r.booker_name || null,
|
||||||
players: players
|
players: players
|
||||||
|
|||||||
@@ -383,7 +383,20 @@ app.get("/api/matches/:id/teams", (req, res) => {
|
|||||||
const m = db.getMatch(String(req.params.id));
|
const m = db.getMatch(String(req.params.id));
|
||||||
if (!m) return res.status(404).json({ error: "match not found" });
|
if (!m) return res.status(404).json({ error: "match not found" });
|
||||||
const teams = m.proposed_teams || null;
|
const teams = m.proposed_teams || null;
|
||||||
res.json({ teams, match: { id: m.id, date: m.date, start: m.start, status: m.status } });
|
res.json({ teams, match: { id: m.id, date: m.date, start: m.start, status: m.status, match_type: m.match_type || 'friendly' } });
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─── MATCH TYPE (ranked/friendly) ───
|
||||||
|
app.post("/api/matches/:id/type", (req, res) => {
|
||||||
|
let m = db.getMatch(String(req.params.id));
|
||||||
|
if (!m) return res.status(404).json({ error: "match not found" });
|
||||||
|
const { match_type } = req.body;
|
||||||
|
if (match_type !== 'ranked' && match_type !== 'friendly')
|
||||||
|
return res.status(400).json({ error: "match_type must be 'ranked' or 'friendly'" });
|
||||||
|
db.updateMatch(String(req.params.id), { match_type });
|
||||||
|
m = db.getMatch(String(req.params.id));
|
||||||
|
const playerIds = (m.players || []).map(p => p.id);
|
||||||
|
res.json({ ...m, players: playerIds, players_array: m.players });
|
||||||
});
|
});
|
||||||
|
|
||||||
// ─── SCORES ───
|
// ─── SCORES ───
|
||||||
|
|||||||
Reference in New Issue
Block a user