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

teableio / teable / 8421671885

25 Mar 2024 02:23PM CUT coverage: 79.959% (+53.9%) from 26.087%
8421671885

Pull #496

github

web-flow
Merge f587f00fb into 9313e45fb
Pull Request #496: fix: unexpected link convert

3265 of 3865 branches covered (84.48%)

63 of 63 new or added lines in 4 files covered. (100.0%)

762 existing lines in 27 files now uncovered.

25183 of 31495 relevant lines covered (79.96%)

1188.33 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✔
UNCOV
12
  private logger = new Logger(JsonRulesEngine.name);
×
UNCOV
13
  private engine: Engine;
×
UNCOV
14

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

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

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

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

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

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

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

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

×
UNCOV
85
  async fire(facts?: Record<string, unknown>): Promise<EngineResult> {
×
UNCOV
86
    return await this.engine.run(facts);
×
UNCOV
87
  }
×
UNCOV
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