From 80c67c4f09572ba490d61b0b7ae65af2ff6ef71f Mon Sep 17 00:00:00 2001 From: Akshay Sasidharan Date: Tue, 1 Jul 2025 13:02:15 +0530 Subject: [PATCH] feat: trigger workflows with their name or id - Fixes webhook trigger failing on license check - Add backward compatible changes to trigger workflows with their name or id --- server/package-lock.json | 2 +- .../modules/licensing/guards/webhook.guard.ts | 91 ++++++++++--------- 2 files changed, 50 insertions(+), 43 deletions(-) diff --git a/server/package-lock.json b/server/package-lock.json index fa1e6d68ba..3cef36d54f 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -20645,4 +20645,4 @@ } } } -} \ No newline at end of file +} diff --git a/server/src/modules/licensing/guards/webhook.guard.ts b/server/src/modules/licensing/guards/webhook.guard.ts index 1a5d084643..67dec9fd98 100644 --- a/server/src/modules/licensing/guards/webhook.guard.ts +++ b/server/src/modules/licensing/guards/webhook.guard.ts @@ -5,6 +5,7 @@ import { LicenseTermsService } from '../interfaces/IService'; import { LICENSE_FIELD, LICENSE_LIMIT } from '../constants'; import { AppsRepository } from '@modules/apps/repository'; import { APP_TYPES } from '@modules/apps/constants'; +import { isUUID } from 'class-validator'; @Injectable() export class WebhookGuard implements CanActivate { @@ -16,17 +17,19 @@ export class WebhookGuard implements CanActivate { ) {} async canActivate(context: ExecutionContext): Promise { - const request = context.switchToHttp().getRequest(); + const request = context.switchToHttp().getRequest(); const workflowsLimit = await this.licenseTermsService.getLicenseTerms(LICENSE_FIELD.WORKFLOWS); + const isUuid = isUUID(request?.params?.idOrName); const workflowApp = await this.appsRepository.findOne({ where: { - id: request?.params?.id, + [isUuid ? 'id' : 'name']: request?.params?.idOrName, type: APP_TYPES.WORKFLOW, }, }); if (!workflowApp) throw new HttpException(`Workflow doesn't exists`, 404); + request.tj_app = workflowApp; // Webhook API token validation if (request.headers.authorization.split(' ')[1] !== workflowApp.workflowApiToken) throw new UnauthorizedException(); @@ -35,71 +38,75 @@ export class WebhookGuard implements CanActivate { if (!workflowApp.workflowEnabled) throw new HttpException(`Webhook endpoint disabled or doesn't exists`, 404); // Workspace Level - - // Daily Limit - if ( - workflowsLimit.workspace.daily_executions !== LICENSE_LIMIT.UNLIMITED && - ( - await this.manager.query( - `SELECT COUNT(*) + if (workflowsLimit.workspace) { + // Daily Limit + if ( + workflowsLimit.workspace.daily_executions !== LICENSE_LIMIT.UNLIMITED && + ( + await this.manager.query( + `SELECT COUNT(*) FROM apps a INNER JOIN app_versions av on av.app_id = a.id INNER JOIN workflow_executions we on we.app_version_id = av.id WHERE a.organization_id = $1 AND DATE(we.created_at) = current_date`, - [workflowApp.organizationId] - ) - )[0].count >= workflowsLimit.workspace.daily_executions - ) { - throw new HttpException('Maximum daily limit for workflow execution has reached for this workspace', 451); - } + [workflowApp.organizationId] + ) + )[0].count >= workflowsLimit.workspace.daily_executions + ) { + throw new HttpException('Maximum daily limit for workflow execution has reached for this workspace', 451); + } - // Monthly Limit - if ( - workflowsLimit.workspace.monthly_executions !== LICENSE_LIMIT.UNLIMITED && - ( - await this.manager.query( - `SELECT COUNT(*) + // Monthly Limit + if ( + workflowsLimit.workspace.monthly_executions !== LICENSE_LIMIT.UNLIMITED && + ( + await this.manager.query( + `SELECT COUNT(*) FROM apps a INNER JOIN app_versions av on av.app_id = a.id INNER JOIN workflow_executions we on we.app_version_id = av.id WHERE a.organization_id = $1 AND extract (year from we.created_at) = extract (year from current_date) AND extract (month from we.created_at) = extract (month from current_date)`, - [workflowApp.organizationId] - ) - )[0].count >= workflowsLimit.workspace.monthly_executions - ) { - throw new HttpException('Maximum monthly limit for workflow execution has reached for this workspace', 451); + [workflowApp.organizationId] + ) + )[0].count >= workflowsLimit.workspace.monthly_executions + ) { + throw new HttpException('Maximum monthly limit for workflow execution has reached for this workspace', 451); + } } // Instance Level - - // Daily Limit - if ( - workflowsLimit.instance.daily_executions !== LICENSE_LIMIT.UNLIMITED && - ( - await this.manager.query(`SELECT COUNT(*) + if (workflowsLimit.instance) { + // Daily Limit + if ( + workflowsLimit.instance.daily_executions !== LICENSE_LIMIT.UNLIMITED && + ( + await this.manager.query(`SELECT COUNT(*) FROM apps a INNER JOIN app_versions av on av.app_id = a.id INNER JOIN workflow_executions we on we.app_version_id = av.id WHERE DATE(we.created_at) = current_date`) - )[0].count >= workflowsLimit.instance.daily_executions - ) { - throw new HttpException('Maximum daily limit for workflow execution has been reached', 451); - } + )[0].count >= workflowsLimit.instance.daily_executions + ) { + throw new HttpException('Maximum daily limit for workflow execution has been reached', 451); + } - // Monthly Limit - if ( - workflowsLimit.instance.monthly_executions !== LICENSE_LIMIT.UNLIMITED && - ( - await this.manager.query(`SELECT COUNT(*) + // Monthly Limit + if ( + workflowsLimit.instance.monthly_executions !== LICENSE_LIMIT.UNLIMITED && + ( + await this.manager.query(`SELECT COUNT(*) FROM apps a INNER JOIN app_versions av on av.app_id = a.id INNER JOIN workflow_executions we on we.app_version_id = av.id WHERE extract (year from we.created_at) = extract (year from current_date) AND extract (month from we.created_at) = extract (month from current_date)`) - )[0].count >= workflowsLimit.instance.monthly_executions - ) { - throw new HttpException('Maximum monthly limit for workflow execution has been reached', 451); + )[0].count >= workflowsLimit.instance.monthly_executions + ) { + throw new HttpException('Maximum monthly limit for workflow execution has been reached', 451); + } } return true;