From eaa38c5c2d4ee91302b61c9124cd489081e628ea Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 19 Jul 2024 10:56:36 +0200 Subject: [PATCH] Return automationid from row action api --- .../src/api/controllers/rowAction/crud.ts | 17 +++++-- .../src/api/routes/tests/rowAction.spec.ts | 46 +++++++++---------- packages/server/src/sdk/app/rowActions.ts | 10 ++-- .../src/tests/utilities/api/automation.ts | 17 +++++++ .../server/src/tests/utilities/api/index.ts | 3 ++ packages/types/src/api/web/app/rowAction.ts | 1 + 6 files changed, 63 insertions(+), 31 deletions(-) create mode 100644 packages/server/src/tests/utilities/api/automation.ts diff --git a/packages/server/src/api/controllers/rowAction/crud.ts b/packages/server/src/api/controllers/rowAction/crud.ts index 640bc35378..bd5326d957 100644 --- a/packages/server/src/api/controllers/rowAction/crud.ts +++ b/packages/server/src/api/controllers/rowAction/crud.ts @@ -31,7 +31,12 @@ export async function find(ctx: Ctx) { actions: Object.entries(actions).reduce>( (acc, [key, action]) => ({ ...acc, - [key]: { id: key, tableId: table._id!, ...action }, + [key]: { + id: key, + tableId: table._id!, + name: action.name, + automationId: action.automationId, + }, }), {} ), @@ -50,7 +55,9 @@ export async function create( ctx.body = { tableId: table._id!, - ...createdAction, + id: createdAction.id, + name: createdAction.name, + automationId: createdAction.automationId, } ctx.status = 201 } @@ -61,13 +68,15 @@ export async function update( const table = await getTable(ctx) const { actionId } = ctx.params - const actions = await sdk.rowActions.update(table._id!, actionId, { + const action = await sdk.rowActions.update(table._id!, actionId, { name: ctx.request.body.name, }) ctx.body = { tableId: table._id!, - ...actions, + id: action.id, + name: action.name, + automationId: action.automationId, } } diff --git a/packages/server/src/api/routes/tests/rowAction.spec.ts b/packages/server/src/api/routes/tests/rowAction.spec.ts index 4df9f630c5..a77f6bdd1a 100644 --- a/packages/server/src/api/routes/tests/rowAction.spec.ts +++ b/packages/server/src/api/routes/tests/rowAction.spec.ts @@ -9,6 +9,9 @@ import { import * as setup from "./utilities" import { generator } from "@budibase/backend-core/tests" +const expectAutomationId = () => + expect.stringMatching(`^${DocumentType.AUTOMATION}_.+`) + describe("/rowsActions", () => { const config = setup.getConfig() @@ -83,20 +86,19 @@ describe("/rowsActions", () => { }) expect(res).toEqual({ + name: rowAction.name, id: expect.stringMatching(/^row_action_\w+/), tableId: tableId, - ...rowAction, + automationId: expectAutomationId(), }) expect(await config.api.rowAction.find(tableId)).toEqual({ actions: { [res.id]: { - ...rowAction, + name: rowAction.name, id: res.id, tableId: tableId, - automationId: expect.stringMatching( - `^${DocumentType.AUTOMATION}_.+` - ), + automationId: expectAutomationId(), }, }, }) @@ -104,19 +106,13 @@ describe("/rowsActions", () => { it("trims row action names", async () => { const name = " action name " - const res = await createRowAction( - tableId, - { name }, - { - status: 201, - } - ) + const res = await createRowAction(tableId, { name }, { status: 201 }) - expect(res).toEqual({ - id: expect.stringMatching(/^row_action_\w+/), - tableId: tableId, + expect(res).toEqual( + expect.objectContaining({ name: "action name", }) + ) expect(await config.api.rowAction.find(tableId)).toEqual({ actions: { @@ -137,19 +133,19 @@ describe("/rowsActions", () => { expect(await config.api.rowAction.find(tableId)).toEqual({ actions: { [responses[0].id]: { - ...rowActions[0], + name: rowActions[0].name, id: responses[0].id, tableId, automationId: expect.any(String), }, [responses[1].id]: { - ...rowActions[1], + name: rowActions[1].name, id: responses[1].id, tableId, automationId: expect.any(String), }, [responses[2].id]: { - ...rowActions[2], + name: rowActions[2].name, id: responses[2].id, tableId, automationId: expect.any(String), @@ -174,7 +170,7 @@ describe("/rowsActions", () => { it("ignores not valid row action data", async () => { const rowAction = createRowActionRequest() const dirtyRowAction = { - ...rowAction, + name: rowAction.name, id: generator.guid(), valueToIgnore: generator.string(), } @@ -183,17 +179,18 @@ describe("/rowsActions", () => { }) expect(res).toEqual({ + name: rowAction.name, id: expect.any(String), tableId, - ...rowAction, + automationId: expectAutomationId(), }) expect(await config.api.rowAction.find(tableId)).toEqual({ actions: { [res.id]: { + name: rowAction.name, id: res.id, tableId: tableId, - ...rowAction, automationId: expect.any(String), }, }, @@ -287,7 +284,6 @@ describe("/rowsActions", () => { const updatedName = generator.string() const res = await config.api.rowAction.update(tableId, actionId, { - ...actionData, name: updatedName, }) @@ -295,14 +291,17 @@ describe("/rowsActions", () => { id: actionId, tableId, name: updatedName, + automationId: actionData.automationId, }) expect(await config.api.rowAction.find(tableId)).toEqual( expect.objectContaining({ actions: expect.objectContaining({ [actionId]: { - ...actionData, name: updatedName, + id: actionData.id, + tableId: actionData.tableId, + automationId: actionData.automationId, }, }), }) @@ -319,7 +318,6 @@ describe("/rowsActions", () => { ) const res = await config.api.rowAction.update(tableId, rowAction.id, { - ...rowAction, name: " action name ", }) diff --git a/packages/server/src/sdk/app/rowActions.ts b/packages/server/src/sdk/app/rowActions.ts index 80266fe572..dfb6975c48 100644 --- a/packages/server/src/sdk/app/rowActions.ts +++ b/packages/server/src/sdk/app/rowActions.ts @@ -91,7 +91,7 @@ export async function create(tableId: string, rowAction: { name: string }) { return { id: newId, - ...action, + ...doc.actions[newId], } } @@ -135,20 +135,24 @@ export async function update( return { id: rowActionId, - ...action, + ...actionsDoc.actions[rowActionId], } } export async function remove(tableId: string, rowActionId: string) { const actionsDoc = await get(tableId) - if (!actionsDoc.actions[rowActionId]) { + const rowAction = actionsDoc.actions[rowActionId] + if (!rowAction) { throw new HTTPError( `Row action '${rowActionId}' not found in '${tableId}'`, 400 ) } + const { automationId } = rowAction + const automation = await automations.get(automationId) + await automations.remove(automation._id, automation._rev) delete actionsDoc.actions[rowActionId] const db = context.getAppDB() diff --git a/packages/server/src/tests/utilities/api/automation.ts b/packages/server/src/tests/utilities/api/automation.ts new file mode 100644 index 0000000000..9620e2011c --- /dev/null +++ b/packages/server/src/tests/utilities/api/automation.ts @@ -0,0 +1,17 @@ +import { Automation } from "@budibase/types" +import { Expectations, TestAPI } from "./base" + +export class AutomationAPI extends TestAPI { + get = async ( + automationId: string, + expectations?: Expectations + ): Promise => { + const result = await this._get( + `/api/automations/${automationId}`, + { + expectations, + } + ) + return result + } +} diff --git a/packages/server/src/tests/utilities/api/index.ts b/packages/server/src/tests/utilities/api/index.ts index a19b68a872..36a6ed0222 100644 --- a/packages/server/src/tests/utilities/api/index.ts +++ b/packages/server/src/tests/utilities/api/index.ts @@ -14,6 +14,7 @@ import { QueryAPI } from "./query" import { RoleAPI } from "./role" import { TemplateAPI } from "./template" import { RowActionAPI } from "./rowAction" +import { AutomationAPI } from "./automation" export default class API { table: TableAPI @@ -31,6 +32,7 @@ export default class API { roles: RoleAPI templates: TemplateAPI rowAction: RowActionAPI + automation: AutomationAPI constructor(config: TestConfiguration) { this.table = new TableAPI(config) @@ -48,5 +50,6 @@ export default class API { this.roles = new RoleAPI(config) this.templates = new TemplateAPI(config) this.rowAction = new RowActionAPI(config) + this.automation = new AutomationAPI(config) } } diff --git a/packages/types/src/api/web/app/rowAction.ts b/packages/types/src/api/web/app/rowAction.ts index ba95ba6b95..305c42b473 100644 --- a/packages/types/src/api/web/app/rowAction.ts +++ b/packages/types/src/api/web/app/rowAction.ts @@ -7,6 +7,7 @@ export interface UpdateRowActionRequest extends RowActionData {} export interface RowActionResponse extends RowActionData { id: string tableId: string + automationId: string } export interface RowActionsResponse {