actions.js
5 days ago
booking.js
5 days ago
cabinet.js
5 days ago
cart.js
5 days ago
catalog.js
5 days ago
coupon.js
5 days ago
customFields.js
5 days ago
events.js
5 days ago
facebookPixel.js
5 days ago
ivy.js
5 days ago
package.js
5 days ago
panel.js
5 days ago
public.js
5 days ago
renderActions.js
5 days ago
restore.js
5 days ago
slots.js
5 days ago
translation.js
5 days ago
user.js
5 days ago
ivy.js
98 lines
| 1 | import { parsePhoneNumberFromString } from 'libphonenumber-js' |
| 2 | import { settings } from '../../../plugins/settings' |
| 3 | |
| 4 | function getCountryIsoMutation(store) { |
| 5 | return store.getters['booking/getBookableType'] === 'event' |
| 6 | ? 'customerInfo/setCustomerCountryPhoneIso' |
| 7 | : 'booking/setCustomerCountryPhoneIso' |
| 8 | } |
| 9 | |
| 10 | function applyIvyPhoneValue(store, infoFormData, rawPhone) { |
| 11 | const value = rawPhone == null ? '' : String(rawPhone).trim() |
| 12 | |
| 13 | if (!value) { |
| 14 | return false |
| 15 | } |
| 16 | |
| 17 | const countryIsoMutation = getCountryIsoMutation(store) |
| 18 | let parsed = parsePhoneNumberFromString(value) |
| 19 | |
| 20 | if (!parsed?.isValid?.()) { |
| 21 | const defaultCountry = store.state.settings?.general?.phoneDefaultCountryCode |
| 22 | |
| 23 | if (defaultCountry && defaultCountry !== 'auto') { |
| 24 | parsed = parsePhoneNumberFromString(value, defaultCountry.toUpperCase()) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | if (parsed) { |
| 29 | if (parsed.country) { |
| 30 | store.commit(countryIsoMutation, parsed.country.toLowerCase()) |
| 31 | } |
| 32 | |
| 33 | // MazInputPhoneNumber expects international format for v-model. |
| 34 | infoFormData.value.phone = parsed.formatInternational() |
| 35 | return true |
| 36 | } |
| 37 | |
| 38 | infoFormData.value.phone = value |
| 39 | return true |
| 40 | } |
| 41 | |
| 42 | function useIvyMapping(store, shortcodeData, infoFormData) { |
| 43 | const formId = shortcodeData.value?.ivy?.formId |
| 44 | const ivyValues = shortcodeData.value?.ivy?.values |
| 45 | let phoneMapped = false |
| 46 | |
| 47 | if (formId && settings?.ivy && formId in settings.ivy) { |
| 48 | const formMapping = settings.ivy[formId] |
| 49 | |
| 50 | if (formMapping && ivyValues && typeof ivyValues === 'object') { |
| 51 | const keys = ['firstName', 'lastName', 'email', 'phone'] |
| 52 | |
| 53 | keys.forEach((key) => { |
| 54 | const fieldMapping = formMapping[key] |
| 55 | |
| 56 | if (key in formMapping && fieldMapping) { |
| 57 | const formKey = fieldMapping.type + '_' + fieldMapping.index |
| 58 | |
| 59 | if (!(formKey in ivyValues)) { |
| 60 | return |
| 61 | } |
| 62 | |
| 63 | const ivyValue = ivyValues[formKey] |
| 64 | const currentValue = infoFormData.value[key] |
| 65 | const hasIvyValue = ivyValue != null && String(ivyValue).trim() !== '' |
| 66 | const shouldMap = hasIvyValue && (key === 'phone' || !currentValue) |
| 67 | |
| 68 | if (!shouldMap) { |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | if (fieldMapping.type === 'name') { |
| 73 | const nameFieldKey = fieldMapping.key |
| 74 | const nameValues = ivyValues[formKey] |
| 75 | |
| 76 | if ( |
| 77 | nameFieldKey && |
| 78 | nameValues && |
| 79 | typeof nameValues === 'object' && |
| 80 | nameFieldKey in nameValues |
| 81 | ) { |
| 82 | infoFormData.value[key] = nameValues[nameFieldKey] |
| 83 | } |
| 84 | } else if (key === 'phone') { |
| 85 | phoneMapped = applyIvyPhoneValue(store, infoFormData, ivyValue) || phoneMapped |
| 86 | } else { |
| 87 | infoFormData.value[key] = ivyValue |
| 88 | } |
| 89 | } |
| 90 | }) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return phoneMapped |
| 95 | } |
| 96 | |
| 97 | export { useIvyMapping } |
| 98 |