From 0bd0b21256bf28730825d78d2295e681ce1b680c Mon Sep 17 00:00:00 2001 From: klaas Date: Thu, 10 Oct 2024 22:32:24 +0200 Subject: [PATCH] Geburtstagsanzeige in allen Listen --- app.js | 39 ++++++++++++++++++++++++++++++++------ views/riegen.ejs | 6 ++++-- views/teilnehmer.ejs | 2 +- views/trainings.ejs | 8 ++++---- views/trainings_riegen.ejs | 4 ++-- 5 files changed, 44 insertions(+), 15 deletions(-) diff --git a/app.js b/app.js index 4d1202d..d603760 100644 --- a/app.js +++ b/app.js @@ -97,6 +97,21 @@ const calculateAge = (birthdate) => { return age; }; +const isBirthday = (birthdate, date, previousTraining) => { + let adjustedBirthdate = new Date(birthdate); + adjustedBirthdate.setFullYear(date.getFullYear()); + + if (adjustedBirthdate === date) { + console.log("birthday"); + return 2; + } + if (adjustedBirthdate >= previousTraining && adjustedBirthdate <= date) { + console.log("had birthday", adjustedBirthdate, date, previousTraining); + return 1; + } + return 0; +}; + // Datumsformatierung const formatDate = (date) => { const d = new Date(date); @@ -683,12 +698,12 @@ app.get("/training", requireAuth, async (req, res) => { // Vorheriges Training ermitteln const previousTrainingResult = await pool.query( - "SELECT * FROM trainings WHERE kw < $1 ORDER BY kw DESC LIMIT 1", - [selectedKW] + "SELECT * FROM trainings WHERE id < $1 ORDER BY id DESC LIMIT 1", + [training.id] ); const nextTrainingResult = await pool.query( - "SELECT * FROM trainings WHERE kw > $1 ORDER BY kw ASC LIMIT 1", - [selectedKW] + "SELECT * FROM trainings WHERE id > $1 ORDER BY id ASC LIMIT 1", + [training.id] ); const previousTraining = previousTrainingResult.rows.length > 0 @@ -696,7 +711,6 @@ app.get("/training", requireAuth, async (req, res) => { : null; const nextTraining = nextTrainingResult.rows.length > 0 ? nextTrainingResult.rows[0] : null; - // Abrufen der Riegendaten einschließlich der Teilnehmer und deren Altersberechnung const result = await pool.query(` SELECT r.riegennummer, @@ -719,6 +733,11 @@ ORDER BY r.riegennummer, t.geburtsdatum ASC; const riegen = {}; result.rows.forEach((row) => { const age = calculateAge(row.geburtsdatum); + const hasBirthday = isBirthday( + row.geburtsdatum, + training.datum, + previousTraining.datum + ); const tnAnwesend = getAnwesenheit(row.id, anwesend); if (!riegen[row.riegennummer]) { riegen[row.riegennummer] = []; @@ -729,6 +748,7 @@ ORDER BY r.riegennummer, t.geburtsdatum ASC; vorname: row.vorname, nachname: row.nachname, geb: row.geburtsdatum, + hasBirthday: hasBirthday, age: age, helfer: row.helfer, probe: row.probe, @@ -801,6 +821,9 @@ ORDER BY r.riegennummer, t.geburtsdatum ASC; `); // Gruppieren der Riegenteilnehmer nach Riegennummer + const date = new Date(); + let previous = new Date(date); + previous.setDate(date.getDate() - 7); const riegen = {}; result.rows.forEach((row) => { const age = calculateAge(row.geburtsdatum); @@ -812,6 +835,7 @@ ORDER BY r.riegennummer, t.geburtsdatum ASC; name: row.name, vorname: row.vorname, nachname: row.nachname, + hasBirthday: isBirthday(row.geburtsdatum, date, previous), age: age, helfer: row.helfer, probe: row.probe, @@ -832,10 +856,13 @@ app.get("/teilnehmer", requireAuth, requireAdmin, async (req, res) => { const teilnehmendeResult = await pool.query( "SELECT m.*, COUNT(a.fid_teilnehmer) AS anwesenheit FROM teilnehmende m LEFT JOIN anwesend a ON m.id = a.fid_teilnehmer GROUP BY m.id ORDER BY helfer DESC, vorname ASC" ); - + const date = new Date(); + let previous = new Date(date); + previous.setDate(date.getDate() - 7); const teilnehmende = teilnehmendeResult.rows.map((t) => ({ ...t, age: calculateAge(t.geburtsdatum), + hasBirthday: isBirthday(t.geburtsdatum, date, previous), })); res.render("teilnehmer", { teilnehmende, session: req.session }); diff --git a/views/riegen.ejs b/views/riegen.ejs index 3dfa09a..4bd1f95 100644 --- a/views/riegen.ejs +++ b/views/riegen.ejs @@ -49,11 +49,13 @@ <%= teilnehmer.vorname %> - <% } else { %> + <%= teilnehmer.hasBirthday == 2 ? 'πŸŽ‚πŸŽ‚' : '' %><%= + teilnehmer.hasBirthday == 1 ? 'πŸŽ‚' : '' %> <% } else { %> <%= teilnehmer.vorname %> - <% } %> + <%= teilnehmer.hasBirthday == 2 ? 'πŸŽ‚πŸŽ‚' : '' %><%= + teilnehmer.hasBirthday == 1 ? 'πŸŽ‚' : '' %> <% } %> <% if (teilnehmer.helfer) { %> diff --git a/views/teilnehmer.ejs b/views/teilnehmer.ejs index 34d50e1..c484a5d 100644 --- a/views/teilnehmer.ejs +++ b/views/teilnehmer.ejs @@ -18,7 +18,7 @@ <% if (teilnehmer.helfer) { %> <%= teilnehmer.vorname %> <% } else { %> - <%= teilnehmer.vorname %> + <%= teilnehmer.vorname %> <%= teilnehmer.hasBirthday == 2 ? 'πŸŽ‚πŸŽ‚' : '' %><%= teilnehmer.hasBirthday == 1 ? 'πŸŽ‚' : '' %> <% } %> diff --git a/views/trainings.ejs b/views/trainings.ejs index a96b776..898cca5 100644 --- a/views/trainings.ejs +++ b/views/trainings.ejs @@ -170,7 +170,7 @@ <% riegen[riegennummer].forEach(teilnehmer => { if(teilnehmer.helfer) { %> - <%= teilnehmer.vorname %> + <%= teilnehmer.vorname %> <%= teilnehmer.hasBirthday == 2 ? 'πŸŽ‚πŸŽ‚' : '' %><%= teilnehmer.hasBirthday == 1 ? 'πŸŽ‚' : '' %> <%= teilnehmer.nachname %> @@ -180,14 +180,14 @@ value="<%= teilnehmer.id %>"> - <% }}) %> - + + <% }}) %> <% riegen[riegennummer].forEach(teilnehmer => { if(!teilnehmer.helfer) { %> - <%= teilnehmer.vorname %> + <%= teilnehmer.vorname %> <%= teilnehmer.hasBirthday == 2 ? 'πŸŽ‚πŸŽ‚' : '' %><%= teilnehmer.hasBirthday == 1 ? 'πŸŽ‚' : '' %> <%= teilnehmer.nachname %> diff --git a/views/trainings_riegen.ejs b/views/trainings_riegen.ejs index 0fdb582..d503afc 100644 --- a/views/trainings_riegen.ejs +++ b/views/trainings_riegen.ejs @@ -282,7 +282,7 @@ <% riegen[riegennummer].forEach(teilnehmer => { if(teilnehmer.helfer) { %> - <%= teilnehmer.vorname %> + <%= teilnehmer.vorname %> <%= teilnehmer.hasBirthday == 2 ? 'πŸŽ‚πŸŽ‚' : '' %><%= teilnehmer.hasBirthday == 1 ? 'πŸŽ‚' : '' %> <%= teilnehmer.nachname %> @@ -299,7 +299,7 @@ <% riegen[riegennummer].forEach(teilnehmer => { if(!teilnehmer.helfer) { %> - <%= teilnehmer.vorname %> + <%= teilnehmer.vorname %> <%= teilnehmer.hasBirthday == 2 ? 'πŸŽ‚πŸŽ‚' : '' %><%= teilnehmer.hasBirthday == 1 ? 'πŸŽ‚' : '' %> <%= teilnehmer.nachname %>