Dry code
This commit is contained in:
parent
2aa71ab419
commit
6d3006e80e
|
@ -1,8 +1,6 @@
|
||||||
import { context, HTTPError, utils } from "@budibase/backend-core"
|
import { context, HTTPError, utils } from "@budibase/backend-core"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
AutomationTriggerStepId,
|
AutomationTriggerStepId,
|
||||||
RowActionData,
|
|
||||||
SEPARATOR,
|
SEPARATOR,
|
||||||
TableRowActions,
|
TableRowActions,
|
||||||
VirtualDocumentType,
|
VirtualDocumentType,
|
||||||
|
@ -102,17 +100,31 @@ export async function docExists(tableId: string) {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRowAction(
|
async function updateDoc(
|
||||||
rowActions: TableRowActions,
|
tableId: string,
|
||||||
rowActionId: string
|
rowActionId: string,
|
||||||
): RowActionData {
|
transformer: (
|
||||||
if (!rowActions.actions[rowActionId]) {
|
tableRowActions: TableRowActions
|
||||||
|
) => TableRowActions | Promise<TableRowActions>
|
||||||
|
) {
|
||||||
|
const actionsDoc = await get(tableId)
|
||||||
|
const rowAction = actionsDoc?.actions[rowActionId]
|
||||||
|
if (!rowAction) {
|
||||||
throw new HTTPError(
|
throw new HTTPError(
|
||||||
`Row action '${rowActionId}' not found in '${rowActions.tableId}'`,
|
`Row action '${rowActionId}' not found in '${tableId}'`,
|
||||||
400
|
400
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return rowActions.actions[rowActionId]
|
|
||||||
|
const updated = await transformer(actionsDoc)
|
||||||
|
|
||||||
|
const db = context.getAppDB()
|
||||||
|
await db.put(updated)
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: rowActionId,
|
||||||
|
...updated.actions[rowActionId],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function update(
|
export async function update(
|
||||||
|
@ -120,21 +132,13 @@ export async function update(
|
||||||
rowActionId: string,
|
rowActionId: string,
|
||||||
rowActionData: { name: string }
|
rowActionData: { name: string }
|
||||||
) {
|
) {
|
||||||
rowActionData.name = rowActionData.name.trim()
|
const newName = rowActionData.name.trim()
|
||||||
|
|
||||||
const actionsDoc = await get(tableId)
|
return await updateDoc(tableId, rowActionId, actionsDoc => {
|
||||||
ensureUniqueAndThrow(actionsDoc, rowActionData.name, rowActionId)
|
ensureUniqueAndThrow(actionsDoc, newName, rowActionId)
|
||||||
|
actionsDoc.actions[rowActionId].name = newName
|
||||||
const rowAction = getRowAction(actionsDoc, rowActionId)
|
return actionsDoc
|
||||||
rowAction.name = rowActionData.name
|
})
|
||||||
|
|
||||||
const db = context.getAppDB()
|
|
||||||
await db.put(actionsDoc)
|
|
||||||
|
|
||||||
return {
|
|
||||||
id: rowActionId,
|
|
||||||
...rowAction,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function setViewPermission(
|
export async function setViewPermission(
|
||||||
|
@ -142,20 +146,12 @@ export async function setViewPermission(
|
||||||
rowActionId: string,
|
rowActionId: string,
|
||||||
viewId: string
|
viewId: string
|
||||||
) {
|
) {
|
||||||
const actionsDoc = await get(tableId)
|
return await updateDoc(tableId, rowActionId, async actionsDoc => {
|
||||||
|
actionsDoc.actions[rowActionId].permissions.views[viewId] = {
|
||||||
const rowAction = getRowAction(actionsDoc, rowActionId)
|
runAllowed: true,
|
||||||
rowAction.permissions.views[viewId] = {
|
}
|
||||||
runAllowed: true,
|
return actionsDoc
|
||||||
}
|
})
|
||||||
|
|
||||||
const db = context.getAppDB()
|
|
||||||
await db.put(actionsDoc)
|
|
||||||
|
|
||||||
return {
|
|
||||||
id: rowActionId,
|
|
||||||
...rowAction,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function unsetViewPermission(
|
export async function unsetViewPermission(
|
||||||
|
@ -163,32 +159,21 @@ export async function unsetViewPermission(
|
||||||
rowActionId: string,
|
rowActionId: string,
|
||||||
viewId: string
|
viewId: string
|
||||||
) {
|
) {
|
||||||
const actionsDoc = await get(tableId)
|
return await updateDoc(tableId, rowActionId, async actionsDoc => {
|
||||||
|
delete actionsDoc.actions[rowActionId].permissions.views[viewId]
|
||||||
const rowAction = getRowAction(actionsDoc, rowActionId)
|
return actionsDoc
|
||||||
delete rowAction.permissions.views[viewId]
|
})
|
||||||
|
|
||||||
const db = context.getAppDB()
|
|
||||||
await db.put(actionsDoc)
|
|
||||||
|
|
||||||
return {
|
|
||||||
id: rowActionId,
|
|
||||||
...rowAction,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function remove(tableId: string, rowActionId: string) {
|
export async function remove(tableId: string, rowActionId: string) {
|
||||||
const actionsDoc = await get(tableId)
|
return await updateDoc(tableId, rowActionId, async actionsDoc => {
|
||||||
|
const { automationId } = actionsDoc.actions[rowActionId]
|
||||||
|
const automation = await automations.get(automationId)
|
||||||
|
await automations.remove(automation._id, automation._rev)
|
||||||
|
|
||||||
const rowAction = getRowAction(actionsDoc, rowActionId)
|
delete actionsDoc.actions[rowActionId]
|
||||||
|
return actionsDoc
|
||||||
const { automationId } = rowAction
|
})
|
||||||
const automation = await automations.get(automationId)
|
|
||||||
await automations.remove(automation._id, automation._rev)
|
|
||||||
delete actionsDoc.actions[rowActionId]
|
|
||||||
|
|
||||||
const db = context.getAppDB()
|
|
||||||
await db.put(actionsDoc)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function run(tableId: any, rowActionId: any, rowId: string) {
|
export async function run(tableId: any, rowActionId: any, rowId: string) {
|
||||||
|
|
Loading…
Reference in New Issue