2022-11-25 16:01:46 +01:00
|
|
|
import { rowEmission, tableEmission } from "./utils"
|
|
|
|
import mainEmitter from "./index"
|
|
|
|
import env from "../environment"
|
2024-10-24 13:51:44 +02:00
|
|
|
import {
|
|
|
|
Table,
|
|
|
|
Row,
|
|
|
|
DocumentType,
|
|
|
|
App,
|
|
|
|
ContextEmitter,
|
|
|
|
EventType,
|
|
|
|
UserBindings,
|
|
|
|
} from "@budibase/types"
|
2024-01-18 17:38:34 +01:00
|
|
|
import { context } from "@budibase/backend-core"
|
|
|
|
|
2024-01-23 11:10:38 +01:00
|
|
|
const MAX_AUTOMATIONS_ALLOWED = 5
|
|
|
|
|
2024-10-24 13:07:13 +02:00
|
|
|
class AutomationEmitter implements ContextEmitter {
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-22 15:36:15 +02:00
|
|
|
async emitRow({
|
|
|
|
eventName,
|
|
|
|
appId,
|
|
|
|
row,
|
|
|
|
table,
|
2024-10-24 13:51:44 +02:00
|
|
|
oldRow,
|
|
|
|
user,
|
2024-10-22 15:36:15 +02:00
|
|
|
}: {
|
2024-10-24 13:51:44 +02:00
|
|
|
eventName: EventType.ROW_SAVE | EventType.ROW_DELETE | EventType.ROW_UPDATE
|
2024-10-22 15:36:15 +02:00
|
|
|
appId: string
|
|
|
|
row: Row
|
|
|
|
table?: Table
|
2024-10-24 13:51:44 +02:00
|
|
|
oldRow?: Row
|
|
|
|
user: UserBindings
|
2024-10-22 15:36:15 +02:00
|
|
|
}) {
|
2024-01-18 17:38:34 +01:00
|
|
|
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,
|
2024-10-24 13:51:44 +02:00
|
|
|
oldRow,
|
2020-12-07 18:23:53 +01:00
|
|
|
metadata: this.metadata,
|
2024-10-24 13:51:44 +02:00
|
|
|
user,
|
2020-12-07 18:23:53 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|