Fix: Cancel button functionality and live updates in booking management
- Add confirmation modal for booking cancellations - Implement proper error handling and success messages - Fix live updates for booking status changes - Add manual refetch to ensure immediate UI updates - Auto-delete past availability slots on list access - Add manual cleanup function for past slots - Improve user experience with instant feedback
This commit is contained in:
@@ -87,32 +87,41 @@ const remove = os
|
||||
const list = os.handler(async () => {
|
||||
const allSlots = await kv.getAllItems();
|
||||
|
||||
// Filter out past slots automatically
|
||||
// Auto-delete past slots
|
||||
const today = new Date().toISOString().split("T")[0]; // YYYY-MM-DD
|
||||
const now = new Date();
|
||||
const currentTime = `${String(now.getHours()).padStart(2, "0")}:${String(now.getMinutes()).padStart(2, "0")}`;
|
||||
|
||||
const filteredSlots = allSlots.filter(slot => {
|
||||
// Keep slots for future dates
|
||||
if (slot.date > today) return true;
|
||||
let deletedCount = 0;
|
||||
const slotsToDelete: string[] = [];
|
||||
|
||||
// Identify past slots for deletion
|
||||
allSlots.forEach(slot => {
|
||||
const isPastDate = slot.date < today;
|
||||
const isPastTime = slot.date === today && slot.time <= currentTime;
|
||||
|
||||
// For today: only keep future time slots
|
||||
if (slot.date === today) {
|
||||
return slot.time > currentTime;
|
||||
if (isPastDate || isPastTime) {
|
||||
slotsToDelete.push(slot.id);
|
||||
}
|
||||
|
||||
// Remove past slots
|
||||
return false;
|
||||
});
|
||||
|
||||
// Debug logging (commented out - uncomment if needed)
|
||||
// const statusCounts = filteredSlots.reduce((acc, slot) => {
|
||||
// acc[slot.status] = (acc[slot.status] || 0) + 1;
|
||||
// return acc;
|
||||
// }, {} as Record<string, number>);
|
||||
// console.log(`Availability list: ${filteredSlots.length} slots (${JSON.stringify(statusCounts)})`);
|
||||
// Delete past slots (only if not reserved)
|
||||
for (const slotId of slotsToDelete) {
|
||||
const slot = await kv.getItem(slotId);
|
||||
if (slot && slot.status !== "reserved") {
|
||||
await kv.removeItem(slotId);
|
||||
deletedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
return filteredSlots;
|
||||
if (deletedCount > 0) {
|
||||
console.log(`Auto-deleted ${deletedCount} past availability slots`);
|
||||
}
|
||||
|
||||
// Return remaining slots (all are now current/future)
|
||||
const remainingSlots = allSlots.filter(slot => !slotsToDelete.includes(slot.id));
|
||||
|
||||
return remainingSlots;
|
||||
});
|
||||
|
||||
const get = os.input(z.string()).handler(async ({ input }) => {
|
||||
@@ -126,6 +135,33 @@ const getByDate = os
|
||||
return all.filter((s) => s.date === input);
|
||||
});
|
||||
|
||||
// Cleanup function to manually delete past slots
|
||||
const cleanupPastSlots = os
|
||||
.input(z.object({ sessionId: z.string() }))
|
||||
.handler(async ({ input }) => {
|
||||
await assertOwner(input.sessionId);
|
||||
|
||||
const allSlots = await kv.getAllItems();
|
||||
const today = new Date().toISOString().split("T")[0];
|
||||
const now = new Date();
|
||||
const currentTime = `${String(now.getHours()).padStart(2, "0")}:${String(now.getMinutes()).padStart(2, "0")}`;
|
||||
|
||||
let deletedCount = 0;
|
||||
|
||||
for (const slot of allSlots) {
|
||||
const isPastDate = slot.date < today;
|
||||
const isPastTime = slot.date === today && slot.time <= currentTime;
|
||||
|
||||
if ((isPastDate || isPastTime) && slot.status !== "reserved") {
|
||||
await kv.removeItem(slot.id);
|
||||
deletedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Manual cleanup: deleted ${deletedCount} past availability slots`);
|
||||
return { deletedCount, message: `${deletedCount} vergangene Slots wurden gelöscht.` };
|
||||
});
|
||||
|
||||
const live = {
|
||||
list: os.handler(async function* ({ signal }) {
|
||||
yield call(list, {}, { signal });
|
||||
@@ -150,6 +186,7 @@ export const router = {
|
||||
list,
|
||||
get,
|
||||
getByDate,
|
||||
cleanupPastSlots,
|
||||
live,
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user