From b8a15207f59f30c8e3e3f9355aa9dc8ad21c8b74 Mon Sep 17 00:00:00 2001
From: Zdravko Iliev <zdravko.iliev@vereign.com>
Date: Thu, 22 Feb 2024 08:16:49 +0000
Subject: [PATCH] fix: add topic type to each webhook

---
 libs/askar/src/agent.utils.ts                 | 25 +++++++------------
 .../src/askar/agent-event-listener.service.ts |  5 +++-
 2 files changed, 13 insertions(+), 17 deletions(-)

diff --git a/libs/askar/src/agent.utils.ts b/libs/askar/src/agent.utils.ts
index 3ae64575..2bd89628 100644
--- a/libs/askar/src/agent.utils.ts
+++ b/libs/askar/src/agent.utils.ts
@@ -599,10 +599,10 @@ export const attachDidWebHandler = (
     }
   });
 };
-
-export const webHookHandler = async (
+export const webHookHandler = async <T>(
   addr: string,
-  ev: TrustPingResponseReceivedEvent,
+  webHookTopic: string,
+  payload: T,
 ) => {
   const promises: Promise<AxiosResponse>[] = [];
 
@@ -611,18 +611,11 @@ export const webHookHandler = async (
   for (const pair of tokenUrlPairs) {
     const [token, url] = pair.split("@");
 
-    const promise = axios.post(
-      url,
-      {
-        thid: ev.payload.message.threadId,
-        connectionId: ev.payload.connectionRecord.id,
+    const promise = axios.post(`${url}/topic/${webHookTopic}`, payload, {
+      headers: {
+        "X-Api-Key": token,
       },
-      {
-        headers: {
-          "X-Api-Key": token,
-        },
-      },
-    );
+    });
 
     promises.push(promise);
   }
@@ -634,10 +627,10 @@ export const webHookHandler = async (
 
     if (promiseResult.status === "rejected") {
       console.log(
-        `Failed to send web hook to ${url}. Reason ${promiseResult.reason}`,
+        `Failed to send web hook to ${url}/topic/${webHookTopic}. Reason ${promiseResult.reason}`,
       );
       continue;
     }
-    console.log(`Successfully sent web hook to ${url}`);
+    console.log(`Successfully sent web hook to ${url}/topic/${webHookTopic}`);
   }
 };
diff --git a/libs/askar/src/askar/agent-event-listener.service.ts b/libs/askar/src/askar/agent-event-listener.service.ts
index 6ac3e2c4..e643b4db 100644
--- a/libs/askar/src/askar/agent-event-listener.service.ts
+++ b/libs/askar/src/askar/agent-event-listener.service.ts
@@ -73,7 +73,10 @@ export class AgentEventListenerService implements OnModuleInit {
             throw new Error("Agent config is missing agentWebHook");
           }
 
-          return webHookHandler(this.agentConfig?.agentWebHook, ev);
+          return webHookHandler(this.agentConfig?.agentWebHook, "ping", {
+            thid: ev.payload.message.threadId,
+            connectionId: ev.payload.connectionRecord.id,
+          });
         },
       );
     }
-- 
GitLab