From a522889ba056855f3e639f5484540a83d46cf609 Mon Sep 17 00:00:00 2001 From: 23nosurrend Date: Sat, 20 Jul 2024 23:46:42 +0200 Subject: [PATCH] ft(notifications):add logic to insert notifications on every booking --- .../ActionMenu/Booking/SelectPayment.tsx | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/app/(app)/ActionMenu/Booking/SelectPayment.tsx b/app/(app)/ActionMenu/Booking/SelectPayment.tsx index 3915adb..6d0f230 100644 --- a/app/(app)/ActionMenu/Booking/SelectPayment.tsx +++ b/app/(app)/ActionMenu/Booking/SelectPayment.tsx @@ -86,6 +86,40 @@ export default function SelectPayment() { console.log("Error while inserting data in booking ", error); } } + const addNotification = async ( doctorName: string) => { + try { + const { error } = await supabase + .from('notifications') + .insert({ + title: 'Appointment Booked', + description: `You have successfully booked an appointment with Dr. ${doctorName}`, + patient_id: patient_id, + type: "appointment_booked", + doctor_id:doctor_id + + }); +console.log("Notification will be pushed") + if (error) { + console.log("Error while inserting notification ", error); + } + } catch (error) { + console.log("Error while inserting notification ", error); + } + }; + const fetchDoctorName = async (doctorId: string) => { + const { data, error } = await supabase + .from('doctors') + .select('first_name') + .eq('id', doctorId) + .single(); + + if (error) { + console.log("Error fetching doctor's name: ", error); + return ""; + } + + return data.first_name; + }; function successBooking() { router.push("ActionMenu"); modal.hide(); @@ -178,10 +212,14 @@ export default function SelectPayment() { ), }); }; - const handleOnRedirect = (data: RedirectParams) => { - // console.log("redire data:", data) + const handleOnRedirect = async(data: RedirectParams) => { + if (data.status === "successful") { bookAppointment(); + if (typeof doctor_id === "string") { + const doctorName = await fetchDoctorName(doctor_id); + await addNotification(doctorName); + } showSuccefulModal(); } else { alert("Payment Failed or cancelled ,please try again");