Skip to content

Commit

Permalink
refactor: change the file contruction
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotatek-HocNguyena committed Jun 3, 2024
1 parent 6f29933 commit c035d41
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/core/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import { IonicStorage } from "../storage/ionicStorage";
import { SqliteStorage } from "../storage/sqliteStorage";
import { BaseRecord } from "../storage/storage.types";
import { ConfigurationService } from "../configuration";
import { IpexMessageStorage } from "./records/ipexMessageStorage";
import { IpexMessageRecord } from "./records/ipexMessageRecord";

const walletId = "idw";
class Agent {
Expand All @@ -63,6 +65,7 @@ class Agent {
private connectionNoteStorage!: ConnectionNoteStorage;
private notificationStorage!: NotificationStorage;
private peerConnectionStorage!: PeerConnectionStorage;
private ipexMessageStorage!: IpexMessageStorage;

private signifyClient!: SignifyClient;

Expand Down Expand Up @@ -103,7 +106,8 @@ class Agent {
this.agentServicesProps,
this.identifierStorage,
this.credentialStorage,
this.notificationStorage
this.notificationStorage,
this.ipexMessageStorage
);
}
return this.ipexCommunicationService;
Expand All @@ -115,7 +119,8 @@ class Agent {
this.agentServicesProps,
this.connectionStorage,
this.connectionNoteStorage,
this.credentialStorage
this.credentialStorage,
this.ipexMessageStorage
);
}
return this.connectionService;
Expand Down Expand Up @@ -195,6 +200,9 @@ class Agent {
this.notificationStorage = new NotificationStorage(
this.getStorageService<NotificationRecord>(this.storageSession)
);
this.ipexMessageStorage = new IpexMessageStorage(
this.getStorageService<IpexMessageRecord>(this.storageSession)
);

await signifyReady();
const bran = await this.getBran();
Expand Down
2 changes: 2 additions & 0 deletions src/core/agent/agent.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,6 @@ export type {
IdentifierResult,
KeriaStatusChangedEvent,
BranAndMnemonic,
IpexMessages,
IpexMessageDetails,
};
41 changes: 41 additions & 0 deletions src/core/agent/records/ipexMessageRecord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { BaseRecord } from "../../storage/storage.types";
import { IpexMessages } from "../agent.types";

interface IpexMessageProps {
id: string;
content: IpexMessages;
connectionId: string;
}

class IpexMessageRecord extends BaseRecord {
content!: IpexMessages;
connectionId!: string;

static readonly type = "IpexMessagMetadataRecord";
readonly type = IpexMessageRecord.type;

constructor(props: {
id: string;
content: IpexMessages;
connectionId: string;
}) {
super();

if (props) {
this.id = props.id;
this.content = props.content;
this.connectionId = props.connectionId;
}
}

getTags() {
return {
...this._tags,
connectionId: this.connectionId,
id: this.id,
};
}
}

export { IpexMessageRecord };
export type { IpexMessageProps };
39 changes: 39 additions & 0 deletions src/core/agent/records/ipexMessageStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { StorageService } from "../../storage/storage.types";
import { IpexMessageProps, IpexMessageRecord } from "./ipexMessageRecord";

class IpexMessageStorage {
static readonly IPEX_MESSAGE_METADATA_RECORD_MISSING =
"Ipex message metadata record does not exist";
private storageService: StorageService<IpexMessageRecord>;

constructor(storageService: StorageService<IpexMessageRecord>) {
this.storageService = storageService;
}

async getIpexMessageMetadata(id: string): Promise<IpexMessageRecord> {
const metadata = await this.storageService.findById(id, IpexMessageRecord);
if (!metadata) {
throw new Error(IpexMessageStorage.IPEX_MESSAGE_METADATA_RECORD_MISSING);
}
return metadata;
}

async createIpexMessageRecord(data: IpexMessageProps): Promise<void> {
const record = new IpexMessageRecord(data);
await this.storageService.save(record);
}

async getIpexMessageMetadataByConnectionId(
connectionId: string
): Promise<IpexMessageRecord[]> {
const records = await this.storageService.findAllByQuery(
{
connectionId,
},
IpexMessageRecord
);
return records;
}
}

export { IpexMessageStorage };
4 changes: 3 additions & 1 deletion src/core/agent/services/connectionService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EventService } from "./eventService";
import { CredentialStorage, IdentifierStorage } from "../records";
import { ConfigurationService } from "../../configuration";
import { Agent } from "../agent";
import { IpexMessageStorage } from "../records/ipexMessageStorage";

const contactListMock = jest.fn();
const deleteContactMock = jest.fn();
Expand Down Expand Up @@ -127,7 +128,8 @@ const connectionService = new ConnectionService(
agentServicesProps,
connectionStorage as any,
connectionNoteStorage as any,
new CredentialStorage(session as any)
new CredentialStorage(session as any),
new IpexMessageStorage(session as any)
);

jest.mock("../../../core/agent/agent", () => ({
Expand Down
21 changes: 19 additions & 2 deletions src/core/agent/services/connectionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
AgentServicesProps,
OobiScan,
KeriConnectionType,
IpexMessageDetails,
} from "../agent.types";
import { AgentService } from "./agentService";
import { Agent } from "../agent";
Expand All @@ -22,23 +23,26 @@ import {
} from "../records";
import { OnlineOnly, waitAndGetDoneOp } from "./utils";
import { ConnectionHistoryType, KeriaContact } from "./connection.types";
import { ConfigurationService } from "../../configuration";
import { IpexMessageStorage } from "../records/ipexMessageStorage";

class ConnectionService extends AgentService {
protected readonly connectionStorage!: ConnectionStorage;
protected readonly connectionNoteStorage!: ConnectionNoteStorage;
protected readonly credentialStorage: CredentialStorage;
protected readonly ipexMessageStorage: IpexMessageStorage;

constructor(
agentServiceProps: AgentServicesProps,
connectionStorage: ConnectionStorage,
connectionNoteStorage: ConnectionNoteStorage,
credentialStorage: CredentialStorage
credentialStorage: CredentialStorage,
ipexMessageStorage: IpexMessageStorage
) {
super(agentServiceProps);
this.connectionStorage = connectionStorage;
this.connectionNoteStorage = connectionNoteStorage;
this.credentialStorage = credentialStorage;
this.ipexMessageStorage = ipexMessageStorage;
}

static readonly CONNECTION_NOTE_RECORD_NOT_FOUND =
Expand Down Expand Up @@ -161,6 +165,9 @@ class ConnectionService extends AgentService {
).createdAt.toISOString(),
serviceEndpoints: [connection.oobi],
notes: await this.getConnectNotesByConnectionId(connection.id),
linkedIpexMessages: await this.getLinkedIPEXMessageByConnectionId(
connection.id
),
};
}

Expand Down Expand Up @@ -321,6 +328,16 @@ class ConnectionService extends AgentService {
};
});
}

private async getLinkedIPEXMessageByConnectionId(
connectionId: string
): Promise<IpexMessageDetails[]> {
const messages =
await this.ipexMessageStorage.getIpexMessageMetadataByConnectionId(
connectionId
);
return messages;
}
}

export { ConnectionService };
10 changes: 9 additions & 1 deletion src/core/agent/services/ipexCommunicationService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ const credentialStorage = jest.mocked({
getCredentialMetadatasById: jest.fn(),
});

const ipexMessageRecordStorage = jest.mocked({
getIpexMessageMetadata: jest.fn(),
getIpexMessageMetadataByConnectionId: jest.fn(),
createIpexMessageRecord: jest.fn(),
});

let credentialListMock = jest.fn();
let credentialGetMock = jest.fn();
const identifierListMock = jest.fn();
Expand Down Expand Up @@ -177,7 +183,8 @@ const ipexCommunicationService = new IpexCommunicationService(
agentServicesProps,
identifierStorage as any,
credentialStorage as any,
notificationStorage as any
notificationStorage as any,
ipexMessageRecordStorage as any
);

describe("Ipex communication service of agent", () => {
Expand Down Expand Up @@ -206,6 +213,7 @@ describe("Ipex communication service of agent", () => {
connectionId: "i",
})
);
expect(ipexMessageRecordStorage.createIpexMessageRecord).toBeCalledTimes(1);
expect(credentialStorage.updateCredentialMetadata).toBeCalledWith("id", {
id: "id",
status: CredentialStatus.CONFIRMED,
Expand Down
21 changes: 20 additions & 1 deletion src/core/agent/services/ipexCommunicationService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { v4 as uuidv4 } from "uuid";
import { Serder } from "signify-ts";
import { ConfigurationService } from "../../configuration";
import { Agent } from "../agent";
import {
AcdcEventTypes,
IpexMessages,
type AcdcStateChangedEvent,
type AgentServicesProps,
type KeriaNotification,
Expand All @@ -23,6 +25,7 @@ import {
} from "./credentialService.types";
import { OnlineOnly, getCredentialShortDetails } from "./utils";
import { CredentialsMatchingApply } from "./ipexCommunicationService.types";
import { IpexMessageStorage } from "../records/ipexMessageStorage";

class IpexCommunicationService extends AgentService {
static readonly ISSUEE_NOT_FOUND_LOCALLY =
Expand All @@ -47,17 +50,20 @@ class IpexCommunicationService extends AgentService {
protected readonly identifierStorage: IdentifierStorage;
protected readonly credentialStorage: CredentialStorage;
protected readonly notificationStorage: NotificationStorage;
protected readonly ipexMessageStorage: IpexMessageStorage;

constructor(
agentServiceProps: AgentServicesProps,
identifierStorage: IdentifierStorage,
credentialStorage: CredentialStorage,
notificationStorage: NotificationStorage
notificationStorage: NotificationStorage,
ipexMessageStorage: IpexMessageStorage
) {
super(agentServiceProps);
this.identifierStorage = identifierStorage;
this.credentialStorage = credentialStorage;
this.notificationStorage = notificationStorage;
this.ipexMessageStorage = ipexMessageStorage;
}

@OnlineOnly
Expand All @@ -69,13 +75,15 @@ class IpexCommunicationService extends AgentService {
const exn = await this.signifyClient
.exchanges()
.get(notifRecord.a.d as string);

const credentialId = exn.exn.e.acdc.d;
const connectionId = exn.exn.i;
await this.saveAcdcMetadataRecord(
exn.exn.e.acdc.d,
exn.exn.e.acdc.a.dt,
connectionId
);
await this.createLinkedIpexMessageRecord(exn.exn.i, exn);

this.eventService.emit<AcdcStateChangedEvent>({
type: AcdcEventTypes.AcdcStateChanged,
Expand Down Expand Up @@ -329,6 +337,17 @@ class IpexCommunicationService extends AgentService {
};
}
}

private async createLinkedIpexMessageRecord(
connectionId: string,
message: IpexMessages
): Promise<void> {
await this.ipexMessageStorage.createIpexMessageRecord({
id: uuidv4(),
content: message,
connectionId,
});
}
}

export { IpexCommunicationService };

0 comments on commit c035d41

Please sign in to comment.