Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions pages/api/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const editEvent = async (eventId, eventStart, eventEnd, eventDesc, materials, sh
}

const eventData = eventDoc.data();
const existingShiftIds = eventData.shifts || [];
const existingShiftIds = new Set(eventData.shifts || []);
const existingMaterials = eventData.materials || [];

const materialsObj = existingMaterials.map(mat => ({ item: mat.item, user: mat.user }));
Expand All @@ -63,19 +63,21 @@ const editEvent = async (eventId, eventStart, eventEnd, eventDesc, materials, sh
});

const shiftIDS = [...existingShiftIds];
for (let i = 0; i < shifts.length; i++) {
const shift = shifts[i];
if (shift.id) {

for (const shift of shifts) {
if (shift.id && existingShiftIds.has(shift.id)) {
// Update the already existing shift
const shiftRef = doc(db, "shifts", shift.id);
await updateDoc(shiftRef, {
endTime: parseInt(shift.endTime),
startTime: parseInt(shift.startTime)
endTime: parseInt(shift.end),
startTime: parseInt(shift.start),
});
} else {
} else if (!shift.id) {
// Create a new shift only if it's not already in the set
const docRef = await addDoc(collection(db, "shifts"), {
endTime: parseInt(shift.endTime),
startTime: parseInt(shift.startTime),
user: []
endTime: parseInt(shift.end),
startTime: parseInt(shift.start),
user: [],
});
shiftIDS.push(docRef.id);
}
Expand All @@ -90,13 +92,13 @@ const editEvent = async (eventId, eventStart, eventEnd, eventDesc, materials, sh
shifts: shiftIDS,
title: title,
category: category,
notfis: notfis == undefined ? [] : notfis
notfis: notfis || [],
};

await getUserNotifTokens(existingShiftIds, title, eventId);
// Notify users about event update
await getUserNotifTokens([...existingShiftIds], title, eventId);

await updateDoc(eventRef, data);

} catch (error) {
console.error("Event Update Error:", error);
throw error;
Expand Down