diff --git a/src/app/domain/geolocation/helpers/index.ts b/src/app/domain/geolocation/helpers/index.ts index 060cda630..d507dfe79 100644 --- a/src/app/domain/geolocation/helpers/index.ts +++ b/src/app/domain/geolocation/helpers/index.ts @@ -1,7 +1,7 @@ import BackgroundGeolocation from 'react-native-background-geolocation' import DeviceInfo from 'react-native-device-info' -import CozyClient, { Q, fetchPolicies } from 'cozy-client' +import CozyClient, { Q, QueryDefinition, fetchPolicies } from 'cozy-client' import Minilog from 'cozy-minilog' import { t } from '/locales/i18n' @@ -56,6 +56,20 @@ const fetchSupportMail = async (client?: CozyClient): Promise => { return result.data?.[0]?.attributes?.support_address ?? t('support.email') } +export const buildServiceWebhookQuery = (): Query => { + // See https://github.com/cozy/cozy-client/blob/c0c7fbf1307bb383debaa6bdb3c79c29c889dbc8/packages/cozy-stack-client/src/TriggerCollection.js#L132 + return { + definition: Q('io.cozy.triggers').where({ + worker: 'service', + type: '@webhook' + }), + options: { + as: 'io.cozy.triggers/webhook/fetchOpenPathTripsWebhook', + fetchPolicy: fetchPolicies.olderThan(60 * 60 * 1000) + } + } +} + interface InstanceInfo { data?: { attributes?: { @@ -63,3 +77,8 @@ interface InstanceInfo { } }[] } + +interface Query { + definition: QueryDefinition + options: object +} diff --git a/src/app/domain/geolocation/hooks/tracking.ts b/src/app/domain/geolocation/hooks/tracking.ts index f6e9d0543..764df4850 100644 --- a/src/app/domain/geolocation/hooks/tracking.ts +++ b/src/app/domain/geolocation/hooks/tracking.ts @@ -1,6 +1,6 @@ import { useEffect } from 'react' -import { useClient } from 'cozy-client' +import { useClient, useQuery } from 'cozy-client' import Minilog from 'cozy-minilog' import { @@ -9,9 +9,23 @@ import { } from '/app/domain/geolocation/services/tracking' import { checkGeolocationQuota } from '/app/domain/geolocation/helpers/quota' import { GeolocationTrackingEmitter } from '/app/domain/geolocation/tracking/events' -import { TRIP_END } from '/app/domain/geolocation/tracking/consts' +import { + FETCH_OPENPATH_TRIPS_SERVICE_NAME, + TRIP_END +} from '/app/domain/geolocation/tracking/consts' +import { buildServiceWebhookQuery } from '/app/domain/geolocation/helpers/index' +import { storeFetchServiceWebHook } from '/app/domain/geolocation/tracking' const log = Minilog('📍 Geolocation') +interface WebhookTrigger { + message: { + name: string + } + links?: { + webhook?: string + } +} + export const useGeolocationTracking = (): void => { const client = useClient() @@ -45,4 +59,19 @@ export const useGeolocationTracking = (): void => { void initializeTracking() }, [client]) + + const webhookQuery = buildServiceWebhookQuery() + const webhookResp = useQuery(webhookQuery.definition, webhookQuery.options) + + if (Array.isArray(webhookResp.data) && webhookResp.data.length > 0) { + const data = webhookResp.data as WebhookTrigger[] + + const openpathServiceWebHook = data.find( + trigger => trigger.message.name === FETCH_OPENPATH_TRIPS_SERVICE_NAME + ) + const webhook = openpathServiceWebHook?.links?.webhook + if (webhook) { + void storeFetchServiceWebHook(webhook) + } + } } diff --git a/src/app/domain/geolocation/tracking/consts.js b/src/app/domain/geolocation/tracking/consts.js index 65b5da7e8..a6210d147 100644 --- a/src/app/domain/geolocation/tracking/consts.js +++ b/src/app/domain/geolocation/tracking/consts.js @@ -52,3 +52,8 @@ export const SERVER_URL = 'https://openpath.cozycloud.cc' * Available events in GeolocationTrackingEmitter */ export const TRIP_END = 'TRIP_END' + +/** + * CoachCO2 service + */ +export const FETCH_OPENPATH_TRIPS_SERVICE_NAME = 'fetchOpenPathTripsWebhook'