2022-09-16 05:43:07 +12:00
|
|
|
import { getScreenParams, generateScreenID, DocumentType } from "../../db/utils"
|
|
|
|
import {
|
|
|
|
events,
|
|
|
|
context,
|
|
|
|
tenancy,
|
|
|
|
db as dbCore,
|
|
|
|
roles,
|
|
|
|
} from "@budibase/backend-core"
|
|
|
|
import { updateAppPackage } from "./application"
|
|
|
|
import { Plugin, ScreenProps } from "@budibase/types"
|
2020-10-14 05:40:28 +13:00
|
|
|
|
2022-09-16 05:43:07 +12:00
|
|
|
exports.fetch = async (ctx: any) => {
|
|
|
|
const db = context.getAppDB()
|
2020-11-04 05:27:28 +13:00
|
|
|
|
2020-11-17 07:04:44 +13:00
|
|
|
const screens = (
|
|
|
|
await db.allDocs(
|
|
|
|
getScreenParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2022-09-16 05:43:07 +12:00
|
|
|
).rows.map((el: any) => el.doc)
|
2020-11-17 07:04:44 +13:00
|
|
|
|
2022-09-16 05:43:07 +12:00
|
|
|
ctx.body = await new roles.AccessController().checkScreensAccess(
|
2020-11-17 07:04:44 +13:00
|
|
|
screens,
|
2020-12-03 02:20:56 +13:00
|
|
|
ctx.user.role._id
|
2020-11-04 05:27:28 +13:00
|
|
|
)
|
2020-10-14 05:40:28 +13:00
|
|
|
}
|
|
|
|
|
2022-09-16 05:43:07 +12:00
|
|
|
exports.save = async (ctx: any) => {
|
|
|
|
const db = context.getAppDB()
|
2020-11-24 05:56:35 +13:00
|
|
|
let screen = ctx.request.body
|
2020-11-04 05:27:28 +13:00
|
|
|
|
2022-04-08 21:55:39 +12:00
|
|
|
let eventFn
|
2020-11-04 05:27:28 +13:00
|
|
|
if (!screen._id) {
|
2020-11-21 06:47:13 +13:00
|
|
|
screen._id = generateScreenID()
|
2022-04-08 21:55:39 +12:00
|
|
|
eventFn = events.screen.created
|
2020-11-04 05:27:28 +13:00
|
|
|
}
|
2022-04-08 21:55:39 +12:00
|
|
|
|
2020-11-04 05:27:28 +13:00
|
|
|
const response = await db.put(screen)
|
|
|
|
|
2022-08-12 02:31:21 +12:00
|
|
|
// Find any custom components being used
|
2022-09-16 05:43:07 +12:00
|
|
|
let pluginNames: string[] = []
|
2022-08-12 04:04:47 +12:00
|
|
|
let pluginAdded = false
|
2022-08-12 02:31:21 +12:00
|
|
|
findPlugins(screen.props, pluginNames)
|
|
|
|
if (pluginNames.length) {
|
2022-09-16 05:43:07 +12:00
|
|
|
const globalDB = tenancy.getGlobalDB()
|
2022-08-12 02:31:21 +12:00
|
|
|
const pluginsResponse = await globalDB.allDocs(
|
2022-09-16 05:43:07 +12:00
|
|
|
dbCore.getPluginParams(null, {
|
2022-08-12 02:31:21 +12:00
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
const requiredPlugins = pluginsResponse.rows
|
2022-09-16 05:43:07 +12:00
|
|
|
.map((row: any) => row.doc)
|
|
|
|
.filter((plugin: Plugin) => {
|
2022-08-12 02:31:21 +12:00
|
|
|
return (
|
|
|
|
plugin.schema.type === "component" &&
|
2022-09-03 06:58:54 +12:00
|
|
|
pluginNames.includes(`plugin/${plugin.name}`)
|
2022-08-12 02:31:21 +12:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Update the app metadata
|
2022-08-13 01:01:56 +12:00
|
|
|
const application = await db.get(DocumentType.APP_METADATA)
|
2022-08-12 02:31:21 +12:00
|
|
|
let usedPlugins = application.usedPlugins || []
|
|
|
|
|
2022-09-16 05:43:07 +12:00
|
|
|
requiredPlugins.forEach((plugin: Plugin) => {
|
|
|
|
if (!usedPlugins.find((x: Plugin) => x._id === plugin._id)) {
|
2022-08-12 02:31:21 +12:00
|
|
|
pluginAdded = true
|
|
|
|
usedPlugins.push({
|
|
|
|
_id: plugin._id,
|
|
|
|
name: plugin.name,
|
|
|
|
version: plugin.version,
|
|
|
|
jsUrl: plugin.jsUrl,
|
2022-09-03 06:58:54 +12:00
|
|
|
hash: plugin.hash,
|
2022-08-12 02:31:21 +12:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (pluginAdded) {
|
|
|
|
await updateAppPackage({ usedPlugins }, ctx.appId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 21:55:39 +12:00
|
|
|
if (eventFn) {
|
2022-05-24 09:14:44 +12:00
|
|
|
await eventFn(screen)
|
2022-04-08 21:55:39 +12:00
|
|
|
}
|
2020-11-04 05:27:28 +13:00
|
|
|
ctx.message = `Screen ${screen.name} saved.`
|
2020-12-04 01:08:33 +13:00
|
|
|
ctx.body = {
|
|
|
|
...screen,
|
|
|
|
_id: response.id,
|
|
|
|
_rev: response.rev,
|
2022-08-12 04:04:47 +12:00
|
|
|
pluginAdded,
|
2020-12-04 01:08:33 +13:00
|
|
|
}
|
2020-10-14 05:40:28 +13:00
|
|
|
}
|
|
|
|
|
2022-09-16 05:43:07 +12:00
|
|
|
exports.destroy = async (ctx: any) => {
|
|
|
|
const db = context.getAppDB()
|
2022-04-08 21:55:39 +12:00
|
|
|
const id = ctx.params.screenId
|
|
|
|
const screen = await db.get(id)
|
|
|
|
|
|
|
|
await db.remove(id, ctx.params.screenRev)
|
|
|
|
|
2022-05-24 09:14:44 +12:00
|
|
|
await events.screen.deleted(screen)
|
2021-03-11 00:47:39 +13:00
|
|
|
ctx.body = {
|
|
|
|
message: "Screen deleted successfully",
|
|
|
|
}
|
2020-11-05 05:13:50 +13:00
|
|
|
ctx.status = 200
|
2020-10-14 05:40:28 +13:00
|
|
|
}
|
2022-08-12 02:31:21 +12:00
|
|
|
|
2022-09-16 05:43:07 +12:00
|
|
|
const findPlugins = (component: ScreenProps, foundPlugins: string[]) => {
|
2022-08-12 02:31:21 +12:00
|
|
|
if (!component) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (component._component.startsWith("plugin")) {
|
|
|
|
if (!foundPlugins.includes(component._component)) {
|
|
|
|
foundPlugins.push(component._component)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!component._children || !component._children.length) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
component._children.forEach(child => findPlugins(child, foundPlugins))
|
|
|
|
}
|