Skip to content

Commit

Permalink
feat: Store service webhook when starting
Browse files Browse the repository at this point in the history
  • Loading branch information
paultranvan committed Jan 19, 2024
1 parent d76a9e7 commit 4cd5131
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
21 changes: 20 additions & 1 deletion src/app/domain/geolocation/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -56,10 +56,29 @@ const fetchSupportMail = async (client?: CozyClient): Promise<string> => {
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?: {
support_address?: string
}
}[]
}

interface Query {
definition: QueryDefinition
options: object
}
33 changes: 31 additions & 2 deletions src/app/domain/geolocation/hooks/tracking.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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()

Expand Down Expand Up @@ -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)
}
}
}
5 changes: 5 additions & 0 deletions src/app/domain/geolocation/tracking/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit 4cd5131

Please sign in to comment.