2023-02-16 02:49:30 +13:00
|
|
|
import { Event, IdentityType, AuditLogFn } from "@budibase/types"
|
2022-05-10 21:33:59 +12:00
|
|
|
import { processors } from "./processors"
|
2023-01-11 22:37:37 +13:00
|
|
|
import identification from "./identification"
|
2023-02-16 01:37:32 +13:00
|
|
|
import { getAppId } from "../context"
|
2022-06-01 08:04:41 +12:00
|
|
|
import * as backfill from "./backfill"
|
2022-04-02 09:29:44 +13:00
|
|
|
|
2023-02-16 02:49:30 +13:00
|
|
|
let writeAuditLogs: AuditLogFn | undefined
|
|
|
|
|
|
|
|
export const initAuditLogs = (fn: AuditLogFn) => {
|
|
|
|
writeAuditLogs = fn
|
|
|
|
}
|
|
|
|
|
2022-05-25 11:15:52 +12:00
|
|
|
export const publishEvent = async (
|
|
|
|
event: Event,
|
|
|
|
properties: any,
|
|
|
|
timestamp?: string | number
|
|
|
|
) => {
|
2022-05-24 09:14:44 +12:00
|
|
|
// in future this should use async events via a distributed queue.
|
2022-05-25 07:01:13 +12:00
|
|
|
const identity = await identification.getCurrentIdentity()
|
2022-06-01 08:04:41 +12:00
|
|
|
|
|
|
|
const backfilling = await backfill.isBackfillingEvent(event)
|
|
|
|
// no backfill - send the event and exit
|
|
|
|
if (!backfilling) {
|
2023-02-16 01:37:32 +13:00
|
|
|
// only audit log actual events, don't include backfills
|
|
|
|
const userId = identity.type === IdentityType.USER ? identity.id : undefined
|
2023-02-16 02:49:30 +13:00
|
|
|
if (writeAuditLogs) {
|
|
|
|
await writeAuditLogs(event, properties, {
|
|
|
|
userId,
|
|
|
|
timestamp,
|
|
|
|
appId: getAppId(),
|
|
|
|
})
|
|
|
|
}
|
2022-06-01 08:04:41 +12:00
|
|
|
await processors.processEvent(event, identity, properties, timestamp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// backfill active - check if the event has been sent already
|
|
|
|
const alreadySent = await backfill.isAlreadySent(event, properties)
|
|
|
|
if (alreadySent) {
|
|
|
|
// do nothing
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// send and record the event
|
|
|
|
await processors.processEvent(event, identity, properties, timestamp)
|
|
|
|
await backfill.recordEvent(event, properties)
|
|
|
|
}
|
2022-04-02 09:29:44 +13:00
|
|
|
}
|