appointments.js
6 days ago
attendees.js
6 days ago
colorManipulation.js
6 days ago
customer.js
6 days ago
date.js
6 days ago
defaultCustomize.js
6 days ago
employee.js
6 days ago
events.js
6 days ago
formFieldsTemplates.js
6 days ago
formatting.js
6 days ago
helper.js
6 days ago
image.js
6 days ago
integrationApple.js
6 days ago
integrationGoogle.js
6 days ago
integrationOutlook.js
6 days ago
integrationStripe.js
6 days ago
integrationZoom.js
6 days ago
licence.js
6 days ago
phone.js
6 days ago
pricing.js
6 days ago
recurring.js
6 days ago
responsive.js
6 days ago
scrollElements.js
6 days ago
settings.js
6 days ago
translationsElementPlus.js
6 days ago
utilHeaderHeight.js
6 days ago
integrationOutlook.js
174 lines
| 1 | import httpClient from '../../../plugins/axios' |
| 2 | import { useRemoveUrlParameter, useUrlQueryParams } from './helper' |
| 3 | |
| 4 | function useOutlookSync(code, successCallback) { |
| 5 | let redirectURL = useRemoveUrlParameter( |
| 6 | useRemoveUrlParameter(useRemoveUrlParameter(window.location.href, 'code'), 'state'), |
| 7 | 'type', |
| 8 | ) |
| 9 | |
| 10 | httpClient |
| 11 | .post('/outlook/authorization/token', { |
| 12 | authCode: code, |
| 13 | userId: useUrlQueryParams(window.location.href)['state'].split( |
| 14 | 'amelia-outlook-calendar-auth-', |
| 15 | )[1], |
| 16 | redirectUri: window.location.href.split('?')[0], |
| 17 | }) |
| 18 | .then(() => { |
| 19 | history.pushState({}, null, redirectURL) |
| 20 | }) |
| 21 | .catch((error) => { |
| 22 | console.log(error) |
| 23 | }) |
| 24 | .finally(() => { |
| 25 | successCallback() |
| 26 | }) |
| 27 | } |
| 28 | |
| 29 | function useOutlookConnect(store) { |
| 30 | const amSettings = store.getters['getSettings'] |
| 31 | if (!store.getters['auth/getOutlookLoading']) { |
| 32 | store.commit('auth/setOutlookLoading', true) |
| 33 | |
| 34 | if (!amSettings.outlookCalendar.accessToken) { |
| 35 | httpClient |
| 36 | .get('/outlook/authorization/url/' + store.getters['employee/getId'], { |
| 37 | redirectUri: window.location.href.split('?')[0], |
| 38 | }) |
| 39 | .then((response) => { |
| 40 | window.location.href = response.data.data.authUrl.replace( |
| 41 | /redirect_uri=.+?&/, |
| 42 | 'redirect_uri=' + window.location.href + '&', |
| 43 | ) |
| 44 | }) |
| 45 | .catch((error) => { |
| 46 | console.log(error) |
| 47 | |
| 48 | store.commit('auth/setOutlookLoading', false) |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | if (amSettings.outlookCalendar.accessToken) { |
| 53 | httpClient |
| 54 | .get('/outlook-calendar/authorization/url/' + store.getters['employee/getId'], { |
| 55 | params: { |
| 56 | redirectUri: window.location.href, |
| 57 | }, |
| 58 | }) |
| 59 | .then((response) => { |
| 60 | window.location.href = response.data.data.authUrl |
| 61 | }) |
| 62 | .catch((error) => { |
| 63 | console.log(error) |
| 64 | |
| 65 | store.commit('auth/setOutlookLoading', false) |
| 66 | }) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | function useOutlookDisconnect(store, accountId = null) { |
| 72 | store.commit('auth/setOutlookLoading', true) |
| 73 | |
| 74 | const endpoint = '/outlook/disconnect/' + store.getters['employee/getId'] |
| 75 | const data = accountId ? { accountId: accountId } : {} |
| 76 | |
| 77 | httpClient |
| 78 | .post(endpoint, data) |
| 79 | .then((response) => { |
| 80 | if (accountId) { |
| 81 | const accounts = store.getters['employee/getOutlookCalendarAccounts'] || [] |
| 82 | |
| 83 | const disconnectedAccount = accounts.find((a) => String(a.id) === String(accountId)) |
| 84 | |
| 85 | const updatedAccounts = accounts.filter( |
| 86 | (account) => String(account.id) !== String(accountId), |
| 87 | ) |
| 88 | store.commit('employee/setOutlookCalendarAccounts', updatedAccounts) |
| 89 | |
| 90 | if (updatedAccounts.length === 0) { |
| 91 | store.commit('employee/setOutlookId', '') |
| 92 | store.commit('employee/setOutlookCalendarId', '') |
| 93 | store.commit('employee/setOutlookToken', null) |
| 94 | store.commit('employee/setOutlookCalendarBlockedCalendars', []) |
| 95 | store.commit('auth/setOutlookCalendars', []) |
| 96 | } else { |
| 97 | const remainingCalendarIds = new Set() |
| 98 | updatedAccounts.forEach((account) => { |
| 99 | if (account.calendarList && account.calendarList.length) { |
| 100 | account.calendarList.forEach((cal) => remainingCalendarIds.add(String(cal.id))) |
| 101 | } |
| 102 | if (account.calendarId) { |
| 103 | remainingCalendarIds.add(String(account.calendarId)) |
| 104 | } |
| 105 | }) |
| 106 | |
| 107 | const removedCalendarIds = new Set() |
| 108 | if (disconnectedAccount) { |
| 109 | if (disconnectedAccount.calendarId) { |
| 110 | removedCalendarIds.add(String(disconnectedAccount.calendarId)) |
| 111 | } |
| 112 | if (disconnectedAccount.calendarList && disconnectedAccount.calendarList.length) { |
| 113 | disconnectedAccount.calendarList.forEach((cal) => |
| 114 | removedCalendarIds.add(String(cal.id)), |
| 115 | ) |
| 116 | } |
| 117 | if ( |
| 118 | disconnectedAccount.blockedCalendars && |
| 119 | disconnectedAccount.blockedCalendars.length |
| 120 | ) { |
| 121 | disconnectedAccount.blockedCalendars.forEach((id) => |
| 122 | removedCalendarIds.add(String(id)), |
| 123 | ) |
| 124 | } |
| 125 | } |
| 126 | if ( |
| 127 | response && |
| 128 | response.data && |
| 129 | response.data.data && |
| 130 | response.data.data.removedCalendarIds |
| 131 | ) { |
| 132 | response.data.data.removedCalendarIds.forEach((id) => |
| 133 | removedCalendarIds.add(String(id)), |
| 134 | ) |
| 135 | } |
| 136 | |
| 137 | const currentCalendarId = store.getters['employee/getOutlookCalendarId'] |
| 138 | if ( |
| 139 | currentCalendarId && |
| 140 | (removedCalendarIds.has(String(currentCalendarId)) || |
| 141 | !remainingCalendarIds.has(String(currentCalendarId))) |
| 142 | ) { |
| 143 | store.commit('employee/setOutlookCalendarId', '') |
| 144 | } |
| 145 | |
| 146 | const blocked = store.getters['employee/getOutlookCalendarBlockedCalendars'] || [] |
| 147 | const cleanedBlocked = blocked.filter((id) => remainingCalendarIds.has(String(id))) |
| 148 | store.commit('employee/setOutlookCalendarBlockedCalendars', cleanedBlocked) |
| 149 | |
| 150 | const firstRemainingConnected = updatedAccounts.find((a) => !!a.token) |
| 151 | if (firstRemainingConnected) { |
| 152 | store.commit('employee/setOutlookId', firstRemainingConnected.id) |
| 153 | store.commit('employee/setOutlookToken', firstRemainingConnected.token) |
| 154 | } |
| 155 | } |
| 156 | } else { |
| 157 | store.commit('employee/setOutlookId', '') |
| 158 | store.commit('employee/setOutlookCalendarId', '') |
| 159 | store.commit('employee/setOutlookToken', null) |
| 160 | store.commit('employee/setOutlookCalendarAccounts', []) |
| 161 | store.commit('employee/setOutlookCalendarBlockedCalendars', []) |
| 162 | store.commit('auth/setOutlookCalendars', []) |
| 163 | } |
| 164 | }) |
| 165 | .catch((error) => { |
| 166 | console.log(error) |
| 167 | }) |
| 168 | .finally(() => { |
| 169 | store.commit('auth/setOutlookLoading', false) |
| 170 | }) |
| 171 | } |
| 172 | |
| 173 | export { useOutlookSync, useOutlookConnect, useOutlookDisconnect } |
| 174 |