2022-03-20 14:13:54 +13:00
|
|
|
import env from "../../environment"
|
|
|
|
import packageJson from "../../../package.json"
|
|
|
|
import {
|
2021-03-26 07:04:44 +13:00
|
|
|
createLinkView,
|
|
|
|
createRoutingView,
|
2021-03-26 12:42:50 +13:00
|
|
|
createAllSearchIndex,
|
2022-03-20 14:13:54 +13:00
|
|
|
} from "../../db/views/staticViews"
|
|
|
|
import {
|
2021-03-20 08:07:47 +13:00
|
|
|
getTemplateStream,
|
|
|
|
createApp,
|
|
|
|
deleteApp,
|
2022-03-20 14:13:54 +13:00
|
|
|
} from "../../utilities/fileSystem"
|
|
|
|
import {
|
2020-11-03 03:53:51 +13:00
|
|
|
generateAppID,
|
2020-11-24 03:07:18 +13:00
|
|
|
getLayoutParams,
|
2020-11-19 07:24:12 +13:00
|
|
|
getScreenParams,
|
2021-05-13 22:06:08 +12:00
|
|
|
generateDevAppID,
|
|
|
|
DocumentTypes,
|
|
|
|
AppStatus,
|
2022-03-20 14:13:54 +13:00
|
|
|
} from "../../db/utils"
|
2022-01-11 08:33:00 +13:00
|
|
|
const {
|
|
|
|
BUILTIN_ROLE_IDS,
|
|
|
|
AccessController,
|
|
|
|
} = require("@budibase/backend-core/roles")
|
2022-03-20 14:13:54 +13:00
|
|
|
import { BASE_LAYOUTS } from "../../constants/layouts"
|
|
|
|
import { cloneDeep } from "lodash/fp"
|
2021-01-21 02:32:15 +13:00
|
|
|
const { processObject } = require("@budibase/string-templates")
|
2022-05-24 02:03:52 +12:00
|
|
|
const { CacheKeys, bustCache } = require("@budibase/backend-core/cache")
|
2021-10-23 02:34:20 +13:00
|
|
|
const {
|
|
|
|
getAllApps,
|
|
|
|
isDevAppID,
|
2022-02-01 06:42:51 +13:00
|
|
|
getProdAppID,
|
2021-10-23 02:34:20 +13:00
|
|
|
Replication,
|
2022-01-11 08:33:00 +13:00
|
|
|
} = require("@budibase/backend-core/db")
|
2022-03-20 14:13:54 +13:00
|
|
|
import { USERS_TABLE_SCHEMA } from "../../constants"
|
|
|
|
import { removeAppFromUserRoles } from "../../utilities/workerRequests"
|
|
|
|
import { clientLibraryPath, stringToReadStream } from "../../utilities"
|
|
|
|
import { getAllLocks } from "../../utilities/redis"
|
|
|
|
import {
|
2021-07-08 23:56:54 +12:00
|
|
|
updateClientLibrary,
|
|
|
|
backupClientLibrary,
|
|
|
|
revertClientLibrary,
|
2022-03-20 14:13:54 +13:00
|
|
|
} from "../../utilities/fileSystem/clientLibrary"
|
2022-01-11 08:33:00 +13:00
|
|
|
const { getTenantId, isMultiTenant } = require("@budibase/backend-core/tenancy")
|
2022-03-20 14:13:54 +13:00
|
|
|
import { syncGlobalUsers } from "./user"
|
2022-01-11 08:33:00 +13:00
|
|
|
const { app: appCache } = require("@budibase/backend-core/cache")
|
2022-03-20 14:13:54 +13:00
|
|
|
import { cleanupAutomations } from "../../automations/utils"
|
2022-02-01 06:42:51 +13:00
|
|
|
const {
|
|
|
|
getAppDB,
|
|
|
|
getProdAppDB,
|
|
|
|
updateAppId,
|
|
|
|
} = require("@budibase/backend-core/context")
|
2022-03-20 14:13:54 +13:00
|
|
|
import { getUniqueRows } from "../../utilities/usageQuota/rows"
|
|
|
|
import { quotas } from "@budibase/pro"
|
2022-04-05 02:59:00 +12:00
|
|
|
import { errors, events } from "@budibase/backend-core"
|
2022-05-04 09:58:19 +12:00
|
|
|
import { App } from "@budibase/types"
|
2020-11-03 03:53:51 +13:00
|
|
|
|
2021-01-19 01:36:49 +13:00
|
|
|
const URL_REGEX_SLASH = /\/|\\/g
|
2020-04-08 04:25:09 +12:00
|
|
|
|
2020-11-19 07:24:12 +13:00
|
|
|
// utility function, need to do away with this
|
2022-01-28 07:18:31 +13:00
|
|
|
async function getLayouts() {
|
2022-02-01 06:42:51 +13:00
|
|
|
const db = getAppDB()
|
2020-11-24 04:46:26 +13:00
|
|
|
return (
|
|
|
|
await db.allDocs(
|
|
|
|
getLayoutParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2022-03-20 14:13:54 +13:00
|
|
|
).rows.map((row: any) => row.doc)
|
2020-11-24 04:46:26 +13:00
|
|
|
}
|
|
|
|
|
2022-01-28 07:18:31 +13:00
|
|
|
async function getScreens() {
|
2022-02-01 06:42:51 +13:00
|
|
|
const db = getAppDB()
|
2020-11-24 04:46:26 +13:00
|
|
|
return (
|
|
|
|
await db.allDocs(
|
|
|
|
getScreenParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2022-03-20 14:13:54 +13:00
|
|
|
).rows.map((row: any) => row.doc)
|
2020-11-24 04:46:26 +13:00
|
|
|
}
|
2020-11-19 07:24:12 +13:00
|
|
|
|
2022-03-20 14:13:54 +13:00
|
|
|
function getUserRoleId(ctx: any) {
|
2020-12-03 02:26:57 +13:00
|
|
|
return !ctx.user.role || !ctx.user.role._id
|
|
|
|
? BUILTIN_ROLE_IDS.PUBLIC
|
|
|
|
: ctx.user.role._id
|
2020-11-19 07:24:12 +13:00
|
|
|
}
|
|
|
|
|
2022-03-20 14:13:54 +13:00
|
|
|
export const getAppUrl = (ctx: any) => {
|
2022-01-22 06:31:18 +13:00
|
|
|
// construct the url
|
2021-01-15 06:01:31 +13:00
|
|
|
let url
|
|
|
|
if (ctx.request.body.url) {
|
2022-01-22 06:31:18 +13:00
|
|
|
// if the url is provided, use that
|
2021-01-15 06:01:31 +13:00
|
|
|
url = encodeURI(ctx.request.body.url)
|
2022-01-29 02:27:19 +13:00
|
|
|
} else if (ctx.request.body.name) {
|
2022-01-22 06:31:18 +13:00
|
|
|
// otherwise use the name
|
2021-01-15 06:01:31 +13:00
|
|
|
url = encodeURI(`${ctx.request.body.name}`)
|
|
|
|
}
|
2022-01-29 02:27:19 +13:00
|
|
|
if (url) {
|
|
|
|
url = `/${url.replace(URL_REGEX_SLASH, "")}`.toLowerCase()
|
|
|
|
}
|
2022-01-22 06:31:18 +13:00
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2022-03-20 14:13:54 +13:00
|
|
|
const checkAppUrl = (ctx: any, apps: any, url: any, currentAppId?: string) => {
|
2022-01-22 06:31:18 +13:00
|
|
|
if (currentAppId) {
|
2022-03-20 14:13:54 +13:00
|
|
|
apps = apps.filter((app: any) => app.appId !== currentAppId)
|
2021-06-29 19:20:17 +12:00
|
|
|
}
|
2022-03-20 14:13:54 +13:00
|
|
|
if (apps.some((app: any) => app.url === url)) {
|
2022-01-22 06:31:18 +13:00
|
|
|
ctx.throw(400, "App URL is already in use.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 14:13:54 +13:00
|
|
|
const checkAppName = (
|
|
|
|
ctx: any,
|
|
|
|
apps: any,
|
|
|
|
name: any,
|
|
|
|
currentAppId?: string
|
|
|
|
) => {
|
2022-01-22 06:31:18 +13:00
|
|
|
// TODO: Replace with Joi
|
|
|
|
if (!name) {
|
|
|
|
ctx.throw(400, "Name is required")
|
|
|
|
}
|
|
|
|
if (currentAppId) {
|
2022-03-20 14:13:54 +13:00
|
|
|
apps = apps.filter((app: any) => app.appId !== currentAppId)
|
2022-01-22 06:31:18 +13:00
|
|
|
}
|
2022-03-20 14:13:54 +13:00
|
|
|
if (apps.some((app: any) => app.name === name)) {
|
2022-01-22 06:31:18 +13:00
|
|
|
ctx.throw(400, "App name is already in use.")
|
2021-01-15 06:01:31 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 14:13:54 +13:00
|
|
|
async function createInstance(template: any) {
|
2021-08-05 20:59:08 +12:00
|
|
|
const tenantId = isMultiTenant() ? getTenantId() : null
|
|
|
|
const baseAppId = generateAppID(tenantId)
|
2021-05-13 22:06:08 +12:00
|
|
|
const appId = generateDevAppID(baseAppId)
|
2022-04-22 01:56:14 +12:00
|
|
|
await updateAppId(appId)
|
2021-05-13 22:06:08 +12:00
|
|
|
|
2022-02-01 06:42:51 +13:00
|
|
|
const db = getAppDB()
|
2020-10-29 23:21:06 +13: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-03 04:13:00 +12:00
|
|
|
// NOTE: indexes need to be created before any tables/templates
|
|
|
|
// add view for linked rows
|
2022-01-28 07:18:31 +13:00
|
|
|
await createLinkView()
|
|
|
|
await createRoutingView()
|
|
|
|
await createAllSearchIndex()
|
2021-09-03 04:13:00 +12:00
|
|
|
|
2020-10-29 23:21:06 +13:00
|
|
|
// replicate the template data to the instance DB
|
2021-03-11 01:56:30 +13:00
|
|
|
// this is currently very hard to test, downloading and importing template files
|
2021-09-28 07:12:41 +13:00
|
|
|
if (template && template.templateString) {
|
2021-09-29 06:05:52 +13:00
|
|
|
const { ok } = await db.load(stringToReadStream(template.templateString))
|
2021-09-28 07:12:41 +13:00
|
|
|
if (!ok) {
|
|
|
|
throw "Error loading database dump from memory."
|
|
|
|
}
|
|
|
|
} else if (template && template.useTemplate === "true") {
|
|
|
|
/* istanbul ignore next */
|
2021-03-20 03:43:41 +13:00
|
|
|
const { ok } = await db.load(await getTemplateStream(template))
|
2020-10-29 23:21:06 +13:00
|
|
|
if (!ok) {
|
|
|
|
throw "Error loading database dump from template."
|
|
|
|
}
|
2020-11-25 03:04:14 +13:00
|
|
|
} else {
|
|
|
|
// create the users table
|
2020-11-26 04:03:19 +13:00
|
|
|
await db.put(USERS_TABLE_SCHEMA)
|
2020-10-29 23:21:06 +13:00
|
|
|
}
|
|
|
|
|
2021-06-09 05:06:16 +12:00
|
|
|
return { _id: appId }
|
2020-10-29 23:21:06 +13:00
|
|
|
}
|
|
|
|
|
2022-03-20 14:13:54 +13:00
|
|
|
export const fetch = async (ctx: any) => {
|
2021-05-20 02:09:57 +12:00
|
|
|
const dev = ctx.query && ctx.query.status === AppStatus.DEV
|
|
|
|
const all = ctx.query && ctx.query.status === AppStatus.ALL
|
2022-01-28 07:18:31 +13:00
|
|
|
const apps = await getAllApps({ dev, all })
|
2021-05-13 22:06:08 +12:00
|
|
|
|
2021-05-13 23:16:09 +12:00
|
|
|
// get the locks for all the dev apps
|
2021-05-20 02:09:57 +12:00
|
|
|
if (dev || all) {
|
2021-05-13 23:16:09 +12:00
|
|
|
const locks = await getAllLocks()
|
|
|
|
for (let app of apps) {
|
2021-05-20 02:09:57 +12:00
|
|
|
if (app.status !== "development") {
|
|
|
|
continue
|
|
|
|
}
|
2022-03-20 14:13:54 +13:00
|
|
|
const lock = locks.find((lock: any) => lock.appId === app.appId)
|
2021-05-13 23:16:09 +12:00
|
|
|
if (lock) {
|
|
|
|
app.lockedBy = lock.user
|
2021-05-14 01:17:04 +12:00
|
|
|
} else {
|
|
|
|
// make sure its definitely not present
|
|
|
|
delete app.lockedBy
|
2021-05-13 23:16:09 +12:00
|
|
|
}
|
|
|
|
}
|
2021-05-13 22:06:08 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.body = apps
|
2020-05-07 21:53:34 +12:00
|
|
|
}
|
2020-04-08 04:25:09 +12:00
|
|
|
|
2022-03-20 14:13:54 +13:00
|
|
|
export const fetchAppDefinition = async (ctx: any) => {
|
2022-01-28 07:18:31 +13:00
|
|
|
const layouts = await getLayouts()
|
2020-12-03 02:26:57 +13:00
|
|
|
const userRoleId = getUserRoleId(ctx)
|
2022-01-28 07:18:31 +13:00
|
|
|
const accessController = new AccessController()
|
2020-12-05 03:02:58 +13:00
|
|
|
const screens = await accessController.checkScreensAccess(
|
2022-01-28 07:18:31 +13:00
|
|
|
await getScreens(),
|
2020-12-03 02:26:57 +13:00
|
|
|
userRoleId
|
2020-11-24 04:46:26 +13:00
|
|
|
)
|
2020-11-19 07:24:12 +13:00
|
|
|
ctx.body = {
|
2020-11-24 04:46:26 +13:00
|
|
|
layouts,
|
|
|
|
screens,
|
2020-11-19 07:24:12 +13:00
|
|
|
libraries: ["@budibase/standard-components"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 14:13:54 +13:00
|
|
|
export const fetchAppPackage = async (ctx: any) => {
|
2022-02-01 06:42:51 +13:00
|
|
|
const db = getAppDB()
|
2021-05-17 08:25:37 +12:00
|
|
|
const application = await db.get(DocumentTypes.APP_METADATA)
|
2022-01-28 07:18:31 +13:00
|
|
|
const layouts = await getLayouts()
|
|
|
|
let screens = await getScreens()
|
2021-07-01 08:54:48 +12:00
|
|
|
|
|
|
|
// Only filter screens if the user is not a builder
|
2021-07-06 04:05:34 +12:00
|
|
|
if (!(ctx.user.builder && ctx.user.builder.global)) {
|
2021-07-01 08:54:48 +12:00
|
|
|
const userRoleId = getUserRoleId(ctx)
|
2022-01-28 07:18:31 +13:00
|
|
|
const accessController = new AccessController()
|
2021-07-01 08:54:48 +12:00
|
|
|
screens = await accessController.checkScreensAccess(screens, userRoleId)
|
|
|
|
}
|
2020-11-03 03:53:51 +13:00
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
application,
|
2020-11-24 04:46:26 +13:00
|
|
|
screens,
|
|
|
|
layouts,
|
2021-11-12 00:29:52 +13:00
|
|
|
clientLibPath: clientLibraryPath(ctx.params.appId, application.version),
|
2020-11-03 03:53:51 +13:00
|
|
|
}
|
2020-04-21 03:17:11 +12:00
|
|
|
}
|
|
|
|
|
2022-03-20 14:13:54 +13:00
|
|
|
const performAppCreate = async (ctx: any) => {
|
2022-01-28 07:18:31 +13:00
|
|
|
const apps = await getAllApps({ dev: true })
|
2022-01-22 06:31:18 +13:00
|
|
|
const name = ctx.request.body.name
|
|
|
|
checkAppName(ctx, apps, name)
|
2022-05-04 09:58:19 +12:00
|
|
|
const url = getAppUrl(ctx)
|
2022-01-22 06:31:18 +13:00
|
|
|
checkAppUrl(ctx, apps, url)
|
|
|
|
|
2021-09-28 07:12:41 +13:00
|
|
|
const { useTemplate, templateKey, templateString } = ctx.request.body
|
2022-03-20 14:13:54 +13:00
|
|
|
const instanceConfig: any = {
|
2021-03-16 07:32:20 +13:00
|
|
|
useTemplate,
|
|
|
|
key: templateKey,
|
2021-09-28 07:12:41 +13:00
|
|
|
templateString,
|
2021-03-17 02:43:46 +13:00
|
|
|
}
|
|
|
|
if (ctx.request.files && ctx.request.files.templateFile) {
|
|
|
|
instanceConfig.file = ctx.request.files.templateFile
|
|
|
|
}
|
2021-08-04 02:32:25 +12:00
|
|
|
const instance = await createInstance(instanceConfig)
|
2021-06-09 05:06:16 +12:00
|
|
|
const appId = instance._id
|
2021-03-16 07:32:20 +13:00
|
|
|
|
2022-02-01 06:42:51 +13:00
|
|
|
const db = getAppDB()
|
2021-06-09 05:06:16 +12:00
|
|
|
let _rev
|
|
|
|
try {
|
|
|
|
// if template there will be an existing doc
|
|
|
|
const existing = await db.get(DocumentTypes.APP_METADATA)
|
|
|
|
_rev = existing._rev
|
|
|
|
} catch (err) {
|
|
|
|
// nothing to do
|
|
|
|
}
|
2022-05-10 21:33:59 +12:00
|
|
|
const newApplication: App = {
|
2021-05-17 08:25:37 +12:00
|
|
|
_id: DocumentTypes.APP_METADATA,
|
2021-06-09 05:06:16 +12:00
|
|
|
_rev,
|
2021-05-17 08:25:37 +12:00
|
|
|
appId: instance._id,
|
2020-04-21 03:17:11 +12:00
|
|
|
type: "app",
|
2020-10-22 03:28:30 +13:00
|
|
|
version: packageJson.version,
|
2020-08-14 22:21:52 +12:00
|
|
|
componentLibraries: ["@budibase/standard-components"],
|
2022-01-22 06:31:18 +13:00
|
|
|
name: name,
|
2021-01-15 06:01:31 +13:00
|
|
|
url: url,
|
2020-09-26 01:47:42 +12:00
|
|
|
template: ctx.request.body.template,
|
2020-10-29 23:21:06 +13:00
|
|
|
instance: instance,
|
2021-08-05 20:59:08 +12:00
|
|
|
tenantId: getTenantId(),
|
2021-05-22 01:38:58 +12:00
|
|
|
updatedAt: new Date().toISOString(),
|
|
|
|
createdAt: new Date().toISOString(),
|
2022-03-01 00:29:48 +13:00
|
|
|
status: AppStatus.DEV,
|
2020-04-10 03:53:48 +12:00
|
|
|
}
|
2021-08-05 20:59:08 +12:00
|
|
|
const response = await db.put(newApplication, { force: true })
|
|
|
|
newApplication._rev = response.rev
|
2020-05-15 02:12:30 +12:00
|
|
|
|
2021-09-23 01:50:52 +12:00
|
|
|
// Only create the default home screens and layout if we aren't importing
|
|
|
|
// an app
|
2021-09-27 23:30:46 +13:00
|
|
|
if (useTemplate !== "true") {
|
2021-09-23 01:50:52 +12:00
|
|
|
await createEmptyAppPackage(ctx, newApplication)
|
|
|
|
}
|
|
|
|
|
2021-03-11 01:56:30 +13:00
|
|
|
/* istanbul ignore next */
|
2021-03-26 02:32:05 +13:00
|
|
|
if (!env.isTest()) {
|
2021-03-20 08:07:47 +13:00
|
|
|
await createApp(appId)
|
2020-05-27 03:29:16 +12:00
|
|
|
}
|
|
|
|
|
2021-11-17 09:56:24 +13:00
|
|
|
await appCache.invalidateAppMetadata(appId, newApplication)
|
2022-03-20 14:13:54 +13:00
|
|
|
return newApplication
|
|
|
|
}
|
|
|
|
|
2022-05-24 09:14:44 +12:00
|
|
|
const creationEvents = async (request: any, app: App) => {
|
|
|
|
let creationFns: ((app: App) => Promise<void>)[] = []
|
2022-04-05 02:59:00 +12:00
|
|
|
|
|
|
|
const body = request.body
|
|
|
|
if (body.useTemplate === "true") {
|
|
|
|
// from template
|
2022-05-26 21:13:26 +12:00
|
|
|
if (body.templateKey && body.templateKey !== "undefined") {
|
|
|
|
creationFns.push(a => events.app.templateImported(a, body.templateKey))
|
2022-04-05 02:59:00 +12:00
|
|
|
}
|
|
|
|
// from file
|
|
|
|
else if (request.files?.templateFile) {
|
2022-05-26 21:13:26 +12:00
|
|
|
creationFns.push(a => events.app.fileImported(a))
|
2022-04-05 02:59:00 +12:00
|
|
|
}
|
|
|
|
// unknown
|
|
|
|
else {
|
|
|
|
console.error("Could not determine template creation event")
|
|
|
|
}
|
|
|
|
}
|
2022-05-26 21:13:26 +12:00
|
|
|
creationFns.push(a => events.app.created(a))
|
2022-04-05 02:59:00 +12:00
|
|
|
|
|
|
|
for (let fn of creationFns) {
|
2022-05-24 09:14:44 +12:00
|
|
|
await fn(app)
|
2022-04-05 02:59:00 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 09:58:19 +12:00
|
|
|
const appPostCreate = async (ctx: any, app: App) => {
|
2022-05-24 09:14:44 +12:00
|
|
|
await creationEvents(ctx.request, app)
|
2022-03-20 14:13:54 +13:00
|
|
|
// app import & template creation
|
|
|
|
if (ctx.request.body.useTemplate === "true") {
|
2022-05-04 09:58:19 +12:00
|
|
|
const rows = await getUniqueRows([app.appId])
|
2022-03-20 14:13:54 +13: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-04 09:58:19 +12:00
|
|
|
ctx.params.appId = app.appId
|
2022-03-20 14:13:54 +13:00
|
|
|
await destroyApp(ctx)
|
|
|
|
}
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const create = async (ctx: any) => {
|
|
|
|
const newApplication = await quotas.addApp(() => performAppCreate(ctx))
|
2022-05-04 09:58:19 +12:00
|
|
|
await appPostCreate(ctx, newApplication)
|
2022-05-24 02:03:52 +12:00
|
|
|
await bustCache(CacheKeys.CHECKLIST)
|
2020-05-15 02:12:30 +12:00
|
|
|
ctx.body = newApplication
|
2022-03-20 14:13:54 +13:00
|
|
|
ctx.status = 200
|
2020-05-07 21:53:34 +12:00
|
|
|
}
|
2020-05-27 03:29:16 +12:00
|
|
|
|
2022-01-29 02:27:19 +13: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 14:13:54 +13:00
|
|
|
export const update = async (ctx: any) => {
|
2022-01-28 07:18:31 +13:00
|
|
|
const apps = await getAllApps({ dev: true })
|
2022-01-22 06:31:18 +13:00
|
|
|
// validation
|
|
|
|
const name = ctx.request.body.name
|
2022-01-29 02:27:19 +13:00
|
|
|
if (name) {
|
|
|
|
checkAppName(ctx, apps, name, ctx.params.appId)
|
|
|
|
}
|
2022-05-04 09:58:19 +12:00
|
|
|
const url = getAppUrl(ctx)
|
2022-01-29 02:27:19 +13:00
|
|
|
if (url) {
|
|
|
|
checkAppUrl(ctx, apps, url, ctx.params.appId)
|
|
|
|
ctx.request.body.url = url
|
|
|
|
}
|
2022-01-22 06:31:18 +13:00
|
|
|
|
2022-05-04 09:58:19 +12:00
|
|
|
const app = await updateAppPackage(ctx.request.body, ctx.params.appId)
|
2022-05-24 09:14:44 +12:00
|
|
|
await events.app.updated(app)
|
2021-07-08 04:07:42 +12:00
|
|
|
ctx.status = 200
|
2022-05-04 09:58:19 +12:00
|
|
|
ctx.body = app
|
2021-07-08 04:07:42 +12:00
|
|
|
}
|
2020-06-30 03:18:43 +12:00
|
|
|
|
2022-03-20 14:13:54 +13:00
|
|
|
export const updateClient = async (ctx: any) => {
|
2021-07-08 04:35:28 +12:00
|
|
|
// Get current app version
|
2022-02-01 06:42:51 +13:00
|
|
|
const db = getAppDB()
|
2021-07-08 04:35:28 +12:00
|
|
|
const application = await db.get(DocumentTypes.APP_METADATA)
|
|
|
|
const currentVersion = application.version
|
|
|
|
|
|
|
|
// Update client library and manifest
|
2021-07-09 00:20:52 +12:00
|
|
|
if (!env.isTest()) {
|
|
|
|
await backupClientLibrary(ctx.params.appId)
|
|
|
|
await updateClientLibrary(ctx.params.appId)
|
|
|
|
}
|
2021-07-08 04:35:28 +12:00
|
|
|
|
|
|
|
// Update versions in app package
|
2022-05-26 21:13:26 +12:00
|
|
|
const updatedToVersion = packageJson.version
|
2021-07-08 04:35:28 +12:00
|
|
|
const appPackageUpdates = {
|
2022-05-26 21:13:26 +12:00
|
|
|
version: updatedToVersion,
|
2021-07-08 04:35:28 +12:00
|
|
|
revertableVersion: currentVersion,
|
|
|
|
}
|
2022-05-04 09:58:19 +12:00
|
|
|
const app = await updateAppPackage(appPackageUpdates, ctx.params.appId)
|
2022-05-26 21:13:26 +12:00
|
|
|
await events.app.versionUpdated(app, currentVersion, updatedToVersion)
|
2020-06-30 03:18:43 +12:00
|
|
|
ctx.status = 200
|
2022-05-04 09:58:19 +12:00
|
|
|
ctx.body = app
|
2020-06-30 03:18:43 +12:00
|
|
|
}
|
|
|
|
|
2022-03-20 14:13:54 +13:00
|
|
|
export const revertClient = async (ctx: any) => {
|
2021-07-08 23:56:54 +12:00
|
|
|
// Check app can be reverted
|
2022-02-01 06:42:51 +13:00
|
|
|
const db = getAppDB()
|
2021-07-08 23:56:54 +12:00
|
|
|
const application = await db.get(DocumentTypes.APP_METADATA)
|
|
|
|
if (!application.revertableVersion) {
|
|
|
|
ctx.throw(400, "There is no version to revert to")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update client library and manifest
|
2021-07-09 00:20:52 +12:00
|
|
|
if (!env.isTest()) {
|
|
|
|
await revertClientLibrary(ctx.params.appId)
|
|
|
|
}
|
2021-07-08 23:56:54 +12:00
|
|
|
|
|
|
|
// Update versions in app package
|
2022-05-26 21:13:26 +12:00
|
|
|
const currentVersion = application.version
|
|
|
|
const revertedToVersion = application.revertableVersion
|
2021-07-08 23:56:54 +12:00
|
|
|
const appPackageUpdates = {
|
2022-05-26 21:13:26 +12:00
|
|
|
version: revertedToVersion,
|
2021-07-08 23:56:54 +12:00
|
|
|
revertableVersion: null,
|
|
|
|
}
|
2022-05-04 09:58:19 +12:00
|
|
|
const app = await updateAppPackage(appPackageUpdates, ctx.params.appId)
|
2022-05-26 21:13:26 +12:00
|
|
|
await events.app.versionReverted(app, currentVersion, revertedToVersion)
|
2021-07-08 23:56:54 +12:00
|
|
|
ctx.status = 200
|
2022-05-04 09:58:19 +12:00
|
|
|
ctx.body = app
|
2021-07-08 23:56:54 +12:00
|
|
|
}
|
|
|
|
|
2022-03-20 14:13:54 +13:00
|
|
|
const destroyApp = async (ctx: any) => {
|
2022-04-20 01:38:09 +12:00
|
|
|
let appId = ctx.params.appId
|
2022-04-20 22:08:06 +12:00
|
|
|
let isUnpublish = ctx.query && ctx.query.unpublish
|
2022-04-20 01:38:09 +12:00
|
|
|
|
2022-04-20 22:08:06 +12:00
|
|
|
if (isUnpublish) {
|
2022-04-20 01:38:09 +12:00
|
|
|
appId = getProdAppID(appId)
|
|
|
|
}
|
2021-06-10 23:07:39 +12:00
|
|
|
|
2022-04-20 22:08:06 +12:00
|
|
|
const db = isUnpublish ? getProdAppDB() : getAppDB()
|
2022-05-04 09:58:19 +12:00
|
|
|
const app = await db.get(DocumentTypes.APP_METADATA)
|
2020-10-29 09:35:06 +13:00
|
|
|
const result = await db.destroy()
|
2022-04-20 01:38:09 +12:00
|
|
|
|
2022-04-22 01:10:58 +12:00
|
|
|
if (isUnpublish) {
|
2022-03-20 14:13:54 +13:00
|
|
|
await quotas.removePublishedApp()
|
2022-05-24 09:14:44 +12:00
|
|
|
await events.app.unpublished(app)
|
2022-03-20 14:13:54 +13:00
|
|
|
} else {
|
|
|
|
await quotas.removeApp()
|
2022-05-24 09:14:44 +12:00
|
|
|
await events.app.deleted(app)
|
2022-03-20 14:13:54 +13:00
|
|
|
}
|
2022-04-22 01:10:58 +12:00
|
|
|
|
2021-03-26 02:32:05 +13:00
|
|
|
/* istanbul ignore next */
|
2022-04-20 22:08:06 +12:00
|
|
|
if (!env.isTest() && !isUnpublish) {
|
2022-04-20 01:38:09 +12:00
|
|
|
await deleteApp(appId)
|
2021-03-24 06:54:02 +13:00
|
|
|
}
|
2022-04-28 03:05:27 +12:00
|
|
|
// automations only in production
|
2022-04-20 22:08:06 +12:00
|
|
|
if (isUnpublish) {
|
2022-04-20 01:38:09 +12:00
|
|
|
await cleanupAutomations(appId)
|
2021-11-18 05:28:52 +13:00
|
|
|
}
|
2022-04-28 03:05:27 +12:00
|
|
|
// remove app role when the dev app is deleted (no trace of app anymore)
|
|
|
|
else {
|
|
|
|
await removeAppFromUserRoles(ctx, appId)
|
|
|
|
}
|
2022-04-20 01:38:09 +12:00
|
|
|
await appCache.invalidateAppMetadata(appId)
|
2022-03-20 14:13:54 +13:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
const preDestroyApp = async (ctx: any) => {
|
2022-03-22 13:23:22 +13:00
|
|
|
const rows = await getUniqueRows([ctx.params.appId])
|
2022-03-20 14:13:54 +13:00
|
|
|
ctx.rowCount = rows.length
|
|
|
|
}
|
|
|
|
|
|
|
|
const postDestroyApp = async (ctx: any) => {
|
|
|
|
const rowCount = ctx.rowCount
|
|
|
|
if (rowCount) {
|
|
|
|
await quotas.removeRows(rowCount)
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 00:44:05 +12:00
|
|
|
|
2022-03-20 14:13:54 +13:00
|
|
|
export const destroy = async (ctx: any) => {
|
|
|
|
await preDestroyApp(ctx)
|
|
|
|
const result = await destroyApp(ctx)
|
|
|
|
await postDestroyApp(ctx)
|
2020-07-08 00:44:05 +12:00
|
|
|
ctx.status = 200
|
|
|
|
ctx.body = result
|
|
|
|
}
|
|
|
|
|
2022-03-20 14:13:54 +13:00
|
|
|
export const sync = async (ctx: any, next: any) => {
|
2022-05-23 04:11:05 +12: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-23 02:34:20 +13:00
|
|
|
const appId = ctx.params.appId
|
|
|
|
if (!isDevAppID(appId)) {
|
|
|
|
ctx.throw(400, "This action cannot be performed for production apps")
|
|
|
|
}
|
2021-11-04 11:23:00 +13:00
|
|
|
|
|
|
|
// replicate prod to dev
|
2022-02-01 06:42:51 +13:00
|
|
|
const prodAppId = getProdAppID(appId)
|
2021-11-09 03:26:07 +13:00
|
|
|
|
|
|
|
try {
|
2022-01-28 07:18:31 +13:00
|
|
|
// specific case, want to make sure setup is skipped
|
2022-02-01 06:42:51 +13:00
|
|
|
const prodDb = getProdAppDB({ skip_setup: true })
|
2021-11-09 03:26:07 +13:00
|
|
|
const info = await prodDb.info()
|
|
|
|
if (info.error) throw info.error
|
|
|
|
} catch (err) {
|
|
|
|
// the database doesn't exist. Don't replicate
|
2021-11-10 05:40:31 +13:00
|
|
|
ctx.status = 200
|
2021-11-09 03:26:07 +13:00
|
|
|
ctx.body = {
|
2021-11-09 03:26:44 +13:00
|
|
|
message: "App sync not required, app not deployed.",
|
2021-11-09 03:26:07 +13:00
|
|
|
}
|
2021-11-10 05:40:31 +13:00
|
|
|
return next()
|
2021-11-09 03:26:07 +13:00
|
|
|
}
|
|
|
|
|
2021-10-23 02:34:20 +13:00
|
|
|
const replication = new Replication({
|
|
|
|
source: prodAppId,
|
|
|
|
target: appId,
|
|
|
|
})
|
|
|
|
let error
|
|
|
|
try {
|
|
|
|
await replication.replicate({
|
2022-03-20 14:13:54 +13:00
|
|
|
filter: function (doc: any) {
|
2021-10-23 02:34:20 +13:00
|
|
|
return doc._id !== DocumentTypes.APP_METADATA
|
|
|
|
},
|
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
error = err
|
2022-04-21 04:33:42 +12:00
|
|
|
} finally {
|
|
|
|
await replication.close()
|
2021-10-23 02:34:20 +13:00
|
|
|
}
|
2021-11-04 11:23:00 +13:00
|
|
|
|
|
|
|
// sync the users
|
2022-01-28 07:18:31 +13:00
|
|
|
await syncGlobalUsers()
|
2021-11-04 11:23:00 +13:00
|
|
|
|
2021-10-23 02:34:20 +13:00
|
|
|
if (error) {
|
|
|
|
ctx.throw(400, error)
|
|
|
|
} else {
|
|
|
|
ctx.body = {
|
|
|
|
message: "App sync completed successfully.",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 14:13:54 +13:00
|
|
|
const updateAppPackage = async (appPackage: any, appId: any) => {
|
2022-02-01 06:42:51 +13:00
|
|
|
const db = getAppDB()
|
2021-07-08 04:07:42 +12:00
|
|
|
const application = await db.get(DocumentTypes.APP_METADATA)
|
|
|
|
|
2022-01-22 06:31:18 +13:00
|
|
|
const newAppPackage = { ...application, ...appPackage }
|
2021-07-08 04:07:42 +12:00
|
|
|
if (appPackage._rev !== application._rev) {
|
|
|
|
newAppPackage._rev = application._rev
|
|
|
|
}
|
|
|
|
|
|
|
|
// the locked by property is attached by server but generated from
|
|
|
|
// Redis, shouldn't ever store it
|
2021-07-10 03:42:09 +12:00
|
|
|
delete newAppPackage.lockedBy
|
2021-07-08 04:07:42 +12:00
|
|
|
|
2022-05-04 09:58:19 +12:00
|
|
|
await db.put(newAppPackage)
|
2021-11-16 06:40:45 +13:00
|
|
|
// remove any cached metadata, so that it will be updated
|
|
|
|
await appCache.invalidateAppMetadata(appId)
|
2022-05-04 09:58:19 +12:00
|
|
|
return newAppPackage
|
2021-07-08 04:07:42 +12:00
|
|
|
}
|
|
|
|
|
2022-03-20 14:13:54 +13:00
|
|
|
const createEmptyAppPackage = async (ctx: any, app: any) => {
|
2022-02-01 06:42:51 +13:00
|
|
|
const db = getAppDB()
|
2020-11-03 03:53:51 +13:00
|
|
|
|
2020-11-24 05:56:35 +13:00
|
|
|
let screensAndLayouts = []
|
2020-11-24 04:46:26 +13:00
|
|
|
for (let layout of BASE_LAYOUTS) {
|
|
|
|
const cloned = cloneDeep(layout)
|
2021-01-21 02:32:15 +13:00
|
|
|
screensAndLayouts.push(await processObject(cloned, app))
|
2020-11-24 04:46:26 +13:00
|
|
|
}
|
2020-11-07 02:40:00 +13:00
|
|
|
|
2020-11-24 05:56:35 +13:00
|
|
|
await db.bulkDocs(screensAndLayouts)
|
2020-05-27 03:29:16 +12:00
|
|
|
}
|