diff --git a/packages/server/src/api/controllers/rowAction/crud.ts b/packages/server/src/api/controllers/rowAction/crud.ts index c946515150..640bc35378 100644 --- a/packages/server/src/api/controllers/rowAction/crud.ts +++ b/packages/server/src/api/controllers/rowAction/crud.ts @@ -21,17 +21,22 @@ export async function find(ctx: Ctx) { if (!(await sdk.rowActions.docExists(table._id!))) { ctx.body = { - tableId: table._id!, actions: {}, } return } - const actions = await sdk.rowActions.get(table._id!) - ctx.body = { - tableId: table._id!, - ...actions, + const { actions } = await sdk.rowActions.get(table._id!) + const result: RowActionsResponse = { + actions: Object.entries(actions).reduce>( + (acc, [key, action]) => ({ + ...acc, + [key]: { id: key, tableId: table._id!, ...action }, + }), + {} + ), } + ctx.body = result } export async function create( @@ -39,10 +44,9 @@ export async function create( ) { const table = await getTable(ctx) - const createdAction = await sdk.rowActions.create( - table._id!, - ctx.request.body - ) + const createdAction = await sdk.rowActions.create(table._id!, { + name: ctx.request.body.name, + }) ctx.body = { tableId: table._id!, @@ -57,11 +61,9 @@ export async function update( const table = await getTable(ctx) const { actionId } = ctx.params - const actions = await sdk.rowActions.update( - table._id!, - actionId, - ctx.request.body - ) + const actions = await sdk.rowActions.update(table._id!, actionId, { + name: ctx.request.body.name, + }) ctx.body = { tableId: table._id!, diff --git a/packages/server/src/api/routes/rowAction.ts b/packages/server/src/api/routes/rowAction.ts index 18a87cd677..3ec00dff4d 100644 --- a/packages/server/src/api/routes/rowAction.ts +++ b/packages/server/src/api/routes/rowAction.ts @@ -11,6 +11,8 @@ export function rowActionValidator() { return middleware.joiValidator.body( Joi.object({ name: Joi.string().required(), + id: Joi.optional(), + tableId: Joi.optional(), }) ) } diff --git a/packages/server/src/api/routes/tests/rowAction.spec.ts b/packages/server/src/api/routes/tests/rowAction.spec.ts index 2af3be00b5..976dc950e4 100644 --- a/packages/server/src/api/routes/tests/rowAction.spec.ts +++ b/packages/server/src/api/routes/tests/rowAction.spec.ts @@ -101,14 +101,13 @@ describe("/rowsActions", () => { }) expect(await config.api.rowAction.find(tableId)).toEqual({ - _id: `ra_${tableId}`, - _rev: expect.stringMatching(/^1-\w+/), - tableId: tableId, actions: { - [res.id]: rowAction, + [res.id]: { + ...rowAction, + id: res.id, + tableId: tableId, + }, }, - createdAt: new Date().toISOString(), - updatedAt: new Date().toISOString(), }) }) @@ -120,16 +119,11 @@ describe("/rowsActions", () => { } expect(await config.api.rowAction.find(tableId)).toEqual({ - _id: `ra_${tableId}`, - _rev: expect.stringMatching(/^3-\w+/), actions: { - [responses[0].id]: rowActions[0], - [responses[1].id]: rowActions[1], - [responses[2].id]: rowActions[2], + [responses[0].id]: { ...rowActions[0], id: responses[0].id, tableId }, + [responses[1].id]: { ...rowActions[1], id: responses[1].id, tableId }, + [responses[2].id]: { ...rowActions[2], id: responses[2].id, tableId }, }, - tableId: tableId, - createdAt: new Date().toISOString(), - updatedAt: new Date().toISOString(), }) }) @@ -162,26 +156,20 @@ describe("/rowsActions", () => { await createRowAction(otherTable._id!, createRowActionRequest()) const response = await config.api.rowAction.find(tableId) - expect(response).toEqual( - expect.objectContaining({ - tableId, - actions: { - [rowActions[0].id]: expect.any(Object), - [rowActions[1].id]: expect.any(Object), - [rowActions[2].id]: expect.any(Object), - }, - }) - ) + expect(response).toEqual({ + actions: { + [rowActions[0].id]: expect.any(Object), + [rowActions[1].id]: expect.any(Object), + [rowActions[2].id]: expect.any(Object), + }, + }) }) it("returns empty for tables without row actions", async () => { const response = await config.api.rowAction.find(tableId) - expect(response).toEqual( - expect.objectContaining({ - tableId, - actions: {}, - }) - ) + expect(response).toEqual({ + actions: {}, + }) }) }) @@ -209,7 +197,6 @@ describe("/rowsActions", () => { expect(res).toEqual({ id: actionId, tableId, - ...actionData, name: updatedName, }) diff --git a/packages/types/src/api/web/app/rowAction.ts b/packages/types/src/api/web/app/rowAction.ts index 72662fcb49..ba95ba6b95 100644 --- a/packages/types/src/api/web/app/rowAction.ts +++ b/packages/types/src/api/web/app/rowAction.ts @@ -1,4 +1,8 @@ +interface RowActionData { + name: string +} export interface CreateRowActionRequest extends RowActionData {} +export interface UpdateRowActionRequest extends RowActionData {} export interface RowActionResponse extends RowActionData { id: string @@ -6,12 +10,5 @@ export interface RowActionResponse extends RowActionData { } export interface RowActionsResponse { - tableId: string - actions: Record + actions: Record } - -interface RowActionData { - name: string -} - -export interface UpdateRowActionRequest extends RowActionData {}