Implement delete

This commit is contained in:
Adria Navarro 2024-07-11 17:33:40 +02:00
parent ba2d6fd73b
commit 2035713b9c
4 changed files with 83 additions and 2 deletions

View File

@ -69,6 +69,10 @@ export async function update(
}
}
export function remove() {
throw new Error("Function not implemented.")
export async function remove(ctx: Ctx<void, void>) {
const table = await getTable(ctx)
const { actionId } = ctx.params
await sdk.rowActions.remove(table._id!, actionId)
ctx.status = 204
}

View File

@ -251,4 +251,49 @@ describe("/rowsActions", () => {
)
})
})
describe("delete", () => {
unauthorisedTests()
it("can delete existing actions", async () => {
const actions: RowActionResponse[] = []
for (const rowAction of createRowActionRequests(3)) {
actions.push(await createRowAction(tableId, rowAction))
}
const actionToDelete = _.sample(actions)!
await config.api.rowAction.delete(tableId, actionToDelete.id, {
status: 204,
})
expect(await config.api.rowAction.find(tableId)).toEqual(
expect.objectContaining({
actions: actions
.filter(a => a.id !== actionToDelete.id)
.reduce((acc, c) => ({ ...acc, [c.id]: expect.any(Object) }), {}),
})
)
})
it("throws Bad Request when trying to delete by a non-existing id", async () => {
await createRowAction(tableId, createRowActionRequest())
await config.api.rowAction.delete(tableId, generator.guid(), {
status: 400,
})
})
it("throws Bad Request when trying to delete by a via another table id", async () => {
const otherTable = await config.api.table.save(
setup.structures.basicTable()
)
await createRowAction(otherTable._id!, createRowActionRequest())
const action = await createRowAction(tableId, createRowActionRequest())
await config.api.rowAction.delete(otherTable._id!, action.id, {
status: 400,
})
})
})
})

View File

@ -43,6 +43,7 @@ export async function docExists(tableId: string) {
const result = await db.exists(rowActionsId)
return result
}
export async function update(
tableId: string,
rowActionId: string,
@ -66,3 +67,19 @@ export async function update(
...rowAction,
}
}
export async function remove(tableId: string, rowActionId: string) {
const actionsDoc = await get(tableId)
if (!actionsDoc.actions[rowActionId]) {
throw new HTTPError(
`Row action '${rowActionId}' not found in '${tableId}'`,
400
)
}
delete actionsDoc.actions[rowActionId]
const db = context.getAppDB()
await db.put(actionsDoc)
}

View File

@ -52,4 +52,19 @@ export class RowActionAPI extends TestAPI {
}
)
}
delete = async (
tableId: string,
rowActionId: string,
expectations?: Expectations,
config?: { publicUser?: boolean }
) => {
return await this._delete<RowActionResponse>(
`/api/tables/${tableId}/actions/${rowActionId}`,
{
expectations,
...config,
}
)
}
}