appointments.js
5 days ago
attendees.js
5 days ago
colorManipulation.js
5 days ago
customer.js
5 days ago
date.js
5 days ago
defaultCustomize.js
5 days ago
employee.js
5 days ago
events.js
5 days ago
formFieldsTemplates.js
5 days ago
formatting.js
5 days ago
helper.js
5 days ago
image.js
5 days ago
integrationApple.js
5 days ago
integrationGoogle.js
5 days ago
integrationOutlook.js
5 days ago
integrationStripe.js
5 days ago
integrationZoom.js
5 days ago
licence.js
5 days ago
phone.js
5 days ago
pricing.js
5 days ago
recurring.js
5 days ago
responsive.js
5 days ago
scrollElements.js
5 days ago
settings.js
5 days ago
translationsElementPlus.js
5 days ago
utilHeaderHeight.js
5 days ago
recurring.js
262 lines
| 1 | import moment from 'moment' |
| 2 | import { useCartItem } from '../public/cart' |
| 3 | import { useSortedDateStrings } from './helper' |
| 4 | |
| 5 | function getFutureMonthDate(dateString, interval) { |
| 6 | let date = moment(dateString, 'YYYY-MM-DD') |
| 7 | let futureMonthDate = moment(date).add(interval, 'M') |
| 8 | let futureMonthDateEnd = moment(futureMonthDate).startOf('month') |
| 9 | |
| 10 | if ( |
| 11 | date.date() !== futureMonthDate.date() && |
| 12 | futureMonthDate.isSame(futureMonthDateEnd.format('YYYY-MM-DD')) |
| 13 | ) { |
| 14 | futureMonthDate.subtract(1, 'd') |
| 15 | } |
| 16 | |
| 17 | return futureMonthDate |
| 18 | } |
| 19 | |
| 20 | function getGivenDateOfMonth(date, monthWeek, weekDay) { |
| 21 | let firstWeek = moment(date, 'YYYY-MM-DD').startOf('month').day(weekDay) |
| 22 | |
| 23 | if (firstWeek.format('M') !== date.format('M')) { |
| 24 | firstWeek.add(7, 'days') |
| 25 | } |
| 26 | |
| 27 | let resultDate = firstWeek.add(7 * (monthWeek - 1), 'days') |
| 28 | |
| 29 | if (date.format('M') !== resultDate.format('M')) { |
| 30 | resultDate.subtract(7, 'days') |
| 31 | } |
| 32 | |
| 33 | return resultDate |
| 34 | } |
| 35 | |
| 36 | function useExpectedDates( |
| 37 | lastAvailableDate, |
| 38 | startDate, |
| 39 | endDate, |
| 40 | maxDays, |
| 41 | type, |
| 42 | interval, |
| 43 | weekDays, |
| 44 | monthRule, |
| 45 | ) { |
| 46 | let lastDate = endDate ? moment(endDate) : moment().add(maxDays, 'days') |
| 47 | |
| 48 | let selectedDate = moment(startDate, 'YYYY-MM-DD') |
| 49 | |
| 50 | let expectedDates = {} |
| 51 | |
| 52 | let expectedDatesArray = [] |
| 53 | |
| 54 | switch (type) { |
| 55 | case 'daily': |
| 56 | expectedDatesArray.push(moment(startDate, 'YYYY-MM-DD')) |
| 57 | |
| 58 | break |
| 59 | |
| 60 | case 'weekly': |
| 61 | let selectedDateIndex = moment(startDate, 'YYYY-MM-DD').isoWeekday() |
| 62 | |
| 63 | weekDays.forEach((item, index) => { |
| 64 | if (item.selected) { |
| 65 | let dayIndex = index + 1 |
| 66 | |
| 67 | let date = moment(startDate, 'YYYY-MM-DD') |
| 68 | |
| 69 | if (selectedDateIndex < dayIndex) { |
| 70 | expectedDatesArray.push(date.add(dayIndex - selectedDateIndex, 'days')) |
| 71 | } else if (selectedDateIndex === dayIndex) { |
| 72 | expectedDatesArray.push(date) |
| 73 | } else if (selectedDateIndex > dayIndex) { |
| 74 | expectedDatesArray.push(date.subtract(selectedDateIndex - dayIndex, 'days')) |
| 75 | } |
| 76 | |
| 77 | let lDate = lastDate ? lastDate : lastAvailableDate |
| 78 | if (date.isSameOrBefore(lDate) && date.isAfter(selectedDate)) { |
| 79 | expectedDates[date.format('YYYY-MM-DD')] = date |
| 80 | } |
| 81 | } |
| 82 | }) |
| 83 | |
| 84 | break |
| 85 | |
| 86 | case 'monthly': |
| 87 | expectedDatesArray.push(moment(startDate, 'YYYY-MM-DD')) |
| 88 | |
| 89 | break |
| 90 | } |
| 91 | |
| 92 | let isValidExpected = expectedDatesArray.length > 0 |
| 93 | |
| 94 | while (isValidExpected) { |
| 95 | for (let i = 0; i < expectedDatesArray.length; i++) { |
| 96 | switch (type) { |
| 97 | case 'daily': |
| 98 | expectedDatesArray[i].add(interval, 'days') |
| 99 | |
| 100 | break |
| 101 | |
| 102 | case 'weekly': |
| 103 | expectedDatesArray[i].add(interval * 7, 'days') |
| 104 | |
| 105 | break |
| 106 | |
| 107 | case 'monthly': |
| 108 | let weekDayIndex = expectedDatesArray[i].isoWeekday() |
| 109 | expectedDatesArray[i] = |
| 110 | monthRule === 0 |
| 111 | ? getFutureMonthDate(expectedDatesArray[i].format('YYYY-MM-DD'), interval) |
| 112 | : getGivenDateOfMonth( |
| 113 | getFutureMonthDate( |
| 114 | expectedDatesArray[i].startOf('month').format('YYYY-MM-DD'), |
| 115 | interval, |
| 116 | ), |
| 117 | monthRule, |
| 118 | weekDayIndex, |
| 119 | ) |
| 120 | |
| 121 | break |
| 122 | } |
| 123 | |
| 124 | isValidExpected = expectedDatesArray[i].isSameOrBefore(lastDate) |
| 125 | |
| 126 | if (isValidExpected) { |
| 127 | expectedDates[moment(expectedDatesArray[i]).format('YYYY-MM-DD')] = moment( |
| 128 | expectedDatesArray[i], |
| 129 | ) |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return expectedDates |
| 135 | } |
| 136 | |
| 137 | function useProposedDates(service, slots, expectedDates, startDate, startTime, endDate, count) { |
| 138 | let recurringStartDate = moment(startDate, 'YYYY-MM-DD') |
| 139 | let recurringEndDate = endDate ? moment(endDate, 'YYYY-MM-DD') : null |
| 140 | |
| 141 | let proposedDates = {} |
| 142 | let availableDates = {} |
| 143 | |
| 144 | useSortedDateStrings(Object.keys(slots)).forEach((dateString) => { |
| 145 | availableDates[dateString] = moment(dateString + ' 00:00:00') |
| 146 | }) |
| 147 | |
| 148 | let bookingStartString = moment(startDate, 'YYYY-MM-DD').format('YYYY-MM-DD') |
| 149 | |
| 150 | for (let expectedDateString in expectedDates) { |
| 151 | let expectedDate = moment(expectedDateString, 'YYYY-MM-DD') |
| 152 | |
| 153 | let isBefore = recurringEndDate ? expectedDate.isSameOrBefore(recurringEndDate) : true |
| 154 | |
| 155 | if ( |
| 156 | expectedDateString in slots && |
| 157 | Object.keys(slots[expectedDateString]).length && |
| 158 | !(expectedDateString in proposedDates) && |
| 159 | isBefore |
| 160 | ) { |
| 161 | let isAvailableTime = startTime in slots[expectedDateString] |
| 162 | |
| 163 | let expectedTime = isAvailableTime ? startTime : Object.keys(slots[expectedDateString])[0] |
| 164 | |
| 165 | proposedDates[expectedDateString] = { |
| 166 | isSubstituteDate: false, |
| 167 | isSubstituteTime: !isAvailableTime, |
| 168 | time: expectedTime, |
| 169 | times: Object.keys(slots[expectedDateString]), |
| 170 | slot: slots[expectedDateString][expectedTime], |
| 171 | } |
| 172 | } else if ( |
| 173 | expectedDateString !== bookingStartString && |
| 174 | !(expectedDateString in proposedDates) && |
| 175 | isBefore |
| 176 | ) { |
| 177 | let previousDate = null |
| 178 | let nextDate = null |
| 179 | |
| 180 | for (let availableDateString in availableDates) { |
| 181 | if ( |
| 182 | (service.recurringSub === 'past' || service.recurringSub === 'both') && |
| 183 | availableDates[availableDateString].isBefore(expectedDates[expectedDateString]) && |
| 184 | availableDates[availableDateString].isAfter(recurringStartDate) && |
| 185 | availableDateString !== expectedDateString && |
| 186 | !(availableDateString in proposedDates) |
| 187 | ) { |
| 188 | previousDate = availableDates[availableDateString] |
| 189 | } |
| 190 | |
| 191 | if ( |
| 192 | (service.recurringSub === 'future' || service.recurringSub === 'both') && |
| 193 | availableDates[availableDateString].isAfter(expectedDates[expectedDateString]) && |
| 194 | availableDateString !== expectedDateString && |
| 195 | !(availableDateString in proposedDates) |
| 196 | ) { |
| 197 | nextDate = availableDates[availableDateString] |
| 198 | |
| 199 | break |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | let previousDateTimeDiff = |
| 204 | previousDate !== null |
| 205 | ? moment(expectedDates[expectedDateString]).diff(previousDate, 'days') |
| 206 | : null |
| 207 | |
| 208 | let nextDateTimeDiff = |
| 209 | nextDate !== null ? moment(nextDate).diff(expectedDates[expectedDateString], 'days') : null |
| 210 | |
| 211 | let substituteDate = null |
| 212 | |
| 213 | if (previousDateTimeDiff && nextDateTimeDiff) { |
| 214 | substituteDate = previousDateTimeDiff < nextDateTimeDiff ? previousDate : nextDate |
| 215 | } else if (previousDateTimeDiff) { |
| 216 | substituteDate = previousDate |
| 217 | } else if (nextDateTimeDiff) { |
| 218 | substituteDate = nextDate |
| 219 | } |
| 220 | |
| 221 | if ( |
| 222 | substituteDate !== null && |
| 223 | (recurringEndDate ? moment(substituteDate).isSameOrBefore(recurringEndDate) : true) |
| 224 | ) { |
| 225 | let substituteDateString = moment(substituteDate).format('YYYY-MM-DD') |
| 226 | |
| 227 | if ( |
| 228 | substituteDateString in slots && |
| 229 | !(expectedDateString in proposedDates) && |
| 230 | !(substituteDateString in expectedDates) |
| 231 | ) { |
| 232 | let isAvailableTime = startTime in slots[substituteDateString] |
| 233 | |
| 234 | let substituteTime = isAvailableTime |
| 235 | ? startTime |
| 236 | : Object.keys(slots[substituteDateString])[0] |
| 237 | |
| 238 | proposedDates[substituteDateString] = { |
| 239 | isSubstituteDate: true, |
| 240 | isSubstituteTime: !isAvailableTime, |
| 241 | time: substituteTime, |
| 242 | times: Object.keys(slots[substituteDateString]), |
| 243 | slot: slots[substituteDateString][substituteTime], |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | if ( |
| 250 | recurringEndDate |
| 251 | ? !isBefore |
| 252 | : useSortedDateStrings(Object.keys(proposedDates)).length >= count |
| 253 | ) { |
| 254 | break |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return proposedDates |
| 259 | } |
| 260 | |
| 261 | export { useProposedDates, useExpectedDates } |
| 262 |