Skip to content

Commit

Permalink
fix training create
Browse files Browse the repository at this point in the history
  • Loading branch information
sansan88 committed Dec 16, 2024
1 parent 88994e7 commit 44be68d
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions functions/src/firestore/training/createTeamTraining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function createTeamTraining(snapshot: QueryDocumentSnapshot, contex
const teamRef = await db.collection("teams").doc(trainingData.teamId).get();
console.log("teamId" + trainingData.teamId);

const calculatedDate: Date = new Date();
// const calculatedDate: Date = new Date();
let offSet = 0; // in milliseconds
switch (trainingData.repeatFrequency) {
case "D":
Expand All @@ -47,8 +47,14 @@ export async function createTeamTraining(snapshot: QueryDocumentSnapshot, contex
console.log(`End Date used: ${trainingData.endDate}`);

// Set Date based on first Training and Start Hours/minutes
calculatedDate.setTime(new Date(trainingData.startDate).getTime());
/* calculatedDate.setTime(new Date(trainingData.startDate).getTime());
calculatedDate.setHours(new Date(trainingData.timeFrom).getHours());
calculatedDate.setMinutes(new Date(trainingData.timeFrom).getMinutes());
calculatedDate.setSeconds(0);
calculatedDate.setMilliseconds(0); */

// Initialisierung des ersten Datums
const calculatedDate = new Date(new Date(trainingData.startDate).getTime());
calculatedDate.setHours(new Date(trainingData.timeFrom).getHours());
calculatedDate.setMinutes(new Date(trainingData.timeFrom).getMinutes());
calculatedDate.setSeconds(0);
Expand All @@ -61,7 +67,6 @@ export async function createTeamTraining(snapshot: QueryDocumentSnapshot, contex
calculatedEndDate.setSeconds(0);
calculatedEndDate.setMilliseconds(0);


// Add Training Entry
const newTrainingRef = await db.collection("teams").doc(trainingData.teamId).collection("trainings").add({
...trainingData,
Expand All @@ -76,34 +81,42 @@ export async function createTeamTraining(snapshot: QueryDocumentSnapshot, contex
console.log(`Calculated Start Date used: ${calculatedDate}`);
console.log(`Calculated End Date used: ${calculatedEndDate}`);

// Schleife für alle Trainings
do {
calculatedDate.setTime(calculatedDate.getTime() + offSet);

// Set EndDate
const calculatedEndDate = calculatedDate;
// Erstelle eine neue Kopie für das Enddatum
const calculatedEndDate = new Date(calculatedDate.getTime());
calculatedEndDate.setHours(new Date(trainingData.timeTo).getHours());
calculatedEndDate.setMinutes(new Date(trainingData.timeTo).getMinutes());
calculatedEndDate.setSeconds(0);
calculatedEndDate.setMilliseconds(0);

// Erstelle Training
const newTrainingRef = await db.collection("teams")
.doc(trainingData.teamId)
.collection("trainings")
.add({
...trainingData,
clubId: teamRef.data().clubId,
date: calculatedDate,
startDate: calculatedDate,
endDate: calculatedEndDate,
teamName: teamRef.data().name,
liga: teamRef.data().liga,
});

// Add Training Entry
const newTrainingRef = await db.collection("teams").doc(trainingData.teamId).collection("trainings").add({
...trainingData,
clubId: teamRef.data().clubId,
date: calculatedDate,
startDate: calculatedDate,
endDate: calculatedEndDate,
teamName: teamRef.data().name,
liga: teamRef.data().liga,
});
console.log("New Training: " + newTrainingRef.id + " " + calculatedDate.toISOString());
console.log(`Calculated Start Date used: ${calculatedDate}`);
console.log(`Calculated End Date used: ${calculatedEndDate}`);
} while (calculatedDate.getTime() <= new Date(trainingData.endDate).getTime());

// Berechne nächstes Datum
calculatedDate.setTime(calculatedDate.getTime() + offSet);
} while (calculatedDate.getTime() <= new Date(trainingData.endDate).getTime());

return db.collection("userProfile").doc(userId).collection("trainings").doc(trainingId).delete();
return db.collection("userProfile")
.doc(userId)
.collection("trainings")
.doc(trainingId)
.delete();
}

export async function createNotificationTeamTraining(snapshot: QueryDocumentSnapshot, context: functions.EventContext) {
Expand Down

0 comments on commit 44be68d

Please sign in to comment.