Add node-fetch dependency and implement meeting report submission endpoint
This commit introduces the node-fetch package to facilitate HTTP requests in the backend. Additionally, a new API endpoint for submitting meeting reports has been implemented in the nuscoreApiRoutes. The endpoint handles report data submission, cookie management, and error handling, enhancing the functionality of the meeting report feature. Frontend components have been updated to support this new functionality, improving the overall user experience.
This commit is contained in:
@@ -182,4 +182,93 @@ router.get('/meetingdetails/:uuid', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Submit Meeting Report API-Endpunkt
|
||||
router.put('/submit/:uuid', async (req, res) => {
|
||||
const { uuid } = req.params;
|
||||
const reportData = req.body;
|
||||
|
||||
try {
|
||||
// Hole Cookies für diese UUID (falls vorhanden)
|
||||
// Versuche zuerst UUID, dann Code als Fallback
|
||||
let cookies = cookieStore.get(uuid) || {};
|
||||
|
||||
// Falls keine Cookies für UUID vorhanden, versuche Code zu finden
|
||||
if (Object.keys(cookies).length === 0 && reportData.gameCode) {
|
||||
cookies = cookieStore.get(reportData.gameCode) || {};
|
||||
}
|
||||
|
||||
const url = `https://ttde-apps.liga.nu/nuliga/rs/tt/2022/meetingentry/reports/${uuid}/submit`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:141.0) Gecko/20100101 Firefox/141.0',
|
||||
'Accept': 'application/json, text/plain, */*',
|
||||
'Accept-Language': 'de,en-US;q=0.7,en;q=0.3',
|
||||
'Accept-Encoding': 'gzip, deflate, br, zstd',
|
||||
'Content-Type': 'application/json',
|
||||
'Referer': `https://ttde-apps.liga.nu/nuliga/nuscore-tt/meeting/${uuid}/report-clearance/review`,
|
||||
'Origin': 'https://ttde-apps.liga.nu',
|
||||
'Sec-Fetch-Dest': 'empty',
|
||||
'Sec-Fetch-Mode': 'cors',
|
||||
'Sec-Fetch-Site': 'same-origin',
|
||||
'Connection': 'keep-alive',
|
||||
...(Object.keys(cookies).length > 0 && { 'Cookie': formatCookies(cookies) })
|
||||
},
|
||||
body: JSON.stringify(reportData)
|
||||
});
|
||||
|
||||
const responseText = await response.text();
|
||||
let responseData;
|
||||
|
||||
try {
|
||||
responseData = JSON.parse(responseText);
|
||||
} catch (e) {
|
||||
// Falls keine JSON-Antwort, verwende Text
|
||||
responseData = { message: responseText };
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
console.error(`❌ Submit fehlgeschlagen: HTTP ${response.status}`, responseData);
|
||||
return res.status(response.status).json({
|
||||
error: 'Fehler beim Absenden des Spielberichts',
|
||||
details: responseData,
|
||||
status: response.status,
|
||||
statusText: response.statusText
|
||||
});
|
||||
}
|
||||
|
||||
// Speichere neue Cookies falls vorhanden
|
||||
const newCookies = extractCookies(response.headers.raw()['set-cookie']);
|
||||
if (Object.keys(newCookies).length > 0) {
|
||||
cookieStore.set(uuid, { ...cookies, ...newCookies });
|
||||
if (reportData.gameCode) {
|
||||
cookieStore.set(reportData.gameCode, { ...cookies, ...newCookies });
|
||||
}
|
||||
}
|
||||
|
||||
// CORS-Header setzen
|
||||
res.set({
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, POST, PUT, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type',
|
||||
'Cache-Control': 'no-cache, no-store, must-revalidate'
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: responseData,
|
||||
message: 'Spielbericht erfolgreich abgesendet'
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error(`❌ Fehler beim Absenden des Spielberichts für UUID ${uuid}:`, error);
|
||||
res.status(500).json({
|
||||
error: 'Fehler beim Absenden des Spielberichts',
|
||||
details: error.message
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user