Skip to content

Commit

Permalink
fix add temporal
Browse files Browse the repository at this point in the history
  • Loading branch information
lotharking committed Dec 18, 2024
1 parent 84aa616 commit 25879dd
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 15 deletions.
45 changes: 38 additions & 7 deletions packages/nestjs-client/src/app.module.ts
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,
},
],
}
}
}
63 changes: 62 additions & 1 deletion packages/nestjs-client/src/credentials/credential.service.ts
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"
Expand Down Expand Up @@ -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))
}
}

14 changes: 7 additions & 7 deletions packages/nestjs-client/src/messages/message.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,8 +25,8 @@ export class MessageEventService {

constructor(
@Inject(MESSAGE_MODULE_OPTIONS) private options: MessageModuleOptions,
@InjectRepository(RevocationEntity)
private readonly revocationRepository: Repository<RevocationEntity>,
@InjectRepository(CredentialEntity)
private readonly credentialRepository: Repository<CredentialEntity>,
private readonly connectionRepository: ConnectionsRepository,
@Optional() @Inject(MESSAGE_EVENT) private eventHandler?: EventHandler,
) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 25879dd

Please sign in to comment.