From e4f0df608fa82f68f0c104cedc50f82b400bea57 Mon Sep 17 00:00:00 2001 From: pallavighule <61926403+pallavighule@users.noreply.github.com> Date: Thu, 18 Apr 2024 11:44:12 +0530 Subject: [PATCH] refactor:add outOfBandId into issuance webhook event (#94) Signed-off-by: tipusinghaw --- src/events/CredentialEvents.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/events/CredentialEvents.ts b/src/events/CredentialEvents.ts index 10b1cb36..1d60b558 100644 --- a/src/events/CredentialEvents.ts +++ b/src/events/CredentialEvents.ts @@ -1,3 +1,4 @@ +import type { RestMultiTenantAgentModules } from '../cliAgent' import type { ServerConfig } from '../utils/ServerConfig' import type { Agent, CredentialStateChangedEvent } from '@credo-ts/core' @@ -6,11 +7,26 @@ import { CredentialEventTypes } from '@credo-ts/core' import { sendWebSocketEvent } from './WebSocketEvents' import { sendWebhookEvent } from './WebhookEvent' -export const credentialEvents = async (agent: Agent, config: ServerConfig) => { +export const credentialEvents = async (agent: Agent, config: ServerConfig) => { agent.events.on(CredentialEventTypes.CredentialStateChanged, async (event: CredentialStateChangedEvent) => { const record = event.payload.credentialRecord - const body = { ...record.toJSON(), ...event.metadata } + const body: Record = { + ...record.toJSON(), + ...event.metadata, + outOfBandId: null, + } + + if (event.metadata.contextCorrelationId !== 'default' && record?.connectionId) { + await agent.modules.tenants.withTenantAgent( + { tenantId: event.metadata.contextCorrelationId }, + async (tenantAgent) => { + const connectionRecord = await tenantAgent.connections.findById(record.connectionId!) + + body.outOfBandId = connectionRecord?.outOfBandId + } + ) + } // Only send webhook if webhook url is configured if (config.webhookUrl) { await sendWebhookEvent(config.webhookUrl + '/credentials', body, agent.config.logger)