2022-03-20 02:13:54 +01:00
|
|
|
import env from "../../environment"
|
|
|
|
import packageJson from "../../../package.json"
|
|
|
|
import {
|
2021-03-25 19:04:44 +01:00
|
|
|
createLinkView,
|
|
|
|
createRoutingView,
|
2021-03-26 00:42:50 +01:00
|
|
|
createAllSearchIndex,
|
2022-03-20 02:13:54 +01:00
|
|
|
} from "../../db/views/staticViews"
|
|
|
|
import {
|
2021-03-19 20:07:47 +01:00
|
|
|
getTemplateStream,
|
|
|
|
createApp,
|
|
|
|
deleteApp,
|
2022-03-20 02:13:54 +01:00
|
|
|
} from "../../utilities/fileSystem"
|
|
|
|
import {
|
2020-11-02 15:53:51 +01:00
|
|
|
generateAppID,
|
2020-11-23 15:07:18 +01:00
|
|
|
getLayoutParams,
|
2020-11-18 19:24:12 +01:00
|
|
|
getScreenParams,
|
2021-05-13 12:06:08 +02:00
|
|
|
generateDevAppID,
|
2022-08-11 14:50:05 +02:00
|
|
|
DocumentType,
|
2021-05-13 12:06:08 +02:00
|
|
|
AppStatus,
|
2022-03-20 02:13:54 +01:00
|
|
|
} from "../../db/utils"
|
2022-01-10 20:33:00 +01:00
|
|
|
const {
|
|
|
|
BUILTIN_ROLE_IDS,
|
|
|
|
AccessController,
|
|
|
|
} = require("@budibase/backend-core/roles")
|
2022-05-23 16:03:52 +02:00
|
|
|
const { CacheKeys, bustCache } = require("@budibase/backend-core/cache")
|
2021-10-22 15:34:20 +02:00
|
|
|
const {
|
|
|
|
getAllApps,
|
|
|
|
isDevAppID,
|
2022-01-31 18:42:51 +01:00
|
|
|
getProdAppID,
|
2021-10-22 15:34:20 +02:00
|
|
|
Replication,
|
2022-01-10 20:33:00 +01:00
|
|
|
} = require("@budibase/backend-core/db")
|
2022-03-20 02:13:54 +01:00
|
|
|
import { USERS_TABLE_SCHEMA } from "../../constants"
|
|
|
|
import { removeAppFromUserRoles } from "../../utilities/workerRequests"
|
|
|
|
import { clientLibraryPath, stringToReadStream } from "../../utilities"
|
2022-10-07 17:05:01 +02:00
|
|
|
import { getLocksById } from "../../utilities/redis"
|
2022-03-20 02:13:54 +01:00
|
|
|
import {
|
2021-07-08 13:56:54 +02:00
|
|
|
updateClientLibrary,
|
|
|
|
backupClientLibrary,
|
|
|
|
revertClientLibrary,
|
2022-03-20 02:13:54 +01:00
|
|
|
} from "../../utilities/fileSystem/clientLibrary"
|
2022-01-10 20:33:00 +01:00
|
|
|
const { getTenantId, isMultiTenant } = require("@budibase/backend-core/tenancy")
|
2022-03-20 02:13:54 +01:00
|
|
|
import { syncGlobalUsers } from "./user"
|
2022-01-10 20:33:00 +01:00
|
|
|
const { app: appCache } = require("@budibase/backend-core/cache")
|
2022-03-20 02:13:54 +01:00
|
|
|
import { cleanupAutomations } from "../../automations/utils"
|
2022-07-13 14:22:21 +02:00
|
|
|
import { context } from "@budibase/backend-core"
|
2022-07-04 16:44:47 +02:00
|
|
|
import { checkAppMetadata } from "../../automations/logging"
|
2022-03-20 02:13:54 +01:00
|
|
|
import { getUniqueRows } from "../../utilities/usageQuota/rows"
|
2022-10-07 17:05:01 +02:00
|
|
|
import { quotas, groups } from "@budibase/pro"
|
2022-06-13 11:51:29 +02:00
|
|
|
import { errors, events, migrations } from "@budibase/backend-core"
|
2022-09-22 00:12:38 +02:00
|
|
|
import { App, Layout, Screen, MigrationType } from "@budibase/types"
|
2022-08-24 22:35:24 +02:00
|
|
|
import { BASE_LAYOUT_PROP_IDS } from "../../constants/layouts"
|
2022-10-04 17:27:42 +02:00
|
|
|
import { enrichPluginURLs } from "../../utilities/plugins"
|
2020-11-02 15:53:51 +01:00
|
|
|
|
2021-01-18 13:36:49 +01:00
|
|
|
const URL_REGEX_SLASH = /\/|\\/g
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2020-11-18 19:24:12 +01:00
|
|
|
// utility function, need to do away with this
|
2022-01-27 19:18:31 +01:00
|
|
|
async function getLayouts() {
|
2022-07-13 14:22:21 +02:00
|
|
|
const db = context.getAppDB()
|
2020-11-23 16:46:26 +01:00
|
|
|
return (
|
|
|
|
await db.allDocs(
|
|
|
|
getLayoutParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2022-03-20 02:13:54 +01:00
|
|
|
).rows.map((row: any) => row.doc)
|
2020-11-23 16:46:26 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 19:18:31 +01:00
|
|
|
async function getScreens() {
|
2022-07-13 14:22:21 +02:00
|
|
|
const db = context.getAppDB()
|
2020-11-23 16:46:26 +01:00
|
|
|
return (
|
|
|
|
await db.allDocs(
|
|
|
|
getScreenParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2022-03-20 02:13:54 +01:00
|
|
|
).rows.map((row: any) => row.doc)
|
2020-11-23 16:46:26 +01:00
|
|
|
}
|
2020-11-18 19:24:12 +01:00
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
function getUserRoleId(ctx: any) {
|
2020-12-02 14:26:57 +01:00
|
|
|
return !ctx.user.role || !ctx.user.role._id
|
|
|
|
? BUILTIN_ROLE_IDS.PUBLIC
|
|
|
|
: ctx.user.role._id
|
2020-11-18 19:24:12 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
export const getAppUrl = (ctx: any) => {
|
2022-01-21 18:31:18 +01:00
|
|
|
// construct the url
|
2021-01-14 18:01:31 +01:00
|
|
|
let url
|
|
|
|
if (ctx.request.body.url) {
|
2022-01-21 18:31:18 +01:00
|
|
|
// if the url is provided, use that
|
2021-01-14 18:01:31 +01:00
|
|
|
url = encodeURI(ctx.request.body.url)
|
2022-01-28 14:27:19 +01:00
|
|
|
} else if (ctx.request.body.name) {
|
2022-01-21 18:31:18 +01:00
|
|
|
// otherwise use the name
|
2021-01-14 18:01:31 +01:00
|
|
|
url = encodeURI(`${ctx.request.body.name}`)
|
|
|
|
}
|
2022-01-28 14:27:19 +01:00
|
|
|
if (url) {
|
|
|
|
url = `/${url.replace(URL_REGEX_SLASH, "")}`.toLowerCase()
|
|
|
|
}
|
2022-01-21 18:31:18 +01:00
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
const checkAppUrl = (ctx: any, apps: any, url: any, currentAppId?: string) => {
|
2022-01-21 18:31:18 +01:00
|
|
|
if (currentAppId) {
|
2022-03-20 02:13:54 +01:00
|
|
|
apps = apps.filter((app: any) => app.appId !== currentAppId)
|
2021-06-29 09:20:17 +02:00
|
|
|
}
|
2022-03-20 02:13:54 +01:00
|
|
|
if (apps.some((app: any) => app.url === url)) {
|
2022-01-21 18:31:18 +01:00
|
|
|
ctx.throw(400, "App URL is already in use.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
const checkAppName = (
|
|
|
|
ctx: any,
|
|
|
|
apps: any,
|
|
|
|
name: any,
|
|
|
|
currentAppId?: string
|
|
|
|
) => {
|
2022-01-21 18:31:18 +01:00
|
|
|
// TODO: Replace with Joi
|
|
|
|
if (!name) {
|
|
|
|
ctx.throw(400, "Name is required")
|
|
|
|
}
|
|
|
|
if (currentAppId) {
|
2022-03-20 02:13:54 +01:00
|
|
|
apps = apps.filter((app: any) => app.appId !== currentAppId)
|
2022-01-21 18:31:18 +01:00
|
|
|
}
|
2022-03-20 02:13:54 +01:00
|
|
|
if (apps.some((app: any) => app.name === name)) {
|
2022-01-21 18:31:18 +01:00
|
|
|
ctx.throw(400, "App name is already in use.")
|
2021-01-14 18:01:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
async function createInstance(template: any) {
|
2021-08-05 10:59:08 +02:00
|
|
|
const tenantId = isMultiTenant() ? getTenantId() : null
|
|
|
|
const baseAppId = generateAppID(tenantId)
|
2021-05-13 12:06:08 +02:00
|
|
|
const appId = generateDevAppID(baseAppId)
|
2022-07-13 14:22:21 +02:00
|
|
|
await context.updateAppId(appId)
|
2021-05-13 12:06:08 +02:00
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
const db = context.getAppDB()
|
2020-10-29 11:21:06 +01:00
|
|
|
await db.put({
|
|
|
|
_id: "_design/database",
|
|
|
|
// view collation information, read before writing any complex views:
|
|
|
|
// https://docs.couchdb.org/en/master/ddocs/views/collation.html#collation-specification
|
|
|
|
views: {},
|
|
|
|
})
|
|
|
|
|
2021-09-02 18:13:00 +02:00
|
|
|
// NOTE: indexes need to be created before any tables/templates
|
|
|
|
// add view for linked rows
|
2022-01-27 19:18:31 +01:00
|
|
|
await createLinkView()
|
|
|
|
await createRoutingView()
|
|
|
|
await createAllSearchIndex()
|
2021-09-02 18:13:00 +02:00
|
|
|
|
2020-10-29 11:21:06 +01:00
|
|
|
// replicate the template data to the instance DB
|
2021-03-10 13:56:30 +01:00
|
|
|
// this is currently very hard to test, downloading and importing template files
|
2021-09-27 20:12:41 +02:00
|
|
|
if (template && template.templateString) {
|
2021-09-28 19:05:52 +02:00
|
|
|
const { ok } = await db.load(stringToReadStream(template.templateString))
|
2021-09-27 20:12:41 +02:00
|
|
|
if (!ok) {
|
|
|
|
throw "Error loading database dump from memory."
|
|
|
|
}
|
|
|
|
} else if (template && template.useTemplate === "true") {
|
|
|
|
/* istanbul ignore next */
|
2021-03-19 15:43:41 +01:00
|
|
|
const { ok } = await db.load(await getTemplateStream(template))
|
2020-10-29 11:21:06 +01:00
|
|
|
if (!ok) {
|
|
|
|
throw "Error loading database dump from template."
|
|
|
|
}
|
2020-11-24 15:04:14 +01:00
|
|
|
} else {
|
|
|
|
// create the users table
|
2020-11-25 16:03:19 +01:00
|
|
|
await db.put(USERS_TABLE_SCHEMA)
|
2020-10-29 11:21:06 +01:00
|
|
|
}
|
|
|
|
|
2021-06-08 19:06:16 +02:00
|
|
|
return { _id: appId }
|
2020-10-29 11:21:06 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
export const fetch = async (ctx: any) => {
|
2021-05-19 16:09:57 +02:00
|
|
|
const dev = ctx.query && ctx.query.status === AppStatus.DEV
|
|
|
|
const all = ctx.query && ctx.query.status === AppStatus.ALL
|
2022-01-27 19:18:31 +01:00
|
|
|
const apps = await getAllApps({ dev, all })
|
2021-05-13 12:06:08 +02:00
|
|
|
|
2022-10-07 17:05:01 +02:00
|
|
|
const appIds = apps
|
|
|
|
.filter((app: any) => app.status === "development")
|
|
|
|
.map((app: any) => app.appId)
|
2021-05-13 13:16:09 +02:00
|
|
|
// get the locks for all the dev apps
|
2021-05-19 16:09:57 +02:00
|
|
|
if (dev || all) {
|
2022-10-07 17:05:01 +02:00
|
|
|
const locks = await getLocksById(appIds)
|
2021-05-13 13:16:09 +02:00
|
|
|
for (let app of apps) {
|
2022-10-07 17:05:01 +02:00
|
|
|
const lock = locks[app.appId]
|
2021-05-13 13:16:09 +02:00
|
|
|
if (lock) {
|
2022-10-07 17:05:01 +02:00
|
|
|
app.lockedBy = lock
|
2021-05-13 15:17:04 +02:00
|
|
|
} else {
|
|
|
|
// make sure its definitely not present
|
|
|
|
delete app.lockedBy
|
2021-05-13 13:16:09 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-13 12:06:08 +02:00
|
|
|
}
|
|
|
|
|
2022-07-04 16:44:47 +02:00
|
|
|
ctx.body = await checkAppMetadata(apps)
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
export const fetchAppDefinition = async (ctx: any) => {
|
2022-01-27 19:18:31 +01:00
|
|
|
const layouts = await getLayouts()
|
2020-12-02 14:26:57 +01:00
|
|
|
const userRoleId = getUserRoleId(ctx)
|
2022-01-27 19:18:31 +01:00
|
|
|
const accessController = new AccessController()
|
2020-12-04 15:02:58 +01:00
|
|
|
const screens = await accessController.checkScreensAccess(
|
2022-01-27 19:18:31 +01:00
|
|
|
await getScreens(),
|
2020-12-02 14:26:57 +01:00
|
|
|
userRoleId
|
2020-11-23 16:46:26 +01:00
|
|
|
)
|
2020-11-18 19:24:12 +01:00
|
|
|
ctx.body = {
|
2020-11-23 16:46:26 +01:00
|
|
|
layouts,
|
|
|
|
screens,
|
2020-11-18 19:24:12 +01:00
|
|
|
libraries: ["@budibase/standard-components"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
export const fetchAppPackage = async (ctx: any) => {
|
2022-07-13 14:22:21 +02:00
|
|
|
const db = context.getAppDB()
|
2022-10-04 17:27:42 +02:00
|
|
|
let application = await db.get(DocumentType.APP_METADATA)
|
2022-01-27 19:18:31 +01:00
|
|
|
const layouts = await getLayouts()
|
|
|
|
let screens = await getScreens()
|
2021-06-30 22:54:48 +02:00
|
|
|
|
2022-10-04 17:27:42 +02:00
|
|
|
// Enrich plugin URLs
|
|
|
|
application.usedPlugins = enrichPluginURLs(application.usedPlugins)
|
|
|
|
|
2021-06-30 22:54:48 +02:00
|
|
|
// Only filter screens if the user is not a builder
|
2021-07-05 18:05:34 +02:00
|
|
|
if (!(ctx.user.builder && ctx.user.builder.global)) {
|
2021-06-30 22:54:48 +02:00
|
|
|
const userRoleId = getUserRoleId(ctx)
|
2022-01-27 19:18:31 +01:00
|
|
|
const accessController = new AccessController()
|
2021-06-30 22:54:48 +02:00
|
|
|
screens = await accessController.checkScreensAccess(screens, userRoleId)
|
|
|
|
}
|
2020-11-02 15:53:51 +01:00
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
application,
|
2020-11-23 16:46:26 +01:00
|
|
|
screens,
|
|
|
|
layouts,
|
2022-07-11 12:29:00 +02:00
|
|
|
clientLibPath: clientLibraryPath(ctx.params.appId, application.version),
|
2020-11-02 15:53:51 +01:00
|
|
|
}
|
2020-04-20 17:17:11 +02:00
|
|
|
}
|
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
const performAppCreate = async (ctx: any) => {
|
2022-01-27 19:18:31 +01:00
|
|
|
const apps = await getAllApps({ dev: true })
|
2022-01-21 18:31:18 +01:00
|
|
|
const name = ctx.request.body.name
|
|
|
|
checkAppName(ctx, apps, name)
|
2022-05-03 23:58:19 +02:00
|
|
|
const url = getAppUrl(ctx)
|
2022-01-21 18:31:18 +01:00
|
|
|
checkAppUrl(ctx, apps, url)
|
|
|
|
|
2021-09-27 20:12:41 +02:00
|
|
|
const { useTemplate, templateKey, templateString } = ctx.request.body
|
2022-03-20 02:13:54 +01:00
|
|
|
const instanceConfig: any = {
|
2021-03-15 19:32:20 +01:00
|
|
|
useTemplate,
|
|
|
|
key: templateKey,
|
2021-09-27 20:12:41 +02:00
|
|
|
templateString,
|
2021-03-16 14:43:46 +01:00
|
|
|
}
|
|
|
|
if (ctx.request.files && ctx.request.files.templateFile) {
|
|
|
|
instanceConfig.file = ctx.request.files.templateFile
|
|
|
|
}
|
2021-08-03 16:32:25 +02:00
|
|
|
const instance = await createInstance(instanceConfig)
|
2021-06-08 19:06:16 +02:00
|
|
|
const appId = instance._id
|
2022-07-13 14:22:21 +02:00
|
|
|
const db = context.getAppDB()
|
2022-08-24 22:35:24 +02:00
|
|
|
|
|
|
|
let newApplication: App = {
|
2022-08-11 14:50:05 +02:00
|
|
|
_id: DocumentType.APP_METADATA,
|
2022-08-24 22:35:24 +02:00
|
|
|
_rev: undefined,
|
|
|
|
appId,
|
2020-04-20 17:17:11 +02:00
|
|
|
type: "app",
|
2020-10-21 16:28:30 +02:00
|
|
|
version: packageJson.version,
|
2020-08-14 12:21:52 +02:00
|
|
|
componentLibraries: ["@budibase/standard-components"],
|
2022-01-21 18:31:18 +01:00
|
|
|
name: name,
|
2021-01-14 18:01:31 +01:00
|
|
|
url: url,
|
2022-08-24 22:35:24 +02:00
|
|
|
template: templateKey,
|
2022-08-25 09:10:11 +02:00
|
|
|
instance,
|
2021-08-05 10:59:08 +02:00
|
|
|
tenantId: getTenantId(),
|
2021-05-21 15:38:58 +02:00
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
createdAt: new Date().toISOString(),
|
2022-02-28 12:29:48 +01:00
|
|
|
status: AppStatus.DEV,
|
2022-05-10 17:57:57 +02:00
|
|
|
navigation: {
|
|
|
|
navigation: "Top",
|
|
|
|
title: name,
|
|
|
|
navWidth: "Large",
|
2022-05-13 12:09:20 +02:00
|
|
|
navBackground: "var(--spectrum-global-color-gray-100)",
|
2022-05-10 19:20:26 +02:00
|
|
|
links: [
|
|
|
|
{
|
|
|
|
url: "/home",
|
|
|
|
text: "Home",
|
|
|
|
},
|
|
|
|
],
|
2022-05-10 17:57:57 +02:00
|
|
|
},
|
|
|
|
theme: "spectrum--light",
|
2022-05-10 19:20:26 +02:00
|
|
|
customTheme: {
|
|
|
|
buttonBorderRadius: "16px",
|
|
|
|
},
|
2020-04-09 17:53:48 +02:00
|
|
|
}
|
2022-08-24 22:35:24 +02:00
|
|
|
|
|
|
|
// If we used a template or imported an app there will be an existing doc.
|
|
|
|
// Fetch and migrate some metadata from the existing app.
|
|
|
|
try {
|
|
|
|
const existing: App = await db.get(DocumentType.APP_METADATA)
|
2022-08-25 09:10:11 +02:00
|
|
|
const keys: (keyof App)[] = [
|
2022-08-24 22:35:24 +02:00
|
|
|
"_rev",
|
|
|
|
"navigation",
|
|
|
|
"theme",
|
|
|
|
"customTheme",
|
|
|
|
"icon",
|
|
|
|
]
|
2022-08-25 09:10:11 +02:00
|
|
|
keys.forEach(key => {
|
2022-08-24 22:35:24 +02:00
|
|
|
if (existing[key]) {
|
|
|
|
// @ts-ignore
|
|
|
|
newApplication[key] = existing[key]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Migrate navigation settings and screens if required
|
2022-09-26 14:59:00 +02:00
|
|
|
if (existing) {
|
2022-08-24 22:35:24 +02:00
|
|
|
const navigation = await migrateAppNavigation()
|
|
|
|
if (navigation) {
|
|
|
|
newApplication.navigation = navigation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// Nothing to do
|
|
|
|
}
|
|
|
|
|
2021-08-05 10:59:08 +02:00
|
|
|
const response = await db.put(newApplication, { force: true })
|
|
|
|
newApplication._rev = response.rev
|
2020-05-14 16:12:30 +02:00
|
|
|
|
2021-03-10 13:56:30 +01:00
|
|
|
/* istanbul ignore next */
|
2021-03-25 14:32:05 +01:00
|
|
|
if (!env.isTest()) {
|
2021-03-19 20:07:47 +01:00
|
|
|
await createApp(appId)
|
2020-05-26 17:29:16 +02:00
|
|
|
}
|
|
|
|
|
2021-11-16 21:56:24 +01:00
|
|
|
await appCache.invalidateAppMetadata(appId, newApplication)
|
2022-03-20 02:13:54 +01:00
|
|
|
return newApplication
|
|
|
|
}
|
|
|
|
|
2022-05-23 23:14:44 +02:00
|
|
|
const creationEvents = async (request: any, app: App) => {
|
|
|
|
let creationFns: ((app: App) => Promise<void>)[] = []
|
2022-04-04 16:59:00 +02:00
|
|
|
|
|
|
|
const body = request.body
|
|
|
|
if (body.useTemplate === "true") {
|
|
|
|
// from template
|
2022-05-26 11:13:26 +02:00
|
|
|
if (body.templateKey && body.templateKey !== "undefined") {
|
|
|
|
creationFns.push(a => events.app.templateImported(a, body.templateKey))
|
2022-04-04 16:59:00 +02:00
|
|
|
}
|
|
|
|
// from file
|
|
|
|
else if (request.files?.templateFile) {
|
2022-05-26 11:13:26 +02:00
|
|
|
creationFns.push(a => events.app.fileImported(a))
|
2022-04-04 16:59:00 +02:00
|
|
|
}
|
|
|
|
// unknown
|
|
|
|
else {
|
|
|
|
console.error("Could not determine template creation event")
|
|
|
|
}
|
|
|
|
}
|
2022-05-26 11:13:26 +02:00
|
|
|
creationFns.push(a => events.app.created(a))
|
2022-04-04 16:59:00 +02:00
|
|
|
|
|
|
|
for (let fn of creationFns) {
|
2022-05-23 23:14:44 +02:00
|
|
|
await fn(app)
|
2022-04-04 16:59:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 23:58:19 +02:00
|
|
|
const appPostCreate = async (ctx: any, app: App) => {
|
2022-06-13 11:51:29 +02:00
|
|
|
const tenantId = getTenantId()
|
|
|
|
await migrations.backPopulateMigrations({
|
|
|
|
type: MigrationType.APP,
|
|
|
|
tenantId,
|
|
|
|
appId: app.appId,
|
|
|
|
})
|
2022-05-23 23:14:44 +02:00
|
|
|
await creationEvents(ctx.request, app)
|
2022-03-20 02:13:54 +01:00
|
|
|
// app import & template creation
|
|
|
|
if (ctx.request.body.useTemplate === "true") {
|
2022-05-03 23:58:19 +02:00
|
|
|
const rows = await getUniqueRows([app.appId])
|
2022-03-20 02:13:54 +01:00
|
|
|
const rowCount = rows ? rows.length : 0
|
|
|
|
if (rowCount) {
|
|
|
|
try {
|
|
|
|
await quotas.addRows(rowCount)
|
|
|
|
} catch (err: any) {
|
|
|
|
if (err.code && err.code === errors.codes.USAGE_LIMIT_EXCEEDED) {
|
|
|
|
// this import resulted in row usage exceeding the quota
|
|
|
|
// delete the app
|
|
|
|
// skip pre and post steps as no rows have been added to quotas yet
|
2022-05-03 23:58:19 +02:00
|
|
|
ctx.params.appId = app.appId
|
2022-03-20 02:13:54 +01:00
|
|
|
await destroyApp(ctx)
|
|
|
|
}
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const create = async (ctx: any) => {
|
|
|
|
const newApplication = await quotas.addApp(() => performAppCreate(ctx))
|
2022-05-03 23:58:19 +02:00
|
|
|
await appPostCreate(ctx, newApplication)
|
2022-05-23 16:03:52 +02:00
|
|
|
await bustCache(CacheKeys.CHECKLIST)
|
2020-05-14 16:12:30 +02:00
|
|
|
ctx.body = newApplication
|
2022-03-20 02:13:54 +01:00
|
|
|
ctx.status = 200
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|
2020-05-26 17:29:16 +02:00
|
|
|
|
2022-01-28 14:27:19 +01:00
|
|
|
// This endpoint currently operates as a PATCH rather than a PUT
|
|
|
|
// Thus name and url fields are handled only if present
|
2022-03-20 02:13:54 +01:00
|
|
|
export const update = async (ctx: any) => {
|
2022-01-27 19:18:31 +01:00
|
|
|
const apps = await getAllApps({ dev: true })
|
2022-01-21 18:31:18 +01:00
|
|
|
// validation
|
|
|
|
const name = ctx.request.body.name
|
2022-01-28 14:27:19 +01:00
|
|
|
if (name) {
|
|
|
|
checkAppName(ctx, apps, name, ctx.params.appId)
|
|
|
|
}
|
2022-05-03 23:58:19 +02:00
|
|
|
const url = getAppUrl(ctx)
|
2022-01-28 14:27:19 +01:00
|
|
|
if (url) {
|
|
|
|
checkAppUrl(ctx, apps, url, ctx.params.appId)
|
|
|
|
ctx.request.body.url = url
|
|
|
|
}
|
2022-01-21 18:31:18 +01:00
|
|
|
|
2022-05-03 23:58:19 +02:00
|
|
|
const app = await updateAppPackage(ctx.request.body, ctx.params.appId)
|
2022-05-23 23:14:44 +02:00
|
|
|
await events.app.updated(app)
|
2021-07-07 18:07:42 +02:00
|
|
|
ctx.status = 200
|
2022-05-03 23:58:19 +02:00
|
|
|
ctx.body = app
|
2021-07-07 18:07:42 +02:00
|
|
|
}
|
2020-06-29 17:18:43 +02:00
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
export const updateClient = async (ctx: any) => {
|
2021-07-07 18:35:28 +02:00
|
|
|
// Get current app version
|
2022-07-13 14:22:21 +02:00
|
|
|
const db = context.getAppDB()
|
2022-08-11 14:50:05 +02:00
|
|
|
const application = await db.get(DocumentType.APP_METADATA)
|
2021-07-07 18:35:28 +02:00
|
|
|
const currentVersion = application.version
|
|
|
|
|
|
|
|
// Update client library and manifest
|
2021-07-08 14:20:52 +02:00
|
|
|
if (!env.isTest()) {
|
|
|
|
await backupClientLibrary(ctx.params.appId)
|
|
|
|
await updateClientLibrary(ctx.params.appId)
|
|
|
|
}
|
2021-07-07 18:35:28 +02:00
|
|
|
|
|
|
|
// Update versions in app package
|
2022-05-26 11:13:26 +02:00
|
|
|
const updatedToVersion = packageJson.version
|
2021-07-07 18:35:28 +02:00
|
|
|
const appPackageUpdates = {
|
2022-05-26 11:13:26 +02:00
|
|
|
version: updatedToVersion,
|
2021-07-07 18:35:28 +02:00
|
|
|
revertableVersion: currentVersion,
|
|
|
|
}
|
2022-05-03 23:58:19 +02:00
|
|
|
const app = await updateAppPackage(appPackageUpdates, ctx.params.appId)
|
2022-05-26 11:13:26 +02:00
|
|
|
await events.app.versionUpdated(app, currentVersion, updatedToVersion)
|
2020-06-29 17:18:43 +02:00
|
|
|
ctx.status = 200
|
2022-05-03 23:58:19 +02:00
|
|
|
ctx.body = app
|
2020-06-29 17:18:43 +02:00
|
|
|
}
|
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
export const revertClient = async (ctx: any) => {
|
2021-07-08 13:56:54 +02:00
|
|
|
// Check app can be reverted
|
2022-07-13 14:22:21 +02:00
|
|
|
const db = context.getAppDB()
|
2022-08-11 14:50:05 +02:00
|
|
|
const application = await db.get(DocumentType.APP_METADATA)
|
2021-07-08 13:56:54 +02:00
|
|
|
if (!application.revertableVersion) {
|
|
|
|
ctx.throw(400, "There is no version to revert to")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update client library and manifest
|
2021-07-08 14:20:52 +02:00
|
|
|
if (!env.isTest()) {
|
|
|
|
await revertClientLibrary(ctx.params.appId)
|
|
|
|
}
|
2021-07-08 13:56:54 +02:00
|
|
|
|
|
|
|
// Update versions in app package
|
2022-05-26 11:13:26 +02:00
|
|
|
const currentVersion = application.version
|
|
|
|
const revertedToVersion = application.revertableVersion
|
2021-07-08 13:56:54 +02:00
|
|
|
const appPackageUpdates = {
|
2022-05-26 11:13:26 +02:00
|
|
|
version: revertedToVersion,
|
2021-07-08 13:56:54 +02:00
|
|
|
revertableVersion: null,
|
|
|
|
}
|
2022-05-03 23:58:19 +02:00
|
|
|
const app = await updateAppPackage(appPackageUpdates, ctx.params.appId)
|
2022-05-26 11:13:26 +02:00
|
|
|
await events.app.versionReverted(app, currentVersion, revertedToVersion)
|
2021-07-08 13:56:54 +02:00
|
|
|
ctx.status = 200
|
2022-05-03 23:58:19 +02:00
|
|
|
ctx.body = app
|
2021-07-08 13:56:54 +02:00
|
|
|
}
|
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
const destroyApp = async (ctx: any) => {
|
2022-04-19 15:38:09 +02:00
|
|
|
let appId = ctx.params.appId
|
2022-04-20 12:08:06 +02:00
|
|
|
let isUnpublish = ctx.query && ctx.query.unpublish
|
2022-04-19 15:38:09 +02:00
|
|
|
|
2022-04-20 12:08:06 +02:00
|
|
|
if (isUnpublish) {
|
2022-04-19 15:38:09 +02:00
|
|
|
appId = getProdAppID(appId)
|
|
|
|
}
|
2021-06-10 13:07:39 +02:00
|
|
|
|
2022-07-13 14:22:21 +02:00
|
|
|
const db = isUnpublish ? context.getProdAppDB() : context.getAppDB()
|
2022-08-11 14:50:05 +02:00
|
|
|
const app = await db.get(DocumentType.APP_METADATA)
|
2020-10-28 21:35:06 +01:00
|
|
|
const result = await db.destroy()
|
2022-04-19 15:38:09 +02:00
|
|
|
|
2022-04-21 15:10:58 +02:00
|
|
|
if (isUnpublish) {
|
2022-05-23 23:14:44 +02:00
|
|
|
await events.app.unpublished(app)
|
2022-03-20 02:13:54 +01:00
|
|
|
} else {
|
|
|
|
await quotas.removeApp()
|
2022-05-23 23:14:44 +02:00
|
|
|
await events.app.deleted(app)
|
2022-03-20 02:13:54 +01:00
|
|
|
}
|
2022-04-21 15:10:58 +02:00
|
|
|
|
2021-03-25 14:32:05 +01:00
|
|
|
/* istanbul ignore next */
|
2022-04-20 12:08:06 +02:00
|
|
|
if (!env.isTest() && !isUnpublish) {
|
2022-04-19 15:38:09 +02:00
|
|
|
await deleteApp(appId)
|
2021-03-23 18:54:02 +01:00
|
|
|
}
|
2022-04-27 17:05:27 +02:00
|
|
|
// automations only in production
|
2022-04-20 12:08:06 +02:00
|
|
|
if (isUnpublish) {
|
2022-04-19 15:38:09 +02:00
|
|
|
await cleanupAutomations(appId)
|
2021-11-17 17:28:52 +01:00
|
|
|
}
|
2022-04-27 17:05:27 +02:00
|
|
|
// remove app role when the dev app is deleted (no trace of app anymore)
|
|
|
|
else {
|
|
|
|
await removeAppFromUserRoles(ctx, appId)
|
|
|
|
}
|
2022-04-19 15:38:09 +02:00
|
|
|
await appCache.invalidateAppMetadata(appId)
|
2022-03-20 02:13:54 +01:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
const preDestroyApp = async (ctx: any) => {
|
2022-03-22 01:23:22 +01:00
|
|
|
const rows = await getUniqueRows([ctx.params.appId])
|
2022-03-20 02:13:54 +01:00
|
|
|
ctx.rowCount = rows.length
|
|
|
|
}
|
|
|
|
|
|
|
|
const postDestroyApp = async (ctx: any) => {
|
|
|
|
const rowCount = ctx.rowCount
|
2022-09-22 00:12:38 +02:00
|
|
|
await groups.cleanupApp(ctx.params.appId)
|
2022-03-20 02:13:54 +01:00
|
|
|
if (rowCount) {
|
|
|
|
await quotas.removeRows(rowCount)
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 14:44:05 +02:00
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
export const destroy = async (ctx: any) => {
|
|
|
|
await preDestroyApp(ctx)
|
|
|
|
const result = await destroyApp(ctx)
|
|
|
|
await postDestroyApp(ctx)
|
2020-07-07 14:44:05 +02:00
|
|
|
ctx.status = 200
|
|
|
|
ctx.body = result
|
|
|
|
}
|
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
export const sync = async (ctx: any, next: any) => {
|
2022-05-22 18:11:05 +02:00
|
|
|
if (env.DISABLE_AUTO_PROD_APP_SYNC) {
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.body = {
|
|
|
|
message:
|
|
|
|
"App sync disabled. You can reenable with the DISABLE_AUTO_PROD_APP_SYNC environment variable.",
|
|
|
|
}
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
2021-10-22 15:34:20 +02:00
|
|
|
const appId = ctx.params.appId
|
|
|
|
if (!isDevAppID(appId)) {
|
|
|
|
ctx.throw(400, "This action cannot be performed for production apps")
|
|
|
|
}
|
2021-11-03 23:23:00 +01:00
|
|
|
|
|
|
|
// replicate prod to dev
|
2022-01-31 18:42:51 +01:00
|
|
|
const prodAppId = getProdAppID(appId)
|
2021-11-08 15:26:07 +01:00
|
|
|
|
|
|
|
try {
|
2022-01-27 19:18:31 +01:00
|
|
|
// specific case, want to make sure setup is skipped
|
2022-07-13 14:22:21 +02:00
|
|
|
const prodDb = context.getProdAppDB({ skip_setup: true })
|
2021-11-08 15:26:07 +01:00
|
|
|
const info = await prodDb.info()
|
|
|
|
if (info.error) throw info.error
|
|
|
|
} catch (err) {
|
|
|
|
// the database doesn't exist. Don't replicate
|
2021-11-09 17:40:31 +01:00
|
|
|
ctx.status = 200
|
2021-11-08 15:26:07 +01:00
|
|
|
ctx.body = {
|
2021-11-08 15:26:44 +01:00
|
|
|
message: "App sync not required, app not deployed.",
|
2021-11-08 15:26:07 +01:00
|
|
|
}
|
2021-11-09 17:40:31 +01:00
|
|
|
return next()
|
2021-11-08 15:26:07 +01:00
|
|
|
}
|
|
|
|
|
2021-10-22 15:34:20 +02:00
|
|
|
const replication = new Replication({
|
|
|
|
source: prodAppId,
|
|
|
|
target: appId,
|
|
|
|
})
|
|
|
|
let error
|
|
|
|
try {
|
2022-09-06 19:07:18 +02:00
|
|
|
await replication.replicate(replication.appReplicateOpts())
|
2021-10-22 15:34:20 +02:00
|
|
|
} catch (err) {
|
|
|
|
error = err
|
2022-04-20 18:33:42 +02:00
|
|
|
} finally {
|
|
|
|
await replication.close()
|
2021-10-22 15:34:20 +02:00
|
|
|
}
|
2021-11-03 23:23:00 +01:00
|
|
|
|
|
|
|
// sync the users
|
2022-01-27 19:18:31 +01:00
|
|
|
await syncGlobalUsers()
|
2021-11-03 23:23:00 +01:00
|
|
|
|
2021-10-22 15:34:20 +02:00
|
|
|
if (error) {
|
|
|
|
ctx.throw(400, error)
|
|
|
|
} else {
|
|
|
|
ctx.body = {
|
|
|
|
message: "App sync completed successfully.",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 16:30:27 +02:00
|
|
|
export const updateAppPackage = async (appPackage: any, appId: any) => {
|
2022-07-14 17:40:23 +02:00
|
|
|
return context.doInAppContext(appId, async () => {
|
|
|
|
const db = context.getAppDB()
|
2022-08-11 14:50:05 +02:00
|
|
|
const application = await db.get(DocumentType.APP_METADATA)
|
2021-07-07 18:07:42 +02:00
|
|
|
|
2022-05-26 11:03:29 +02:00
|
|
|
const newAppPackage = { ...application, ...appPackage }
|
|
|
|
if (appPackage._rev !== application._rev) {
|
|
|
|
newAppPackage._rev = application._rev
|
|
|
|
}
|
2020-11-02 15:53:51 +01:00
|
|
|
|
2022-05-26 11:03:29 +02:00
|
|
|
// the locked by property is attached by server but generated from
|
|
|
|
// Redis, shouldn't ever store it
|
|
|
|
delete newAppPackage.lockedBy
|
2020-11-06 14:40:00 +01:00
|
|
|
|
2022-07-04 11:38:16 +02:00
|
|
|
await db.put(newAppPackage)
|
2022-05-26 11:03:29 +02:00
|
|
|
// remove any cached metadata, so that it will be updated
|
|
|
|
await appCache.invalidateAppMetadata(appId)
|
2022-07-04 11:38:16 +02:00
|
|
|
return newAppPackage
|
2022-05-26 11:03:29 +02:00
|
|
|
})
|
2020-05-26 17:29:16 +02:00
|
|
|
}
|
2022-08-24 22:35:24 +02:00
|
|
|
|
|
|
|
const migrateAppNavigation = async () => {
|
|
|
|
const db = context.getAppDB()
|
|
|
|
const existing: App = await db.get(DocumentType.APP_METADATA)
|
2022-08-25 09:10:11 +02:00
|
|
|
const layouts: Layout[] = await getLayouts()
|
|
|
|
const screens: Screen[] = await getScreens()
|
2022-08-24 22:35:24 +02:00
|
|
|
|
|
|
|
// Migrate all screens, removing custom layouts
|
|
|
|
for (let screen of screens) {
|
|
|
|
if (!screen.layoutId) {
|
2022-09-26 14:59:00 +02:00
|
|
|
continue
|
2022-08-24 22:35:24 +02:00
|
|
|
}
|
2022-08-25 09:10:11 +02:00
|
|
|
const layout = layouts.find(layout => layout._id === screen.layoutId)
|
2022-08-24 22:35:24 +02:00
|
|
|
screen.layoutId = undefined
|
|
|
|
screen.showNavigation = layout?.props.navigation !== "None"
|
|
|
|
screen.width = layout?.props.width || "Large"
|
|
|
|
await db.put(screen)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate layout navigation settings
|
|
|
|
const { name, customTheme } = existing
|
|
|
|
const layout = layouts?.find(
|
|
|
|
(layout: Layout) => layout._id === BASE_LAYOUT_PROP_IDS.PRIVATE
|
|
|
|
)
|
2022-09-26 14:59:00 +02:00
|
|
|
if (layout && !existing.navigation) {
|
2022-08-24 22:35:24 +02:00
|
|
|
let navigationSettings: any = {
|
|
|
|
navigation: "Top",
|
|
|
|
title: name,
|
|
|
|
navWidth: "Large",
|
|
|
|
navBackground:
|
|
|
|
customTheme?.navBackground || "var(--spectrum-global-color-gray-50)",
|
|
|
|
navTextColor:
|
|
|
|
customTheme?.navTextColor || "var(--spectrum-global-color-gray-800)",
|
|
|
|
}
|
|
|
|
if (layout) {
|
|
|
|
navigationSettings.hideLogo = layout.props.hideLogo
|
|
|
|
navigationSettings.hideTitle = layout.props.hideTitle
|
|
|
|
navigationSettings.title = layout.props.title || name
|
|
|
|
navigationSettings.logoUrl = layout.props.logoUrl
|
|
|
|
navigationSettings.links = layout.props.links
|
|
|
|
navigationSettings.navigation = layout.props.navigation || "Top"
|
|
|
|
navigationSettings.sticky = layout.props.sticky
|
|
|
|
navigationSettings.navWidth = layout.props.width || "Large"
|
|
|
|
if (navigationSettings.navigation === "None") {
|
|
|
|
navigationSettings.navigation = "Top"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return navigationSettings
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|