Geburtstagsanzeige in allen Listen

This commit is contained in:
klaas 2024-10-10 22:32:24 +02:00
parent e8a899b601
commit 0bd0b21256
5 changed files with 44 additions and 15 deletions

39
app.js
View File

@ -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 });

View File

@ -49,11 +49,13 @@
<a href="/mitglied/<%= teilnehmer.id %>"
><strong><%= teilnehmer.vorname %></strong></a
>
<% } else { %>
<%= teilnehmer.hasBirthday == 2 ? '🎂🎂' : '' %><%=
teilnehmer.hasBirthday == 1 ? '🎂' : '' %> <% } else { %>
<a href="/mitglied/<%= teilnehmer.id %>"
><%= teilnehmer.vorname %></a
>
<% } %>
<%= teilnehmer.hasBirthday == 2 ? '🎂🎂' : '' %><%=
teilnehmer.hasBirthday == 1 ? '🎂' : '' %> <% } %>
</td>
<td>
<% if (teilnehmer.helfer) { %>

View File

@ -18,7 +18,7 @@
<% if (teilnehmer.helfer) { %>
<a href="/mitglied/<%= teilnehmer.id %>"><strong><%= teilnehmer.vorname %></strong></a>
<% } else { %>
<a href="/mitglied/<%= teilnehmer.id %>"><%= teilnehmer.vorname %></a>
<a href="/mitglied/<%= teilnehmer.id %>"><%= teilnehmer.vorname %> <%= teilnehmer.hasBirthday == 2 ? '🎂🎂' : '' %><%= teilnehmer.hasBirthday == 1 ? '🎂' : '' %></a>
<% } %>
</td>
<td>

View File

@ -170,7 +170,7 @@
<% riegen[riegennummer].forEach(teilnehmer => { if(teilnehmer.helfer) { %>
<tr>
<td>
<%= teilnehmer.vorname %>
<%= teilnehmer.vorname %> <%= teilnehmer.hasBirthday == 2 ? '🎂🎂' : '' %><%= teilnehmer.hasBirthday == 1 ? '🎂' : '' %>
</td>
<td>
<%= teilnehmer.nachname %>
@ -180,14 +180,14 @@
</td>
<td>
<input type="hidden" name="inriege" value="<%= teilnehmer.id %>" /> <input type="checkbox" name="anw" <%= teilnehmer.anwesend ? 'checked' : '' %> value="<%= teilnehmer.id %>">
<% }}) %>
</td>
</tr>
<% }}) %>
<% riegen[riegennummer].forEach(teilnehmer => { if(!teilnehmer.helfer) { %>
<tr>
<td>
<%= teilnehmer.vorname %>
<%= teilnehmer.vorname %> <%= teilnehmer.hasBirthday == 2 ? '🎂🎂' : '' %><%= teilnehmer.hasBirthday == 1 ? '🎂' : '' %>
</td>
<td>
<%= teilnehmer.nachname %>

View File

@ -282,7 +282,7 @@
<% riegen[riegennummer].forEach(teilnehmer => { if(teilnehmer.helfer) { %>
<tr>
<td>
<%= teilnehmer.vorname %>
<%= teilnehmer.vorname %> <%= teilnehmer.hasBirthday == 2 ? '🎂🎂' : '' %><%= teilnehmer.hasBirthday == 1 ? '🎂' : '' %>
</td>
<td>
<%= teilnehmer.nachname %>
@ -299,7 +299,7 @@
<% riegen[riegennummer].forEach(teilnehmer => { if(!teilnehmer.helfer) { %>
<tr>
<td>
<%= teilnehmer.vorname %>
<%= teilnehmer.vorname %> <%= teilnehmer.hasBirthday == 2 ? '🎂🎂' : '' %><%= teilnehmer.hasBirthday == 1 ? '🎂' : '' %>
</td>
<td>
<%= teilnehmer.nachname %>