From 25879ddea6d1385bfc8ed4242c5242e08e956b76 Mon Sep 17 00:00:00 2001 From: Andres Vallecilla Date: Wed, 18 Dec 2024 08:37:56 -0500 Subject: [PATCH] fix add temporal --- packages/nestjs-client/src/app.module.ts | 45 ++++++++++--- .../src/credentials/credential.service.ts | 63 ++++++++++++++++++- .../src/messages/message.service.ts | 14 ++--- 3 files changed, 107 insertions(+), 15 deletions(-) diff --git a/packages/nestjs-client/src/app.module.ts b/packages/nestjs-client/src/app.module.ts index 22db711..014e4d2 100644 --- a/packages/nestjs-client/src/app.module.ts +++ b/packages/nestjs-client/src/app.module.ts @@ -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; + 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, + }, + ], } } } diff --git a/packages/nestjs-client/src/credentials/credential.service.ts b/packages/nestjs-client/src/credentials/credential.service.ts index 80e284f..13d96f7 100644 --- a/packages/nestjs-client/src/credentials/credential.service.ts +++ b/packages/nestjs-client/src/credentials/credential.service.ts @@ -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} A promise that resolves when the credential is created successfully. */ - async CreateCredential(records: CredentialTypeInfo): Promise { + async createCredential(records: CredentialTypeInfo): Promise { + 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} 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: "john.doe@example.com" }, + * { name: "name", value: "John Doe" }, + * } + * + * @returns {Promise} 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): Promise { + 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)) } } diff --git a/packages/nestjs-client/src/messages/message.service.ts b/packages/nestjs-client/src/messages/message.service.ts index 32b4d43..57342b1 100644 --- a/packages/nestjs-client/src/messages/message.service.ts +++ b/packages/nestjs-client/src/messages/message.service.ts @@ -12,9 +12,9 @@ import { Repository } from 'typeorm' import { ConnectionsRepository } from '../connections' import { EventHandler } from '../interfaces' -import { RevocationEntity } from '../models' import { MESSAGE_EVENT, MESSAGE_MODULE_OPTIONS, MessageModuleOptions } from './message.config' +import { CredentialEntity } from '../credentials' @Injectable() export class MessageEventService { @@ -25,8 +25,8 @@ export class MessageEventService { constructor( @Inject(MESSAGE_MODULE_OPTIONS) private options: MessageModuleOptions, - @InjectRepository(RevocationEntity) - private readonly revocationRepository: Repository, + @InjectRepository(CredentialEntity) + private readonly credentialRepository: Repository, private readonly connectionRepository: ConnectionsRepository, @Optional() @Inject(MESSAGE_EVENT) private eventHandler?: EventHandler, ) { @@ -59,19 +59,19 @@ export class MessageEventService { const [credential] = await this.apiClient.credentialTypes.getAll() const connectionId = await this.connectionRepository.findById(event.message.connectionId) const hash = Buffer.from(await this.eventHandler.credentialHash(event.message.connectionId)) - const currentCred = await this.revocationRepository.findOneBy({ hash }) + const currentCred = await this.credentialRepository.findOneBy({ hash }) const isCredentialDone = event.message.state === CredentialState.Done if (connectionId && isCredentialDone) { if (!currentCred) { - const credentialRev = this.revocationRepository.create({ + const credentialRev = this.credentialRepository.create({ connectionId, hash, revocationDefinitionId: credential.revocationId, }) - await this.revocationRepository.save(credentialRev) + await this.credentialRepository.save(credentialRev) } else { - this.revocationRepository.update(currentCred.id, { connectionId }) + this.credentialRepository.update(currentCred.id, { connectionId }) } } } catch (error) {