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