-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84aa616
commit 25879dd
Showing
3 changed files
with
107 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,62 @@ | ||
import { Module, DynamicModule } from '@nestjs/common' | ||
import { Module, DynamicModule, Provider, Type } from '@nestjs/common' | ||
|
||
import { ConnectionsEventModule } from './connections' | ||
import { MessageEventModule } from './messages' | ||
import { ApiVersion } from 'packages/client/build'; | ||
import { EventHandler } from './interfaces'; | ||
|
||
export interface EventsModuleOptions { | ||
prefix?: string | ||
enableMessages?: boolean | ||
enableConnections?: boolean | ||
enableMessages?: boolean; | ||
enableConnections?: boolean; | ||
eventHandler?: Type<EventHandler>; | ||
url?: string; | ||
version?: string; | ||
} | ||
|
||
export const EVENTS_MODULE_OPTIONS = 'EVENTS_MODULE_OPTIONS' | ||
|
||
@Module({}) | ||
export class EventsModule { | ||
static register(options: EventsModuleOptions = {}): DynamicModule { | ||
const imports = [] | ||
|
||
const providers: Provider[] = [ | ||
{ | ||
provide: EVENTS_MODULE_OPTIONS, | ||
useValue: options, | ||
}, | ||
]; | ||
|
||
|
||
if (options.enableMessages !== false) { | ||
imports.push(MessageEventModule) | ||
imports.push( | ||
MessageEventModule.forRoot({ | ||
eventHandler: options.eventHandler, | ||
url: options.url, | ||
version: options.version as ApiVersion, | ||
}) | ||
) | ||
} | ||
|
||
if (options.enableConnections !== false) { | ||
imports.push(ConnectionsEventModule) | ||
imports.push( | ||
ConnectionsEventModule.forRoot({ | ||
eventHandler: options.eventHandler, | ||
}) | ||
); | ||
} | ||
|
||
return { | ||
module: EventsModule, | ||
imports, | ||
exports: imports, | ||
providers, | ||
exports: [ | ||
...imports, | ||
{ | ||
provide: EVENTS_MODULE_OPTIONS, | ||
useValue: options, | ||
}, | ||
], | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import { | ||
Claim, | ||
CredentialIssuanceMessage, | ||
CredentialTypeInfo, | ||
} from '@2060.io/service-agent-model' | ||
import { Injectable, Logger } from "@nestjs/common" | ||
|
@@ -32,7 +34,66 @@ export class CredentialEventService { | |
* | ||
* @returns {Promise<CredentialTypeInfo>} A promise that resolves when the credential is created successfully. | ||
*/ | ||
async CreateCredential(records: CredentialTypeInfo): Promise<void> { | ||
async createCredential(records: CredentialTypeInfo): Promise<CredentialTypeInfo> { | ||
const [credential] = await this.apiClient.credentialTypes.getAll() | ||
|
||
if (!credential || credential.length === 0) { | ||
const newCredential = await this.apiClient.credentialTypes.create({ | ||
id: records.id, | ||
name: records.name, | ||
version: records.version, | ||
attributes: records.attributes, | ||
}) | ||
credential.push(newCredential) | ||
} | ||
return credential | ||
} | ||
|
||
/** | ||
* Sends a credential issuance to the specified connection using the provided claims. | ||
* This method initiates the issuance process by sending claims as part of a credential to | ||
* the recipient identified by the connection ID. | ||
* | ||
* @param {string} connectionId - The unique identifier of the connection to which the credential | ||
* will be issued. This represents the recipient of the credential. | ||
* | ||
* @param {Record<string, any>} records - A key value objects, where each key represents an attribute | ||
* of the credential. | ||
* | ||
* Example of constructing the `records` array: | ||
* const records = { | ||
* { name: "email", value: "[email protected]" }, | ||
* { name: "name", value: "John Doe" }, | ||
* } | ||
* | ||
* @returns {Promise<void>} A promise that resolves when the credential issuance is successfully | ||
* sent. If an error occurs during the process, the promise will be rejected. | ||
*/ | ||
async sendCredentialIssuance(connectionId: string, records: Record<string, any>): Promise<void> { | ||
const claims: Claim[] = [] | ||
if (records) { | ||
Object.entries(records).forEach(([key, value]) => { | ||
claims.push( | ||
new Claim({ | ||
name: key, | ||
value: value ?? null, | ||
}), | ||
) | ||
}) | ||
} | ||
|
||
let credentialId | ||
let credential = (await this.apiClient.credentialTypes.getAll())[0] | ||
if (!credential) credentialId = (await this.sendCredentialType())[0]?.id | ||
await this.apiClient.messages.send( | ||
new CredentialIssuanceMessage({ | ||
connectionId: connectionId, | ||
credentialDefinitionId: credentialId, | ||
claims: claims, | ||
}), | ||
) | ||
|
||
this.logger.debug('sendCredential with claims: ' + JSON.stringify(claims)) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters