Skip to content

Commit

Permalink
feat(tenants): return value from withTenatnAgent (#1832)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored Apr 12, 2024
1 parent 958bf64 commit 8371d87
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
9 changes: 5 additions & 4 deletions packages/tenants/src/TenantsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,17 @@ export class TenantsApi<AgentModules extends ModulesMap = DefaultAgentModules> {
return tenantAgent
}

public async withTenantAgent(
public async withTenantAgent<ReturnValue>(
options: GetTenantAgentOptions,
withTenantAgentCallback: WithTenantAgentCallback<AgentModules>
): Promise<void> {
withTenantAgentCallback: WithTenantAgentCallback<AgentModules, ReturnValue>
): Promise<ReturnValue> {
this.logger.debug(`Getting tenant agent for tenant '${options.tenantId}' in with tenant agent callback`)
const tenantAgent = await this.getTenantAgent(options)

try {
this.logger.debug(`Calling tenant agent callback for tenant '${options.tenantId}'`)
await withTenantAgentCallback(tenantAgent)
const result = await withTenantAgentCallback(tenantAgent)
return result
} catch (error) {
this.logger.error(`Error in tenant agent callback for tenant '${options.tenantId}'`, { error })
throw error
Expand Down
4 changes: 2 additions & 2 deletions packages/tenants/src/TenantsApiOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export interface GetTenantAgentOptions {
tenantId: string
}

export type WithTenantAgentCallback<AgentModules extends ModulesMap> = (
export type WithTenantAgentCallback<AgentModules extends ModulesMap, Return> = (
tenantAgent: TenantAgent<AgentModules>
) => Promise<void>
) => Promise<Return>

export interface CreateTenantOptions {
config: Omit<TenantConfig, 'walletConfig'>
Expand Down
16 changes: 16 additions & 0 deletions packages/tenants/tests/tenants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ describe('Tenants E2E', () => {
)
})

test('withTenantAgent returns value from callback', async () => {
const tenantRecord = await agent1.modules.tenants.createTenant({
config: {
label: 'Tenant 2',
},
})

const result = await agent1.modules.tenants.withTenantAgent({ tenantId: tenantRecord.id }, async () => {
return {
hello: 'world',
}
})

expect(result).toEqual({ hello: 'world' })
})

test('create a connection between two tenants within the same agent', async () => {
// Create tenants
const tenantRecord1 = await agent1.modules.tenants.createTenant({
Expand Down

0 comments on commit 8371d87

Please sign in to comment.