Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing old row on automations #14862

Merged
merged 4 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 30 additions & 31 deletions packages/server/src/api/controllers/row/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DeleteRow,
DeleteRowRequest,
DeleteRows,
EventType,
ExportRowsRequest,
ExportRowsResponse,
FieldType,
Expand Down Expand Up @@ -64,15 +65,15 @@ export async function patch(
ctx.throw(404, "Row not found")
}
ctx.status = 200
ctx.eventEmitter &&
ctx.eventEmitter.emitRow({
eventName: `row:update`,
appId,
row,
table,
oldRow,
user: sdk.users.getUserContextBindings(ctx.user),
})

ctx.eventEmitter?.emitRow({
eventName: EventType.ROW_UPDATE,
appId,
row,
table,
oldRow,
user: sdk.users.getUserContextBindings(ctx.user),
})
ctx.message = `${table.name} updated successfully.`
ctx.body = row
gridSocket?.emitRowUpdate(ctx, row)
Expand Down Expand Up @@ -103,14 +104,14 @@ export const save = async (ctx: UserCtx<Row, Row>) => {
sdk.rows.save(sourceId, ctx.request.body, ctx.user?._id)
)
ctx.status = 200
ctx.eventEmitter &&
ctx.eventEmitter.emitRow({
eventName: `row:save`,
appId,
row,
table,
user: sdk.users.getUserContextBindings(ctx.user),
})

ctx.eventEmitter?.emitRow({
eventName: EventType.ROW_SAVE,
appId,
row,
table,
user: sdk.users.getUserContextBindings(ctx.user),
})
ctx.message = `${table.name} saved successfully`
// prefer squashed for response
ctx.body = row || squashed
Expand Down Expand Up @@ -182,13 +183,12 @@ async function deleteRows(ctx: UserCtx<DeleteRowRequest>) {
}

for (let row of rows) {
ctx.eventEmitter &&
ctx.eventEmitter.emitRow({
eventName: `row:delete`,
appId,
row,
user: sdk.users.getUserContextBindings(ctx.user),
})
ctx.eventEmitter?.emitRow({
eventName: EventType.ROW_DELETE,
appId,
row,
user: sdk.users.getUserContextBindings(ctx.user),
})
gridSocket?.emitRowDeletion(ctx, row)
}
return rows
Expand All @@ -203,13 +203,12 @@ async function deleteRow(ctx: UserCtx<DeleteRowRequest>) {
await quotas.removeRow()
}

ctx.eventEmitter &&
ctx.eventEmitter.emitRow({
eventName: `row:delete`,
appId,
row: resp.row,
user: sdk.users.getUserContextBindings(ctx.user),
})
ctx.eventEmitter?.emitRow({
eventName: EventType.ROW_DELETE,
appId,
row: resp.row,
user: sdk.users.getUserContextBindings(ctx.user),
})
gridSocket?.emitRowDeletion(ctx, resp.row)

return resp
Expand Down
8 changes: 4 additions & 4 deletions packages/server/src/api/controllers/table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
BulkImportResponse,
CsvToJsonRequest,
CsvToJsonResponse,
EventType,
FetchTablesResponse,
FieldType,
MigrateRequest,
Expand Down Expand Up @@ -129,8 +130,7 @@ export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
}
ctx.status = 200
ctx.message = `Table ${table.name} saved successfully.`
ctx.eventEmitter &&
ctx.eventEmitter.emitTable(`table:save`, appId, { ...savedTable })
ctx.eventEmitter?.emitTable(EventType.TABLE_SAVE, appId, { ...savedTable })
ctx.body = savedTable

savedTable = await processTable(savedTable)
Expand All @@ -143,8 +143,8 @@ export async function destroy(ctx: UserCtx) {
await sdk.rowActions.deleteAll(tableId)
const deletedTable = await pickApi({ tableId }).destroy(ctx)
await events.table.deleted(deletedTable)
ctx.eventEmitter &&
ctx.eventEmitter.emitTable(`table:delete`, appId, deletedTable)

ctx.eventEmitter?.emitTable(EventType.TABLE_DELETE, appId, deletedTable)
ctx.status = 200
ctx.table = deletedTable
ctx.body = { message: `Table ${tableId} deleted.` }
Expand Down
11 changes: 2 additions & 9 deletions packages/server/src/db/linkedRows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { context, features } from "@budibase/backend-core"
import {
ContextUser,
EventType,
FeatureFlag,
FieldType,
LinkDocumentValue,
Expand Down Expand Up @@ -44,15 +45,7 @@ const INVALID_DISPLAY_COLUMN_TYPE = [
* This functionality makes sure that when rows with links are created, updated or deleted they are processed
* correctly - making sure that no stale links are left around and that all links have been made successfully.
*/

export const EventType = {
ROW_SAVE: "row:save",
ROW_UPDATE: "row:update",
ROW_DELETE: "row:delete",
TABLE_SAVE: "table:save",
TABLE_UPDATED: "table:updated",
TABLE_DELETE: "table:delete",
}
export { EventType } from "@budibase/types"

function clearRelationshipFields(schema: TableSchema, rows: Row[]) {
for (let [key, field] of Object.entries(schema)) {
Expand Down
20 changes: 17 additions & 3 deletions packages/server/src/events/AutomationEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { rowEmission, tableEmission } from "./utils"
import mainEmitter from "./index"
import env from "../environment"
import { Table, Row, DocumentType, App } from "@budibase/types"
import {
Table,
Row,
DocumentType,
App,
ContextEmitter,
EventType,
UserBindings,
} from "@budibase/types"
import { context } from "@budibase/backend-core"

const MAX_AUTOMATIONS_ALLOWED = 5

class AutomationEmitter {
class AutomationEmitter implements ContextEmitter {
chainCount: number
metadata: { automationChainCount: number }

Expand Down Expand Up @@ -36,11 +44,15 @@ class AutomationEmitter {
appId,
row,
table,
oldRow,
user,
}: {
eventName: string
eventName: EventType.ROW_SAVE | EventType.ROW_DELETE | EventType.ROW_UPDATE
appId: string
row: Row
table?: Table
oldRow?: Row
user: UserBindings
}) {
let MAX_AUTOMATION_CHAIN = await this.getMaxAutomationChain()

Expand All @@ -54,7 +66,9 @@ class AutomationEmitter {
appId,
row,
table,
oldRow,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual fix

metadata: this.metadata,
user,
})
}

Expand Down
16 changes: 11 additions & 5 deletions packages/server/src/events/BudibaseEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { EventEmitter } from "events"
import { rowEmission, tableEmission } from "./utils"
import { Table, Row, User } from "@budibase/types"
import {
Table,
Row,
UserBindings,
EventType,
ContextEmitter,
} from "@budibase/types"

/**
* keeping event emitter in one central location as it might be used for things other than
Expand All @@ -12,7 +18,7 @@ import { Table, Row, User } from "@budibase/types"
* Extending the standard emitter to some syntactic sugar and standardisation to the emitted event.
* This is specifically quite important for template strings used in automations.
*/
class BudibaseEmitter extends EventEmitter {
class BudibaseEmitter extends EventEmitter implements ContextEmitter {
emitRow({
eventName,
appId,
Expand All @@ -21,17 +27,17 @@ class BudibaseEmitter extends EventEmitter {
oldRow,
user,
}: {
eventName: string
eventName: EventType.ROW_SAVE | EventType.ROW_DELETE | EventType.ROW_UPDATE
appId: string
row: Row
table?: Table
oldRow?: Row
user: User
user: UserBindings
}) {
rowEmission({ emitter: this, eventName, appId, row, table, oldRow, user })
}

emitTable(eventName: string, appId: string, table?: Table) {
emitTable(eventName: EventType, appId: string, table?: Table) {
tableEmission({ emitter: this, eventName, appId, table })
}

Expand Down
6 changes: 3 additions & 3 deletions packages/server/src/events/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Table, Row, User } from "@budibase/types"
import { Table, Row, UserBindings } from "@budibase/types"
import BudibaseEmitter from "./BudibaseEmitter"

type BBEventOpts = {
Expand All @@ -9,7 +9,7 @@ type BBEventOpts = {
row?: Row
oldRow?: Row
metadata?: any
user?: User
user?: UserBindings
}

interface BBEventTable extends Table {
Expand All @@ -25,7 +25,7 @@ type BBEvent = {
id?: string
revision?: string
metadata?: any
user?: User
user?: UserBindings
}

export function rowEmission({
Expand Down
8 changes: 8 additions & 0 deletions packages/types/src/core/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const enum EventType {
ROW_SAVE = "row:save",
ROW_UPDATE = "row:update",
ROW_DELETE = "row:delete",
TABLE_SAVE = "table:save",
TABLE_UPDATED = "table:updated",
TABLE_DELETE = "table:delete",
}
1 change: 1 addition & 0 deletions packages/types/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./installation"
export * from "./events"
42 changes: 41 additions & 1 deletion packages/types/src/sdk/koa.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { Context, Request } from "koa"
import { User, Role, UserRoles, Account, ConfigType } from "../documents"
import {
User,
Role,
UserRoles,
Account,
ConfigType,
Row,
Table,
UserBindings,
} from "../documents"
import { FeatureFlag, License } from "../sdk"
import { Files } from "formidable"
import { EventType } from "../core"

export interface ContextUser extends Omit<User, "roles"> {
globalId?: string
Expand Down Expand Up @@ -40,6 +50,7 @@ export interface UserCtx<RequestBody = any, ResponseBody = any>
extends Ctx<RequestBody, ResponseBody> {
user: ContextUser
roleId?: string
eventEmitter?: ContextEmitter
}

/**
Expand All @@ -49,3 +60,32 @@ export interface UserCtx<RequestBody = any, ResponseBody = any>
export interface BBContext extends Ctx {
user?: ContextUser
}

export interface ContextEmitter {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really nice, I was actually confused about this the other day when trying to track down some emitter issues - the fact we had the AutomationEmitter and the standard emitter etc. Really nice stuff!

emitRow(values: {
eventName: EventType.ROW_SAVE
appId: string
row: Row
table: Table
user: UserBindings
}): void
emitRow(values: {
eventName: EventType.ROW_UPDATE
appId: string
row: Row
table: Table
oldRow: Row
user: UserBindings
}): void
emitRow(values: {
eventName: EventType.ROW_DELETE
appId: string
row: Row
user: UserBindings
}): void
emitTable(
eventName: EventType.TABLE_SAVE | EventType.TABLE_DELETE,
appId: string,
table?: Table
): void
}
Loading