2022-11-25 16:01:46 +01:00
|
|
|
import { rowEmission, tableEmission } from "./utils"
|
|
|
|
import mainEmitter from "./index"
|
|
|
|
import env from "../environment"
|
2024-01-18 17:38:34 +01:00
|
|
|
import { Table, Row, DocumentType, App } from "@budibase/types"
|
|
|
|
import { context } from "@budibase/backend-core"
|
|
|
|
|
2024-01-23 11:10:38 +01:00
|
|
|
const MAX_AUTOMATIONS_ALLOWED = 5
|
|
|
|
|
2020-12-07 18:23:53 +01:00
|
|
|
class AutomationEmitter {
|
2022-11-25 16:01:46 +01:00
|
|
|
chainCount: number
|
|
|
|
metadata: { automationChainCount: number }
|
|
|
|
|
|
|
|
constructor(chainCount: number) {
|
2020-12-07 18:23:53 +01:00
|
|
|
this.chainCount = chainCount
|
|
|
|
this.metadata = {
|
|
|
|
automationChainCount: chainCount,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 17:38:34 +01:00
|
|
|
async getMaxAutomationChain() {
|
|
|
|
const db = context.getAppDB()
|
|
|
|
const appMetadata = await db.get<App>(DocumentType.APP_METADATA)
|
|
|
|
let chainAutomations = appMetadata?.automations?.chainAutomations
|
|
|
|
|
|
|
|
if (chainAutomations === true) {
|
2024-01-23 11:10:38 +01:00
|
|
|
return MAX_AUTOMATIONS_ALLOWED
|
2024-01-18 17:38:34 +01:00
|
|
|
} else if (chainAutomations === undefined && env.SELF_HOSTED) {
|
2024-01-23 11:10:38 +01:00
|
|
|
return MAX_AUTOMATIONS_ALLOWED
|
2024-01-18 17:38:34 +01:00
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async emitRow(eventName: string, appId: string, row: Row, table?: Table) {
|
|
|
|
let MAX_AUTOMATION_CHAIN = await this.getMaxAutomationChain()
|
|
|
|
|
2020-12-07 18:23:53 +01:00
|
|
|
// don't emit even if we've reached max automation chain
|
2020-12-07 18:55:35 +01:00
|
|
|
if (this.chainCount >= MAX_AUTOMATION_CHAIN) {
|
2020-12-07 18:23:53 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
rowEmission({
|
|
|
|
emitter: mainEmitter,
|
|
|
|
eventName,
|
|
|
|
appId,
|
|
|
|
row,
|
|
|
|
table,
|
|
|
|
metadata: this.metadata,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-18 17:38:34 +01:00
|
|
|
async emitTable(eventName: string, appId: string, table?: Table) {
|
|
|
|
let MAX_AUTOMATION_CHAIN = await this.getMaxAutomationChain()
|
|
|
|
|
2020-12-07 18:23:53 +01:00
|
|
|
// don't emit even if we've reached max automation chain
|
2024-01-18 17:38:34 +01:00
|
|
|
if (this.chainCount >= MAX_AUTOMATION_CHAIN) {
|
2020-12-07 18:23:53 +01:00
|
|
|
return
|
|
|
|
}
|
2022-11-25 16:01:46 +01:00
|
|
|
|
2020-12-07 18:23:53 +01:00
|
|
|
tableEmission({
|
|
|
|
emitter: mainEmitter,
|
|
|
|
eventName,
|
|
|
|
appId,
|
|
|
|
table,
|
|
|
|
metadata: this.metadata,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 10:37:37 +01:00
|
|
|
export default AutomationEmitter
|