mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-23 08:58:26 +00:00
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
This commit is contained in:
parent
9e470a5076
commit
80c67c4f09
2 changed files with 50 additions and 43 deletions
2
server/package-lock.json
generated
2
server/package-lock.json
generated
|
|
@ -20645,4 +20645,4 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<boolean> {
|
||||
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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue