Refactor
This commit is contained in:
parent
de3eae2d47
commit
5f3dcda73a
|
@ -2,6 +2,7 @@ import { context, HTTPError, utils } from "@budibase/backend-core"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
AutomationTriggerStepId,
|
AutomationTriggerStepId,
|
||||||
|
RowActionData,
|
||||||
SEPARATOR,
|
SEPARATOR,
|
||||||
TableRowActions,
|
TableRowActions,
|
||||||
VirtualDocumentType,
|
VirtualDocumentType,
|
||||||
|
@ -101,26 +102,51 @@ export async function docExists(tableId: string) {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function update(
|
function getRowAction(
|
||||||
tableId: string,
|
rowActions: TableRowActions,
|
||||||
rowActionId: string,
|
rowActionId: string
|
||||||
rowAction: { name: string }
|
): RowActionData {
|
||||||
) {
|
if (!rowActions.actions[rowActionId]) {
|
||||||
const action = { name: rowAction.name.trim() }
|
|
||||||
const actionsDoc = await get(tableId)
|
|
||||||
|
|
||||||
if (!actionsDoc.actions[rowActionId]) {
|
|
||||||
throw new HTTPError(
|
throw new HTTPError(
|
||||||
`Row action '${rowActionId}' not found in '${tableId}'`,
|
`Row action '${rowActionId}' not found in '${rowActions.tableId}'`,
|
||||||
400
|
400
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
return rowActions.actions[rowActionId]
|
||||||
|
}
|
||||||
|
|
||||||
ensureUniqueAndThrow(actionsDoc, action.name, rowActionId)
|
export async function update(
|
||||||
|
tableId: string,
|
||||||
|
rowActionId: string,
|
||||||
|
rowActionData: { name: string }
|
||||||
|
) {
|
||||||
|
rowActionData.name = rowActionData.name.trim()
|
||||||
|
|
||||||
actionsDoc.actions[rowActionId] = {
|
const actionsDoc = await get(tableId)
|
||||||
...actionsDoc.actions[rowActionId],
|
ensureUniqueAndThrow(actionsDoc, rowActionData.name, rowActionId)
|
||||||
...action,
|
|
||||||
|
const rowAction = getRowAction(actionsDoc, rowActionId)
|
||||||
|
rowAction.name = rowActionData.name
|
||||||
|
|
||||||
|
const db = context.getAppDB()
|
||||||
|
await db.put(actionsDoc)
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: rowActionId,
|
||||||
|
...rowAction,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function setViewPermission(
|
||||||
|
tableId: string,
|
||||||
|
rowActionId: string,
|
||||||
|
viewId: string
|
||||||
|
) {
|
||||||
|
const actionsDoc = await get(tableId)
|
||||||
|
|
||||||
|
const rowAction = getRowAction(actionsDoc, rowActionId)
|
||||||
|
rowAction.permissions.views[viewId] = {
|
||||||
|
runAllowed: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
|
@ -128,20 +154,14 @@ export async function update(
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: rowActionId,
|
id: rowActionId,
|
||||||
...actionsDoc.actions[rowActionId],
|
...rowAction,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function remove(tableId: string, rowActionId: string) {
|
export async function remove(tableId: string, rowActionId: string) {
|
||||||
const actionsDoc = await get(tableId)
|
const actionsDoc = await get(tableId)
|
||||||
|
|
||||||
const rowAction = actionsDoc.actions[rowActionId]
|
const rowAction = getRowAction(actionsDoc, rowActionId)
|
||||||
if (!rowAction) {
|
|
||||||
throw new HTTPError(
|
|
||||||
`Row action '${rowActionId}' not found in '${tableId}'`,
|
|
||||||
400
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const { automationId } = rowAction
|
const { automationId } = rowAction
|
||||||
const automation = await automations.get(automationId)
|
const automation = await automations.get(automationId)
|
||||||
|
|
|
@ -3,15 +3,14 @@ import { Document } from "../document"
|
||||||
export interface TableRowActions extends Document {
|
export interface TableRowActions extends Document {
|
||||||
_id: string
|
_id: string
|
||||||
tableId: string
|
tableId: string
|
||||||
actions: Record<
|
actions: Record<string, RowActionData>
|
||||||
string,
|
}
|
||||||
{
|
|
||||||
name: string
|
export interface RowActionData {
|
||||||
automationId: string
|
name: string
|
||||||
permissions: {
|
automationId: string
|
||||||
table: { runAllowed: boolean }
|
permissions: {
|
||||||
views: Record<string, { runAllowed: boolean }>
|
table: { runAllowed: boolean }
|
||||||
}
|
views: Record<string, { runAllowed: boolean }>
|
||||||
}
|
}
|
||||||
>
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue