1
0
Fork 0
mirror of synced 2024-06-28 02:50:50 +12:00
budibase/packages/server/src/api/controllers/screen.js

123 lines
3 KiB
JavaScript
Raw Normal View History

const {
getScreenParams,
generateScreenID,
getPluginParams,
DocumentTypes,
} = require("../../db/utils")
const { AccessController } = require("@budibase/backend-core/roles")
const { getAppDB } = require("@budibase/backend-core/context")
2022-04-08 21:55:39 +12:00
const { events } = require("@budibase/backend-core")
const { getGlobalDB } = require("@budibase/backend-core/tenancy")
import { updateAppPackage } from "./application"
2021-05-04 22:32:22 +12:00
exports.fetch = async ctx => {
const db = getAppDB()
2020-11-04 05:27:28 +13:00
const screens = (
await db.allDocs(
getScreenParams(null, {
include_docs: true,
})
)
2021-05-04 22:32:22 +12:00
).rows.map(element => element.doc)
ctx.body = await new AccessController().checkScreensAccess(
screens,
ctx.user.role._id
2020-11-04 05:27:28 +13:00
)
}
2021-05-04 22:32:22 +12:00
exports.save = async ctx => {
const db = getAppDB()
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)
// Find any custom components being used
let pluginNames = []
findPlugins(screen.props, pluginNames)
if (pluginNames.length) {
const globalDB = getGlobalDB()
const pluginsResponse = await globalDB.allDocs(
getPluginParams(null, {
include_docs: true,
})
)
const requiredPlugins = pluginsResponse.rows
.map(row => row.doc)
.filter(plugin => {
return (
plugin.schema.type === "component" &&
pluginNames.includes(`plugin/${plugin.name}/${plugin.version}`)
)
})
// Update the app metadata
const application = await db.get(DocumentTypes.APP_METADATA)
let usedPlugins = application.usedPlugins || []
let pluginAdded = false
requiredPlugins.forEach(plugin => {
if (!usedPlugins.find(x => x._id === plugin._id)) {
pluginAdded = true
usedPlugins.push({
_id: plugin._id,
name: plugin.name,
version: plugin.version,
jsUrl: plugin.jsUrl,
})
}
})
if (pluginAdded) {
console.log("plugin added! new plugins", usedPlugins)
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.`
ctx.body = {
...screen,
_id: response.id,
_rev: response.rev,
}
}
2021-05-04 22:32:22 +12:00
exports.destroy = async ctx => {
const db = 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)
ctx.body = {
message: "Screen deleted successfully",
}
ctx.status = 200
}
const findPlugins = (component, foundPlugins) => {
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))
}