Browse Source

enable SSE for hello asso profile after connection

Olivier Massot 2 months ago
parent
commit
4d3dee0c0d

+ 0 - 0
models/ApiResources/HelloAsso/AuthUrl.ts → models/HelloAsso/AuthUrl.ts


+ 0 - 0
models/ApiResources/HelloAsso/ConnectionRequest.ts → models/HelloAsso/ConnectionRequest.ts


+ 0 - 0
models/ApiResources/HelloAsso/HelloAssoProfile.ts → models/HelloAsso/HelloAssoProfile.ts


+ 1 - 1
pages/helloasso/callback.vue

@@ -29,7 +29,7 @@ Page cible du callback après authentification via la mire d'autorisation HelloA
  */
  */
 import type { RouteLocationNormalizedLoaded } from 'vue-router'
 import type { RouteLocationNormalizedLoaded } from 'vue-router'
 import { useEntityManager } from '~/composables/data/useEntityManager'
 import { useEntityManager } from '~/composables/data/useEntityManager'
-import ConnectionRequest from '~/models/ApiResources/HelloAsso/ConnectionRequest'
+import ConnectionRequest from '~/models/HelloAsso/ConnectionRequest'
 import { useOrganizationProfileStore } from '~/stores/organizationProfile'
 import { useOrganizationProfileStore } from '~/stores/organizationProfile'
 
 
 definePageMeta({
 definePageMeta({

+ 29 - 7
pages/helloasso/index.vue

@@ -16,8 +16,14 @@ Administration de la connexion Opentalent / HelloAsso
 
 
       <v-row>
       <v-row>
         <v-col cols="12" class="d-flex justify-center align-center w-100 mt-6">
         <v-col cols="12" class="d-flex justify-center align-center w-100 mt-6">
+          <v-progress-circular
+            v-if="statusHelloAssoProfile === FETCHING_STATUS.PENDING"
+            indeterminate
+            size="32"
+          />
+
           <UiButtonHelloAssoConnect
           <UiButtonHelloAssoConnect
-            v-if="!helloAssoProfile || !helloAssoProfile.token"
+            v-else-if="!helloAssoProfile || !helloAssoProfile.token"
             @click="onHelloAssoConnectClicked"
             @click="onHelloAssoConnectClicked"
           />
           />
 
 
@@ -34,10 +40,11 @@ Administration de la connexion Opentalent / HelloAsso
 </template>
 </template>
 
 
 <script setup lang="ts">
 <script setup lang="ts">
-import AuthUrl from '~/models/ApiResources/HelloAsso/AuthUrl'
-import HelloAssoProfile from '~/models/ApiResources/HelloAsso/HelloAssoProfile'
+import AuthUrl from '~/models/HelloAsso/AuthUrl'
+import HelloAssoProfile from '~/models/HelloAsso/HelloAssoProfile'
 import { useEntityManager } from '~/composables/data/useEntityManager'
 import { useEntityManager } from '~/composables/data/useEntityManager'
 import { useEntityFetch } from '~/composables/data/useEntityFetch'
 import { useEntityFetch } from '~/composables/data/useEntityFetch'
+import { FETCHING_STATUS } from '~/types/enum/data'
 
 
 const { em } = useEntityManager()
 const { em } = useEntityManager()
 
 
@@ -74,13 +81,28 @@ onMounted(() => {
 
 
 const { fetch } = useEntityFetch()
 const { fetch } = useEntityFetch()
 
 
-const { data: helloAssoProfile, refresh: refreshHelloAsso } = await fetch(HelloAssoProfile)
+const {
+  status: statusHelloAssoProfile,
+  refresh: refreshHelloAssoProfile,
+} = await fetch(HelloAssoProfile)
+
+const helloAssoProfile: ComputedRef<HelloAssoProfile | null> = computed(() => {
+  if (statusHelloAssoProfile.value !== FETCHING_STATUS.SUCCESS) {
+    return null
+  }
+  return em.find(HelloAssoProfile, 1)
+})
 
 
 const onHelloAssoConnected = async () => {
 const onHelloAssoConnected = async () => {
-  console.log('Helloasso connected')
-  await refreshHelloAsso()
-}
+  // On attend 200ms pour laisser en attente du message SSE
+  await new Promise(r => setTimeout(r, 200))
 
 
+  if (!helloAssoProfile.value || !helloAssoProfile.value.token) {
+    // Fallback en cas de défaut de fonctionnement du SSE
+    console.log('Helloasso connected (fallback SSE)')
+    await refreshHelloAssoProfile()
+  }
+}
 </script>
 </script>
 
 
 <style scoped lang="scss">
 <style scoped lang="scss">

+ 1 - 1
prepare/buildIndex.ts

@@ -23,7 +23,7 @@ files.forEach((file) => {
   let entity = null
   let entity = null
 
 
   for (const line of lines) {
   for (const line of lines) {
-    const match = line.match(/static entity = ['"]([\w-/]+)['"]/)
+    const match = line.match(/static (?:override )?entity = ['"]([\w-/]+)['"]/)
     if (match) {
     if (match) {
       // afficher le groupe capturant
       // afficher le groupe capturant
       entity = match[1]
       entity = match[1]

+ 2 - 2
services/data/entityManager.ts

@@ -102,7 +102,7 @@ class EntityManager {
    * @param iri An IRI of the form .../api/<entity>/...
    * @param iri An IRI of the form .../api/<entity>/...
    */
    */
   public async getModelFromIri(iri: string): Promise<typeof ApiResource> {
   public async getModelFromIri(iri: string): Promise<typeof ApiResource> {
-    const matches = iri.match(/^\/api\/(\w+)\/.*/)
+    const matches = iri.match(/^\/api\/([\w-\/]+)(\/\d+)?/)
     if (!matches || !matches[1]) {
     if (!matches || !matches[1]) {
       throw new Error('cannot parse the IRI')
       throw new Error('cannot parse the IRI')
     }
     }
@@ -161,7 +161,7 @@ class EntityManager {
    * @param model
    * @param model
    * @param id
    * @param id
    */
    */
-  public find<T extends typeof ApiResource>(model: T, id: number | string): T {
+  public find<T extends typeof ApiResource>(model: T, id: number | string): InstanceType<T> {
     const repository = this.getRepository(model)
     const repository = this.getRepository(model)
     return repository.find(id) as T
     return repository.find(id) as T
   }
   }