From 1c8e70475aa4242b97516137f33e6eb125ce9d67 Mon Sep 17 00:00:00 2001
From: Berend Sliedrecht <berend@animo.id>
Date: Mon, 27 Nov 2023 16:42:13 +0100
Subject: [PATCH] feat(ssi): get connection by id

Signed-off-by: Berend Sliedrecht <berend@animo.id>
---
 apps/shared/src/events/events.ts                |  4 ++++
 .../agent/__tests__/agent.controller.spec.ts    |  2 +-
 .../__tests__/connections.controller.spec.ts    | 17 ++++++++++++++++-
 .../agent/connections/connections.controller.ts | 16 +++++++++++++++-
 .../agent/connections/connections.service.ts    |  4 ++++
 .../test/connections.e2e-spec.ts                | 14 +++++++++++++-
 6 files changed, 53 insertions(+), 4 deletions(-)

diff --git a/apps/shared/src/events/events.ts b/apps/shared/src/events/events.ts
index ee5edda..3702b4b 100644
--- a/apps/shared/src/events/events.ts
+++ b/apps/shared/src/events/events.ts
@@ -25,3 +25,7 @@ export class EventInfoPublicDid extends BaseEvent<{
 export class EventDidcommConnectionsGetAll extends BaseEvent<{
   connections: Array<ConnectionRecord>;
 }> {}
+
+export class EventDidcommConnectionsGetById extends BaseEvent<{
+  connection: ConnectionRecord | null;
+}> {}
diff --git a/apps/ssi-abstraction/src/agent/__tests__/agent.controller.spec.ts b/apps/ssi-abstraction/src/agent/__tests__/agent.controller.spec.ts
index 6487fe7..be3157d 100644
--- a/apps/ssi-abstraction/src/agent/__tests__/agent.controller.spec.ts
+++ b/apps/ssi-abstraction/src/agent/__tests__/agent.controller.spec.ts
@@ -11,7 +11,7 @@ describe('AgentController', () => {
 
   beforeEach(async () => {
     const moduleRef = await Test.createTestingModule({
-      imports: [mockConfigModule(3002)],
+      imports: [mockConfigModule()],
       controllers: [AgentController],
       providers: [AgentService],
     }).compile();
diff --git a/apps/ssi-abstraction/src/agent/connections/__tests__/connections.controller.spec.ts b/apps/ssi-abstraction/src/agent/connections/__tests__/connections.controller.spec.ts
index 690d3f7..da7bf43 100644
--- a/apps/ssi-abstraction/src/agent/connections/__tests__/connections.controller.spec.ts
+++ b/apps/ssi-abstraction/src/agent/connections/__tests__/connections.controller.spec.ts
@@ -13,7 +13,7 @@ describe('ConnectionsController', () => {
 
   beforeEach(async () => {
     const moduleRef = await Test.createTestingModule({
-      imports: [mockConfigModule(3003), AgentModule],
+      imports: [mockConfigModule(), AgentModule],
       controllers: [ConnectionsController],
       providers: [ConnectionsService],
     }).compile();
@@ -34,4 +34,19 @@ describe('ConnectionsController', () => {
       expect(connectionsEvent.data).toStrictEqual({ connections: result });
     });
   });
+
+  describe('get by id', () => {
+    it('should get a connection record by id', async () => {
+      const result: ConnectionRecord | null = null;
+      jest
+        .spyOn(connectionsService, 'getById')
+        .mockImplementation(() => Promise.resolve(result));
+
+      const connectionsEvent = await connectionsController.getById({
+        id: 'id',
+      });
+
+      expect(connectionsEvent.data).toStrictEqual({ connection: result });
+    });
+  });
 });
diff --git a/apps/ssi-abstraction/src/agent/connections/connections.controller.ts b/apps/ssi-abstraction/src/agent/connections/connections.controller.ts
index f9637e3..fa034d5 100644
--- a/apps/ssi-abstraction/src/agent/connections/connections.controller.ts
+++ b/apps/ssi-abstraction/src/agent/connections/connections.controller.ts
@@ -1,6 +1,9 @@
 import { Controller } from '@nestjs/common';
 import { MessagePattern } from '@nestjs/microservices';
-import { EventDidcommConnectionsGetAll } from '@ocm/shared';
+import {
+  EventDidcommConnectionsGetById,
+  EventDidcommConnectionsGetAll,
+} from '@ocm/shared';
 
 import { ConnectionsService } from './connections.service.js';
 
@@ -14,4 +17,15 @@ export class ConnectionsController {
       connections: await this.connectionsService.getAll(),
     });
   }
+
+  @MessagePattern('didcomm.connections.getById')
+  public async getById({
+    id,
+  }: {
+    id: string;
+  }): Promise<EventDidcommConnectionsGetById> {
+    return new EventDidcommConnectionsGetById({
+      connection: await this.connectionsService.getById(id),
+    });
+  }
 }
diff --git a/apps/ssi-abstraction/src/agent/connections/connections.service.ts b/apps/ssi-abstraction/src/agent/connections/connections.service.ts
index 5b8c7e8..fe3123f 100644
--- a/apps/ssi-abstraction/src/agent/connections/connections.service.ts
+++ b/apps/ssi-abstraction/src/agent/connections/connections.service.ts
@@ -16,4 +16,8 @@ export class ConnectionsService {
   public async getAll(): Promise<Array<ConnectionRecord>> {
     return await this.agent.connections.getAll();
   }
+
+  public async getById(id: string): Promise<ConnectionRecord | null> {
+    return await this.agent.connections.findById(id);
+  }
 }
diff --git a/apps/ssi-abstraction/test/connections.e2e-spec.ts b/apps/ssi-abstraction/test/connections.e2e-spec.ts
index a2b5a52..7c53e1a 100644
--- a/apps/ssi-abstraction/test/connections.e2e-spec.ts
+++ b/apps/ssi-abstraction/test/connections.e2e-spec.ts
@@ -1,6 +1,9 @@
 import type { INestApplication } from '@nestjs/common';
 import type { ClientProxy } from '@nestjs/microservices';
-import type { EventDidcommConnectionsGetAll } from '@ocm/shared';
+import type {
+  EventDidcommConnectionsGetById,
+  EventDidcommConnectionsGetAll,
+} from '@ocm/shared';
 
 import { ClientsModule, Transport } from '@nestjs/microservices';
 import { Test } from '@nestjs/testing';
@@ -45,6 +48,15 @@ describe('Connections', () => {
     expect(response.data).toMatchObject({ connections: [] });
   });
 
+  it('didcomm.connections.getById', async () => {
+    const response$: Observable<EventDidcommConnectionsGetById> = client.send(
+      'didcomm.connections.getById',
+      { id: 'some-id' },
+    );
+    const response = await firstValueFrom(response$);
+    expect(response.data).toMatchObject({ connection: null });
+  });
+
   afterAll(async () => {
     await app.close();
     client.close();
-- 
GitLab