diff --git a/src/backdrops/SelectConsultation/SelectConsultation.jsx b/src/backdrops/SelectConsultation/SelectConsultation.jsx index 009169f..78db582 100644 --- a/src/backdrops/SelectConsultation/SelectConsultation.jsx +++ b/src/backdrops/SelectConsultation/SelectConsultation.jsx @@ -69,7 +69,6 @@ export const SelectConsultation = ({ providerId, campaignId ); - if (campaignId) { return data.map((x) => ({ time: parseUTCDate(x.time), @@ -77,7 +76,34 @@ export const SelectConsultation = ({ })); } - return data; + const slots = data.map((x) => { + if (x.time) { + return { + time: parseUTCDate(x.time), + organization_id: x.organization_id, + }; + } + return x; + }); + + const organizationSlotTimes = slots.reduce((acc, slot) => { + if (slot.organization_id) { + acc.push(slot.time.getTime()); + } + return acc; + }, []); + + // Ensure that there is no overlap between organization slots and regular slots + // If there are duplicates, remove the regular slot + if (organizationSlotTimes.length > 0) { + return slots.filter((slot) => { + if (slot.time) return slot; + const slotTime = new Date(slot).getTime(); + return !organizationSlotTimes.includes(slotTime); + }); + } + + return slots; }; const availableSlotsQuery = useQuery( ["available-slots", startDate, currentDay, providerId], @@ -97,7 +123,7 @@ export const SelectConsultation = ({ const renderFreeSlots = () => { const todaySlots = availableSlots?.filter((slot) => { - const slotDate = new Date(campaignId ? slot.time : slot).getDate(); + const slotDate = new Date(slot.time || slot).getDate(); const currentDayDate = new Date(currentDay).getDate(); // Check if the slot is for the current campaign @@ -111,9 +137,9 @@ export const SelectConsultation = ({ const options = todaySlots?.map( (slot) => { - const slotLocal = new Date(campaignId ? slot.time : slot); + const slotLocal = new Date(slot.time || slot); - const value = new Date(campaignId ? slot.time : slot).getTime(); + const value = new Date(slot.time || slot).getTime(); const getDoubleDigitHour = (hour) => hour === 24 ? "00" : hour < 10 ? `0${hour}` : hour; @@ -150,8 +176,23 @@ export const SelectConsultation = ({ } return isTimeMatching && slot.campaign_id === campaignId; }); + } else { + const allMatchingSlots = availableSlots.filter((slot) => { + const isTimeMatching = new Date(slot.time).getTime() === selectedSlot; + return isTimeMatching; + }); + + if (allMatchingSlots.length >= 1) { + const hasOrganizationSlot = allMatchingSlots.find( + (slot) => !!slot.organization_id + ); + if (hasOrganizationSlot) { + slotObject = hasOrganizationSlot; + } + } } - const time = campaignId ? slotObject : selectedSlot; + const time = slotObject || selectedSlot; + handleBlockSlot(time, providerData.consultationPrice); };