• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

teableio / teable / 8421654220

25 Mar 2024 02:22PM CUT coverage: 79.934% (+53.8%) from 26.087%
8421654220

Pull #495

github

web-flow
Merge 4faeebea5 into 1869c986d
Pull Request #495: chore: add licenses for non-NPM packages

3256 of 3853 branches covered (84.51%)

25152 of 31466 relevant lines covered (79.93%)

1188.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

12.5
/apps/nestjs-backend/src/features/automation/engine/json-rules-engine.ts
1
import { Injectable, Logger, Scope } from '@nestjs/common';
2✔
2
import { assertNever } from '@teable/core';
2✔
3
import dayjs from 'dayjs';
2✔
4
import type { EngineResult, TopLevelCondition } from 'json-rules-engine';
2✔
5
import { Engine } from 'json-rules-engine';
2✔
6
import { Webhook, MailSender, CreateRecord, UpdateRecord } from '../actions';
2✔
7
import type { ActionCore, IActionInputSchema, IActionType } from '../actions/action-core';
2✔
8
import { ActionTypeEnums } from '../enums/action-type.enum';
2✔
9

2✔
10
@Injectable({ scope: Scope.REQUEST })
2✔
11
export class JsonRulesEngine {
2✔
12
  private logger = new Logger(JsonRulesEngine.name);
×
13
  private engine: Engine;
×
14

×
15
  constructor(
×
16
    private readonly webhook: Webhook,
×
17
    private readonly mailSender: MailSender,
×
18
    private readonly createRecord: CreateRecord,
×
19
    private readonly updateRecord: UpdateRecord
×
20
  ) {
×
21
    this.engine = new Engine([], { allowUndefinedFacts: true });
×
22
    this.initOperator();
×
23
    this.initFact();
×
24
  }
×
25

×
26
  private initOperator() {
×
27
    /*
×
28
     * `always` executed, conditions, operator variables
×
29
     */
×
30
    this.engine.addOperator('always', () => {
×
31
      return true;
×
32
    });
×
33
  }
×
34

×
35
  private initFact() {
×
36
    /*
×
37
     * initialize built in system variables
×
38
     */
×
39
    this.engine.addFact<{ [key: string]: unknown }>('__system__.runEnv', () => {
×
40
      return {
×
41
        executionTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
×
42
      };
×
43
    });
×
44
  }
×
45

×
46
  private getAction(actionType: IActionType): ActionCore {
×
47
    switch (actionType) {
×
48
      case ActionTypeEnums.Webhook:
×
49
        return this.webhook;
×
50
      case ActionTypeEnums.MailSender:
×
51
        return this.mailSender;
×
52
      case ActionTypeEnums.CreateRecord:
×
53
        return this.createRecord;
×
54
      case ActionTypeEnums.UpdateRecord:
×
55
        return this.updateRecord;
×
56
      default:
×
57
        assertNever(actionType);
×
58
    }
×
59
  }
×
60

×
61
  addRule(
×
62
    actionId: string,
×
63
    actionType: string,
×
64
    options: {
×
65
      inputSchema: IActionInputSchema;
×
66
      conditions?: TopLevelCondition;
×
67
      priority?: number;
×
68
    }
×
69
  ): void {
×
70
    const { inputSchema, conditions, priority } = options;
×
71

×
72
    const actionRule = this.getAction(actionType as IActionType).bindParams(
×
73
      actionId,
×
74
      inputSchema,
×
75
      priority
×
76
    );
×
77

×
78
    if (conditions) {
×
79
      actionRule.setConditions(conditions);
×
80
    }
×
81

×
82
    this.engine.addRule(actionRule);
×
83
  }
×
84

×
85
  async fire(facts?: Record<string, unknown>): Promise<EngineResult> {
×
86
    return await this.engine.run(facts);
×
87
  }
×
88
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc