feat: add number of tables to tournament updates and enhance related UI components
All checks were successful
Deploy tt-tagebuch / deploy (push) Successful in 47s
All checks were successful
Deploy tt-tagebuch / deploy (push) Successful in 47s
This commit is contained in:
74
backend/scripts/api_put_test_numberOfTables.js
Normal file
74
backend/scripts/api_put_test_numberOfTables.js
Normal file
@@ -0,0 +1,74 @@
|
||||
import '../config.js';
|
||||
import sequelize from '../database.js';
|
||||
import { Op } from 'sequelize';
|
||||
import UserToken from '../models/UserToken.js';
|
||||
import User from '../models/User.js';
|
||||
import UserClub from '../models/UserClub.js';
|
||||
import Tournament from '../models/Tournament.js';
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
console.log('[api-test] DB connected');
|
||||
|
||||
const tournaments = await Tournament.findAll({ limit: 50 });
|
||||
if (!tournaments || tournaments.length === 0) {
|
||||
console.error('[api-test] No tournaments found');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const tokenCandidates = await UserToken.findAll({ where: { expiresAt: { [Op.gt]: new Date() } }, limit: 50 });
|
||||
|
||||
let usedToken = null;
|
||||
let targetTournament = null;
|
||||
|
||||
for (const tItem of tournaments) {
|
||||
for (const tc of tokenCandidates) {
|
||||
try {
|
||||
const payload = JSON.parse(Buffer.from(tc.token.split('.')[1] || '', 'base64').toString('utf8'));
|
||||
const user = await User.findByPk(payload.userId);
|
||||
if (!user) continue;
|
||||
const uc = await UserClub.findOne({ where: { user_id: user.id, club_id: tItem.clubId, approved: true } });
|
||||
if (uc) { usedToken = tc.token; targetTournament = tItem; break; }
|
||||
} catch (e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (usedToken) break;
|
||||
}
|
||||
|
||||
if (!usedToken) {
|
||||
console.error('[api-test] No token with club access found');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log('[api-test] Using token for tournament', targetTournament.id, 'club', targetTournament.clubId);
|
||||
|
||||
const url = `http://localhost:3005/tournament/${targetTournament.clubId}/${targetTournament.id}`;
|
||||
const payload = {
|
||||
name: targetTournament.name || 'Test Tournament',
|
||||
date: targetTournament.date,
|
||||
winningSets: targetTournament.winningSets || 3,
|
||||
numberOfTables: 9
|
||||
};
|
||||
|
||||
const res = await fetch(url, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'authcode': usedToken
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
console.log('[api-test] HTTP status', res.status);
|
||||
const body = await res.text();
|
||||
console.log('[api-test] Response body:', body);
|
||||
process.exit(res.ok ? 0 : 2);
|
||||
} catch (err) {
|
||||
console.error('[api-test] Error:', err);
|
||||
process.exit(3);
|
||||
}
|
||||
}
|
||||
|
||||
run();
|
||||
66
backend/scripts/local_test_update_numberOfTables.js
Normal file
66
backend/scripts/local_test_update_numberOfTables.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import '../config.js';
|
||||
import sequelize from '../database.js';
|
||||
import { Op } from 'sequelize';
|
||||
import UserToken from '../models/UserToken.js';
|
||||
import User from '../models/User.js';
|
||||
import UserClub from '../models/UserClub.js';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import Tournament from '../models/Tournament.js';
|
||||
import tournamentService from '../services/tournamentService.js';
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
console.log('[test] DB connected');
|
||||
|
||||
const tokenCandidates = await UserToken.findAll({ where: { expiresAt: { [Op.gt]: new Date() } }, limit: 50 });
|
||||
let token = null;
|
||||
|
||||
const tournaments = await Tournament.findAll({ limit: 50 });
|
||||
if (!tournaments || tournaments.length === 0) {
|
||||
console.error('[test] No tournaments found');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
let performed = false;
|
||||
for (const tournamentItem of tournaments) {
|
||||
// try to find token for this tournament's club
|
||||
for (const t of tokenCandidates) {
|
||||
try {
|
||||
const payload = jwt.verify(t.token, process.env.JWT_SECRET);
|
||||
const user = await User.findByPk(payload.userId);
|
||||
if (!user) continue;
|
||||
const uc = await UserClub.findOne({ where: { user_id: user.id, club_id: tournamentItem.clubId, approved: true } });
|
||||
if (!uc) continue;
|
||||
// found suitable token and tournament
|
||||
token = t.token;
|
||||
console.log('[test] Using token id=', t.id, 'userId=', user.id, 'for tournament id=', tournamentItem.id);
|
||||
const newNumber = 7;
|
||||
const updated = await tournamentService.updateTournament(token, tournamentItem.clubId, tournamentItem.id, tournamentItem.name, tournamentItem.date, tournamentItem.winningSets, newNumber);
|
||||
console.log('[test] Update successful for tournament', tournamentItem.id, 'numberOfTables now=', updated.numberOfTables);
|
||||
performed = true;
|
||||
break;
|
||||
} catch (e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (performed) break;
|
||||
}
|
||||
|
||||
if (!performed) {
|
||||
console.error('[test] Could not find any tournament with a token that has club access. Falling back to direct DB update test.');
|
||||
const t0 = tournaments[0];
|
||||
const old = t0.numberOfTables;
|
||||
t0.numberOfTables = 11;
|
||||
await t0.save();
|
||||
console.log('[test] Direct DB update successful. tournament id=', t0.id, 'old=', old, 'new=', t0.numberOfTables);
|
||||
process.exit(0);
|
||||
}
|
||||
process.exit(0);
|
||||
} catch (err) {
|
||||
console.error('[test] Error:', err);
|
||||
process.exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
run();
|
||||
Reference in New Issue
Block a user