Cal("on", { action: "bookingSuccessful", callback: (e) => { let flag = true; const userRef = window.dbRef( window.db, `users/${window.auth.currentUser.uid}` ); window.dbOnValue(userRef, (userSnapshot) => { if (flag) { flag = false; const { payments } = userSnapshot.val(); const servicePrice = $("#current-mentor-prices") .data("service-price") .toString(); const userBookingsRef = window.dbRef( window.db, `users/${window.auth.currentUser.uid}/bookings` ); const newBookingRef = window.dbPush(userBookingsRef); const params = new Proxy(new URLSearchParams(window.location.search), { get: (searchParams, prop) => searchParams.get(prop), }); const orderId = params.id; const payment = Object.values(payments || {}).find( (payment) => payment.orderId === orderId ); const mentorName = payment.mentorName const numberOfService = payment.serviceNumber ?? payment.redirectUrl[payment.redirectUrl.length - 1]; const serviceNumber = `service=${numberOfService}`; const date = new Date().toLocaleString("de-DE", { timeZone: "Europe/Kiev", }); const pathArr = window.location.pathname.split("/"); const serviceType = pathArr[2]; const isCharity = $('[data-container="video-answer-charitable"]').data( "charitable" ); const price = $("#current-mentor-prices").data("service-price"); const calUserId = e.detail.data.booking.uid; window .dbSet(newBookingRef, { mentorName, serviceType, orderId, createdAt: date, amount: servicePrice, serviceNumber, calUserId, }) .then(() => { (mentorName !== "test-mentor" || mentorName !== "test-mentor-no2") && dataLayer.push({ mentor_name: mentorName, payment_option: isCharity ? "сharity" : price === 0 ? "free" : "payment_to_the_mentor", event: "booked_video_consult", }); }) .then(async () => { const formData = new FormData(); formData.append( "timestamp", new Date().toLocaleString("de-DE", { timeZone: "Europe/Kiev" }) ); formData.append("mentor", mentorName); formData.append( "bookingTime", new Date(e?.detail?.data?.date).toLocaleString("de-DE", { timeZone: "Europe/Kiev", }) ); formData.append( "clientsName", e?.detail?.data?.booking?.responses?.name || `${window.auth?.currentUser?.displayName} - from FIREBASE` ); formData.append( "clientsEmail", e?.detail?.data?.booking?.responses?.email || `${window.auth?.currentUser?.email} - from FIREBASE` ); formData.append( "request", e?.detail?.data?.booking?.responses?.notes || `UNKNOWN REQUEST` ); formData.append( "contact", e?.detail?.data?.booking?.responses?.Contact || `UNKNOWN CONTACT` ); formData.append( "linkedIn", e?.detail?.data?.booking?.responses?.LinkedIn ); formData.append( "links", e?.detail?.data?.booking?.responses?.Links ); await $.ajax({ type: "POST", url: "https://script.google.com/macros/s/AKfycbzBA5CdUFxdTlaYTd6oIvlIWkDK-ACRODuIC5jTnp5Jh1EAhh4Nxzu7uYBefceprnwD/exec", data: formData, processData: false, contentType: false, }); window.location.href = `${window.location.origin}/booking/successful-calendar-booking?name=${mentorName}`; }) .catch((e) => { console.log(e); }); } }); }, });