From aeba935c2fcae61491d5b12f6865c7410b30a5f8 Mon Sep 17 00:00:00 2001 From: dev-callgent Date: Wed, 28 Aug 2024 16:32:58 +0800 Subject: [PATCH] fix: map 2 function Signed-off-by: dev-callgent --- package.json | 2 +- pnpm-lock.yaml | 16 +++++------ .../migration.sql | 27 +++++++++++++++++++ prisma/schema.prisma | 5 ++-- prisma/seed.ts | 8 ++++++ src/agents/agents.service.ts | 9 ++++++- src/event-listeners/event-object.ts | 2 +- 7 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 prisma/migrations/20240828045122_event_listener_desc/migration.sql diff --git a/package.json b/package.json index 3896725..d26f531 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "@nestjs/swagger": "^7.3.0", "@nodeteam/nestjs-prisma-pagination": "^1.0.6", "@prisma/client": "5.18.0", - "axios": "^1.6.7", + "axios": "^1.7.5", "bcrypt": "^5.1.1", "class-transformer": "^0.5.1", "class-validator": "^0.14.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8161c1c..adefaaf 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -59,8 +59,8 @@ dependencies: specifier: 5.18.0 version: 5.18.0(prisma@5.18.0) axios: - specifier: ^1.6.7 - version: 1.6.7(debug@2.6.9) + specifier: ^1.7.5 + version: 1.7.5(debug@2.6.9) bcrypt: specifier: ^5.1.1 version: 5.1.1 @@ -2686,10 +2686,10 @@ packages: - supports-color dev: false - /axios@1.6.7(debug@2.6.9): - resolution: {integrity: sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA==} + /axios@1.7.5(debug@2.6.9): + resolution: {integrity: sha512-fZu86yCo+svH3uqJ/yTdQ0QHpQu5oL+/QE+QPSv6BZSkDAoky9vytxp7u5qk83OJFS3kEBcesWni9WTZAv3tSw==} dependencies: - follow-redirects: 1.15.5(debug@2.6.9) + follow-redirects: 1.15.6(debug@2.6.9) form-data: 4.0.0 proxy-from-env: 1.1.0 transitivePeerDependencies: @@ -4361,8 +4361,8 @@ packages: resolution: {integrity: sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==} dev: true - /follow-redirects@1.15.5(debug@2.6.9): - resolution: {integrity: sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==} + /follow-redirects@1.15.6(debug@2.6.9): + resolution: {integrity: sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==} engines: {node: '>=4.0'} peerDependencies: debug: '*' @@ -8733,7 +8733,7 @@ packages: requiresBuild: true dependencies: async: 3.2.5 - axios: 1.6.7(debug@2.6.9) + axios: 1.7.5(debug@2.6.9) bindings: 1.5.0 lodash: 4.17.21 nan: 2.19.0 diff --git a/prisma/migrations/20240828045122_event_listener_desc/migration.sql b/prisma/migrations/20240828045122_event_listener_desc/migration.sql new file mode 100644 index 0000000..94a977c --- /dev/null +++ b/prisma/migrations/20240828045122_event_listener_desc/migration.sql @@ -0,0 +1,27 @@ +-- AlterTable +ALTER TABLE "Callgent" ALTER COLUMN "tenantPk" SET DEFAULT (current_setting('tenancy.tenantPk')::int); + +-- AlterTable +ALTER TABLE "CallgentFunction" ALTER COLUMN "tenantPk" SET DEFAULT (current_setting('tenancy.tenantPk')::int); + +-- AlterTable +ALTER TABLE "Endpoint" ALTER COLUMN "tenantPk" SET DEFAULT (current_setting('tenancy.tenantPk')::int); + +-- AlterTable +ALTER TABLE "EndpointAuth" ALTER COLUMN "tenantPk" SET DEFAULT (current_setting('tenancy.tenantPk')::int); + +-- AlterTable +ALTER TABLE "EventListener" ADD COLUMN "description" VARCHAR(2000) NOT NULL DEFAULT '', +ALTER COLUMN "tenantPk" SET DEFAULT (current_setting('tenancy.tenantPk')::int); + +-- AlterTable +ALTER TABLE "Task" ALTER COLUMN "tenantPk" SET DEFAULT (current_setting('tenancy.tenantPk')::int); + +-- AlterTable +ALTER TABLE "TaskAction" ALTER COLUMN "tenantPk" SET DEFAULT (current_setting('tenancy.tenantPk')::int); + +-- AlterTable +ALTER TABLE "User" ALTER COLUMN "tenantPk" SET DEFAULT (current_setting('tenancy.tenantPk')::int); + +-- AlterTable +ALTER TABLE "UserIdentity" ALTER COLUMN "tenantPk" SET DEFAULT (current_setting('tenancy.tenantPk')::int); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 272214d..77fd577 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -468,6 +468,7 @@ model EventListener { serviceType ServiceType // callgent, service serviceName String @db.VarChar(255) funName String @db.VarChar(255) + description String @default("") @db.VarChar(2000) // TODO add the listener's input/output props in event.context, for automatic checking /// @DtoReadOnly @@ -491,11 +492,11 @@ enum EventCallbackType { model EventStore { /// @DtoEntityHidden - pk BigInt @id @default(autoincrement()) + pk BigInt @id @default(autoincrement()) /// @DtoCreateApiResponse /// @DtoUpdateApiResponse /// @DtoPlainApiResponse - id String @unique @db.VarChar(36) + id String @unique @db.VarChar(36) /// @Description src entity id which bind to the listener srcId String @db.VarChar(36) diff --git a/prisma/seed.ts b/prisma/seed.ts index 99e8c64..66248eb 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -37,6 +37,7 @@ function initEventListeners() { serviceType: 'SERVICE', serviceName: 'EndpointsService', funName: 'preprocessClientRequest', + description: 'Find the CEP, then preprocess the request', createdBy: 'GLOBAL', priority: (priority += 100), }, @@ -50,6 +51,8 @@ function initEventListeners() { serviceType: 'SERVICE', serviceName: 'CallgentFunctionsService', funName: 'loadFunctions', + description: + 'Load all entries of the callgent into event.context.functions', createdBy: 'GLOBAL', priority: (priority += 100), }, @@ -63,6 +66,8 @@ function initEventListeners() { serviceType: 'SERVICE', serviceName: 'EventStoresService', funName: 'loadTargetEvents', + description: + 'Load all events of same targetId into event.context.tgtEvents', createdBy: 'GLOBAL', priority: (priority += 100), }, @@ -76,6 +81,8 @@ function initEventListeners() { serviceType: 'SERVICE', serviceName: 'AgentsService', funName: 'map2Function', + description: + 'Map the request event to a endpoint function, put into event.context.map2Function and event.context.functions[0]', createdBy: 'GLOBAL', priority: (priority += 100), }, @@ -89,6 +96,7 @@ function initEventListeners() { serviceType: 'SERVICE', serviceName: 'SandBoxService', funName: 'map2Args', + description: 'Map the request event to the function arguments', createdBy: 'GLOBAL', priority: (priority += 100), }, diff --git a/src/agents/agents.service.ts b/src/agents/agents.service.ts index 35f5a39..cc6a1b3 100644 --- a/src/agents/agents.service.ts +++ b/src/agents/agents.service.ts @@ -61,7 +61,7 @@ export class AgentsService { }, { funName: '', mapping: '', question: '' }, ); // TODO check `funName` exists in callgentFunctions, validating `mapping` - reqEvent.context.function = mapped; + reqEvent.context.map2Function = mapped; if (mapped.question) { if (!progressive) @@ -81,6 +81,13 @@ export class AgentsService { if (statusCode == 1) return { event: reqEvent, callbackName: 'map2FunctionProgressive' }; throw new HttpException(prEvent.message, statusCode); + } else { + const functions = reqEvent.context.functions.filter( + (f) => f.name == mapped.funName, + ); + if (functions?.length != 1) + throw new BadRequestException('Failed to map to function: ' + mapped); + reqEvent.context.functions = functions; } } diff --git a/src/event-listeners/event-object.ts b/src/event-listeners/event-object.ts index 75ebf31..87f43eb 100644 --- a/src/event-listeners/event-object.ts +++ b/src/event-listeners/event-object.ts @@ -18,7 +18,7 @@ export class EventObject { } public readonly id: string; public statusCode = -1; // for response only - public readonly context: { [key: string]: JsonValue } = {}; + public readonly context: { [key: string]: any } = {}; public message: string; public stopPropagation = false; public defaultPrevented = false;