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
integrationGoogle.js
165 lines
| 1 | import httpClient from '../../../plugins/axios' |
| 2 | import { useRemoveUrlParameter, useUrlQueryParams } from './helper' |
| 3 | |
| 4 | function useGoogleSync(code, successCallback) { |
| 5 | let redirectURL = useRemoveUrlParameter( |
| 6 | useRemoveUrlParameter( |
| 7 | useRemoveUrlParameter(useRemoveUrlParameter(window.location.href, 'code'), 'state'), |
| 8 | 'scope', |
| 9 | ), |
| 10 | 'iss', |
| 11 | ) |
| 12 | |
| 13 | httpClient |
| 14 | .post('/google/authorization/token', { |
| 15 | authCode: code, |
| 16 | userId: useUrlQueryParams(window.location.href)['state'], |
| 17 | redirectUri: redirectURL, |
| 18 | }) |
| 19 | .then(() => { |
| 20 | history.pushState(null, null, redirectURL) |
| 21 | }) |
| 22 | .catch((error) => { |
| 23 | console.log(error) |
| 24 | }) |
| 25 | .finally(() => { |
| 26 | successCallback() |
| 27 | }) |
| 28 | } |
| 29 | |
| 30 | function useGoogleConnect(store) { |
| 31 | const amSettings = store.getters['getSettings'] |
| 32 | if (!store.getters['auth/getGoogleLoading']) { |
| 33 | store.commit('auth/setGoogleLoading', true) |
| 34 | |
| 35 | let cleanUrl = useRemoveUrlParameter(window.location.href, 'iss') |
| 36 | |
| 37 | if (!amSettings.googleCalendar.accessToken) { |
| 38 | httpClient |
| 39 | .get('/google/authorization/url/' + store.getters['employee/getId'], { |
| 40 | redirectUri: cleanUrl.split('?')[0], |
| 41 | }) |
| 42 | .then((response) => { |
| 43 | window.location.href = response.data.data.authUrl.replace( |
| 44 | /redirect_uri=.+?&/, |
| 45 | 'redirect_uri=' + cleanUrl + '&', |
| 46 | ) |
| 47 | }) |
| 48 | .catch((error) => { |
| 49 | console.log(error) |
| 50 | |
| 51 | store.commit('auth/setGoogleLoading', false) |
| 52 | }) |
| 53 | } |
| 54 | |
| 55 | if (amSettings.googleCalendar.accessToken) { |
| 56 | httpClient |
| 57 | .get('/google-calendar/authorization/url/' + store.getters['employee/getId'], { |
| 58 | params: { |
| 59 | redirectUri: cleanUrl, |
| 60 | }, |
| 61 | }) |
| 62 | .then((response) => { |
| 63 | window.location.href = response.data.data.authUrl |
| 64 | }) |
| 65 | .catch((error) => { |
| 66 | console.log(error) |
| 67 | |
| 68 | store.commit('auth/setGoogleLoading', false) |
| 69 | }) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | async function useGoogleDisconnect(store, accountId = null) { |
| 75 | store.commit('auth/setGoogleLoading', true) |
| 76 | |
| 77 | const endpoint = '/google/disconnect/' + store.getters['employee/getId'] |
| 78 | const data = accountId ? { accountId: accountId } : {} |
| 79 | |
| 80 | await httpClient |
| 81 | .post(endpoint, data) |
| 82 | .then(async (response) => { |
| 83 | if (accountId) { |
| 84 | const accounts = store.getters['employee/getGoogleCalendarAccounts'] || [] |
| 85 | |
| 86 | const disconnectedAccount = accounts.find((a) => a.id === accountId) |
| 87 | |
| 88 | const updatedAccounts = accounts.filter((account) => account.id !== accountId) |
| 89 | store.commit('employee/setGoogleCalendarAccounts', updatedAccounts) |
| 90 | |
| 91 | if (updatedAccounts.length === 0) { |
| 92 | store.commit('employee/setGoogleId', null) |
| 93 | store.commit('employee/setGoogleCalendarId', '') |
| 94 | store.commit('employee/setGoogleToken', null) |
| 95 | store.commit('employee/setGoogleCalendarBlockedCalendars', []) |
| 96 | store.commit('auth/setGoogleCalendars', []) |
| 97 | } else { |
| 98 | const remainingCalendarIds = new Set() |
| 99 | updatedAccounts.forEach((account) => { |
| 100 | if (account.calendarList && account.calendarList.length) { |
| 101 | account.calendarList.forEach((cal) => remainingCalendarIds.add(String(cal.id))) |
| 102 | } |
| 103 | if (account.calendarId) { |
| 104 | remainingCalendarIds.add(String(account.calendarId)) |
| 105 | } |
| 106 | }) |
| 107 | |
| 108 | const removedCalendarIds = new Set() |
| 109 | if (disconnectedAccount) { |
| 110 | if (disconnectedAccount.calendarId) { |
| 111 | removedCalendarIds.add(String(disconnectedAccount.calendarId)) |
| 112 | } |
| 113 | if (disconnectedAccount.calendarList && disconnectedAccount.calendarList.length) { |
| 114 | disconnectedAccount.calendarList.forEach((cal) => |
| 115 | removedCalendarIds.add(String(cal.id)), |
| 116 | ) |
| 117 | } |
| 118 | if ( |
| 119 | disconnectedAccount.blockedCalendars && |
| 120 | disconnectedAccount.blockedCalendars.length |
| 121 | ) { |
| 122 | disconnectedAccount.blockedCalendars.forEach((id) => |
| 123 | removedCalendarIds.add(String(id)), |
| 124 | ) |
| 125 | } |
| 126 | } |
| 127 | if ( |
| 128 | response && |
| 129 | response.data && |
| 130 | response.data.data && |
| 131 | response.data.data.removedCalendarIds |
| 132 | ) { |
| 133 | response.data.data.removedCalendarIds.forEach((id) => |
| 134 | removedCalendarIds.add(String(id)), |
| 135 | ) |
| 136 | } |
| 137 | |
| 138 | const currentCalendarId = store.getters['employee/getGoogleCalendarId'] |
| 139 | if (currentCalendarId && !remainingCalendarIds.has(String(currentCalendarId))) { |
| 140 | store.commit('employee/setGoogleCalendarId', '') |
| 141 | } |
| 142 | |
| 143 | const blocked = store.getters['employee/getGoogleCalendarBlockedCalendars'] || [] |
| 144 | const cleanedBlocked = blocked.filter((id) => remainingCalendarIds.has(String(id))) |
| 145 | store.commit('employee/setGoogleCalendarBlockedCalendars', cleanedBlocked) |
| 146 | } |
| 147 | } else { |
| 148 | store.commit('employee/setGoogleId', null) |
| 149 | store.commit('employee/setGoogleCalendarId', '') |
| 150 | store.commit('employee/setGoogleToken', null) |
| 151 | store.commit('employee/setGoogleCalendarAccounts', []) |
| 152 | store.commit('employee/setGoogleCalendarBlockedCalendars', []) |
| 153 | store.commit('auth/setGoogleCalendars', []) |
| 154 | } |
| 155 | }) |
| 156 | .catch((error) => { |
| 157 | console.log(error) |
| 158 | }) |
| 159 | .finally(() => { |
| 160 | store.commit('auth/setGoogleLoading', false) |
| 161 | }) |
| 162 | } |
| 163 | |
| 164 | export { useGoogleSync, useGoogleConnect, useGoogleDisconnect } |
| 165 |