|
| 1 | +import { BookingStatus, SchedulingType } from "@calcom/prisma/enums"; |
| 2 | +import type { ActionType } from "@calcom/ui/components/table"; |
| 3 | + |
| 4 | +import type { BookingItemProps } from "./BookingListItem"; |
| 5 | + |
| 6 | +export interface BookingActionContext { |
| 7 | + booking: BookingItemProps; |
| 8 | + isUpcoming: boolean; |
| 9 | + isOngoing: boolean; |
| 10 | + isBookingInPast: boolean; |
| 11 | + isCancelled: boolean; |
| 12 | + isConfirmed: boolean; |
| 13 | + isRejected: boolean; |
| 14 | + isPending: boolean; |
| 15 | + isRescheduled: boolean; |
| 16 | + isRecurring: boolean; |
| 17 | + isTabRecurring: boolean; |
| 18 | + isTabUnconfirmed: boolean; |
| 19 | + isBookingFromRoutingForm: boolean; |
| 20 | + isDisabledCancelling: boolean; |
| 21 | + isDisabledRescheduling: boolean; |
| 22 | + isCalVideoLocation: boolean; |
| 23 | + showPendingPayment: boolean; |
| 24 | + cardCharged: boolean; |
| 25 | + attendeeList: Array<{ |
| 26 | + name: string; |
| 27 | + email: string; |
| 28 | + id: number; |
| 29 | + noShow: boolean; |
| 30 | + phoneNumber: string | null; |
| 31 | + }>; |
| 32 | + getSeatReferenceUid: () => string | undefined; |
| 33 | + t: (key: string) => string; |
| 34 | +} |
| 35 | + |
| 36 | +export function getPendingActions(context: BookingActionContext): ActionType[] { |
| 37 | + const { booking, isPending, isTabRecurring, isTabUnconfirmed, isRecurring, showPendingPayment, t } = |
| 38 | + context; |
| 39 | + |
| 40 | + const actions: ActionType[] = [ |
| 41 | + { |
| 42 | + id: "reject", |
| 43 | + label: (isTabRecurring || isTabUnconfirmed) && isRecurring ? t("reject_all") : t("reject"), |
| 44 | + icon: "ban", |
| 45 | + disabled: false, // This would be controlled by mutation state in the component |
| 46 | + }, |
| 47 | + ]; |
| 48 | + |
| 49 | + // For bookings with payment, only confirm if the booking is paid for |
| 50 | + // Original logic: (isPending && !paymentAppData.enabled) || (paymentAppData.enabled && !!paymentAppData.price && booking.paid) |
| 51 | + if ((isPending && !showPendingPayment) || (showPendingPayment && booking.paid)) { |
| 52 | + actions.push({ |
| 53 | + id: "confirm", |
| 54 | + bookingId: booking.id, |
| 55 | + label: (isTabRecurring || isTabUnconfirmed) && isRecurring ? t("confirm_all") : t("confirm"), |
| 56 | + icon: "check" as const, |
| 57 | + disabled: false, // This would be controlled by mutation state in the component |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + return actions; |
| 62 | +} |
| 63 | + |
| 64 | +export function getCancelEventAction(context: BookingActionContext): ActionType { |
| 65 | + const { booking, isTabRecurring, isRecurring, getSeatReferenceUid, t } = context; |
| 66 | + |
| 67 | + return { |
| 68 | + id: "cancel", |
| 69 | + label: isTabRecurring && isRecurring ? t("cancel_all_remaining") : t("cancel_event"), |
| 70 | + href: `/booking/${booking.uid}?cancel=true${ |
| 71 | + isTabRecurring && isRecurring ? "&allRemainingBookings=true" : "" |
| 72 | + }${booking.seatsReferences.length ? `&seatReferenceUid=${getSeatReferenceUid()}` : ""}`, |
| 73 | + icon: "circle-x", |
| 74 | + color: "destructive", |
| 75 | + disabled: isActionDisabled("cancel", context), |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +export function getVideoOptionsActions(context: BookingActionContext): ActionType[] { |
| 80 | + const { booking, isBookingInPast, isConfirmed, isCalVideoLocation, t } = context; |
| 81 | + |
| 82 | + return [ |
| 83 | + { |
| 84 | + id: "view_recordings", |
| 85 | + label: t("view_recordings"), |
| 86 | + icon: "video", |
| 87 | + disabled: !(isBookingInPast && isConfirmed && isCalVideoLocation && booking.isRecorded), |
| 88 | + }, |
| 89 | + { |
| 90 | + id: "meeting_session_details", |
| 91 | + label: t("view_session_details"), |
| 92 | + icon: "info", |
| 93 | + disabled: !(isBookingInPast && isConfirmed && isCalVideoLocation), |
| 94 | + }, |
| 95 | + ]; |
| 96 | +} |
| 97 | + |
| 98 | +export function getEditEventActions(context: BookingActionContext): ActionType[] { |
| 99 | + const { |
| 100 | + booking, |
| 101 | + isBookingInPast, |
| 102 | + isDisabledRescheduling, |
| 103 | + isBookingFromRoutingForm, |
| 104 | + getSeatReferenceUid, |
| 105 | + t, |
| 106 | + } = context; |
| 107 | + |
| 108 | + const actions: (ActionType | null)[] = [ |
| 109 | + { |
| 110 | + id: "reschedule", |
| 111 | + icon: "clock", |
| 112 | + label: t("reschedule_booking"), |
| 113 | + href: `/reschedule/${booking.uid}${ |
| 114 | + booking.seatsReferences.length ? `?seatReferenceUid=${getSeatReferenceUid()}` : "" |
| 115 | + }`, |
| 116 | + disabled: |
| 117 | + (isBookingInPast && !booking.eventType.allowReschedulingPastBookings) || isDisabledRescheduling, |
| 118 | + }, |
| 119 | + { |
| 120 | + id: "reschedule_request", |
| 121 | + icon: "send", |
| 122 | + iconClassName: "rotate-45 w-[16px] -translate-x-0.5 ", |
| 123 | + label: t("send_reschedule_request"), |
| 124 | + disabled: |
| 125 | + (isBookingInPast && !booking.eventType.allowReschedulingPastBookings) || isDisabledRescheduling, |
| 126 | + }, |
| 127 | + isBookingFromRoutingForm |
| 128 | + ? { |
| 129 | + id: "reroute", |
| 130 | + label: t("reroute"), |
| 131 | + icon: "waypoints", |
| 132 | + disabled: false, |
| 133 | + } |
| 134 | + : null, |
| 135 | + { |
| 136 | + id: "change_location", |
| 137 | + label: t("edit_location"), |
| 138 | + icon: "map-pin", |
| 139 | + disabled: false, |
| 140 | + }, |
| 141 | + booking.eventType?.disableGuests |
| 142 | + ? null |
| 143 | + : { |
| 144 | + id: "add_members", |
| 145 | + label: t("additional_guests"), |
| 146 | + icon: "user-plus", |
| 147 | + disabled: false, |
| 148 | + }, |
| 149 | + // Reassign (if round robin) |
| 150 | + booking.eventType.schedulingType === SchedulingType.ROUND_ROBIN |
| 151 | + ? { |
| 152 | + id: "reassign", |
| 153 | + label: t("reassign"), |
| 154 | + icon: "users", |
| 155 | + disabled: false, |
| 156 | + } |
| 157 | + : null, |
| 158 | + ]; |
| 159 | + |
| 160 | + return actions.filter(Boolean) as ActionType[]; |
| 161 | +} |
| 162 | + |
| 163 | +export function getAfterEventActions(context: BookingActionContext): ActionType[] { |
| 164 | + const { booking, cardCharged, attendeeList, t } = context; |
| 165 | + |
| 166 | + const actions: (ActionType | null)[] = [ |
| 167 | + ...getVideoOptionsActions(context), |
| 168 | + booking.status === BookingStatus.ACCEPTED && booking.paid && booking.payment[0]?.paymentOption === "HOLD" |
| 169 | + ? { |
| 170 | + id: "charge_card", |
| 171 | + label: cardCharged ? t("no_show_fee_charged") : t("collect_no_show_fee"), |
| 172 | + icon: "credit-card", |
| 173 | + disabled: cardCharged, |
| 174 | + } |
| 175 | + : null, |
| 176 | + { |
| 177 | + id: "no_show", |
| 178 | + label: |
| 179 | + attendeeList.length === 1 && attendeeList[0].noShow ? t("unmark_as_no_show") : t("mark_as_no_show"), |
| 180 | + icon: attendeeList.length === 1 && attendeeList[0].noShow ? "eye" : "eye-off", |
| 181 | + disabled: false, // This would be controlled by booking state in the component |
| 182 | + }, |
| 183 | + ]; |
| 184 | + |
| 185 | + return actions.filter(Boolean) as ActionType[]; |
| 186 | +} |
| 187 | + |
| 188 | +export function shouldShowPendingActions(context: BookingActionContext): boolean { |
| 189 | + const { isPending, isUpcoming, isCancelled } = context; |
| 190 | + return isPending && isUpcoming && !isCancelled; |
| 191 | +} |
| 192 | + |
| 193 | +export function shouldShowEditActions(context: BookingActionContext): boolean { |
| 194 | + const { isPending, isTabRecurring, isRecurring, isCancelled } = context; |
| 195 | + return !isPending && !(isTabRecurring && isRecurring) && !isCancelled; |
| 196 | +} |
| 197 | + |
| 198 | +export function shouldShowRecurringCancelAction(context: BookingActionContext): boolean { |
| 199 | + const { isTabRecurring, isRecurring } = context; |
| 200 | + return isTabRecurring && isRecurring; |
| 201 | +} |
| 202 | + |
| 203 | +export function isActionDisabled(actionId: string, context: BookingActionContext): boolean { |
| 204 | + const { booking, isBookingInPast, isDisabledRescheduling, isDisabledCancelling, isPending, isConfirmed } = |
| 205 | + context; |
| 206 | + |
| 207 | + switch (actionId) { |
| 208 | + case "reschedule": |
| 209 | + case "reschedule_request": |
| 210 | + return (isBookingInPast && !booking.eventType.allowReschedulingPastBookings) || isDisabledRescheduling; |
| 211 | + case "cancel": |
| 212 | + return isDisabledCancelling || (isBookingInPast && isPending && !isConfirmed); |
| 213 | + case "view_recordings": |
| 214 | + return !(isBookingInPast && booking.status === BookingStatus.ACCEPTED && context.isCalVideoLocation); |
| 215 | + case "meeting_session_details": |
| 216 | + return !(isBookingInPast && booking.status === BookingStatus.ACCEPTED && context.isCalVideoLocation); |
| 217 | + case "charge_card": |
| 218 | + return context.cardCharged; |
| 219 | + default: |
| 220 | + return false; |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +export function getActionLabel(actionId: string, context: BookingActionContext): string { |
| 225 | + const { booking, isTabRecurring, isRecurring, attendeeList, cardCharged, t } = context; |
| 226 | + |
| 227 | + switch (actionId) { |
| 228 | + case "reject": |
| 229 | + return (isTabRecurring || context.isTabUnconfirmed) && isRecurring ? t("reject_all") : t("reject"); |
| 230 | + case "confirm": |
| 231 | + return (isTabRecurring || context.isTabUnconfirmed) && isRecurring ? t("confirm_all") : t("confirm"); |
| 232 | + case "cancel": |
| 233 | + return isTabRecurring && isRecurring ? t("cancel_all_remaining") : t("cancel_event"); |
| 234 | + case "no_show": |
| 235 | + return attendeeList.length === 1 && attendeeList[0].noShow |
| 236 | + ? t("unmark_as_no_show") |
| 237 | + : t("mark_as_no_show"); |
| 238 | + case "charge_card": |
| 239 | + return cardCharged ? t("no_show_fee_charged") : t("collect_no_show_fee"); |
| 240 | + default: |
| 241 | + return t(actionId); |
| 242 | + } |
| 243 | +} |
0 commit comments