2022-02-24 07:31:32 +13:00
|
|
|
const { getAllApps } = require("@budibase/backend-core/db")
|
|
|
|
const { updateAppId } = require("@budibase/backend-core/context")
|
2022-03-02 07:35:08 +13:00
|
|
|
import { search as stringSearch } from "./utils"
|
2022-02-25 04:13:14 +13:00
|
|
|
import { default as controller } from "../application"
|
|
|
|
import { Application } from "../../../definitions/common"
|
2022-02-22 08:04:13 +13:00
|
|
|
|
2022-02-25 04:13:14 +13:00
|
|
|
function fixAppID(app: Application, params: any) {
|
|
|
|
if (!params) {
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
if (!app._id && params.appId) {
|
|
|
|
app._id = params.appId
|
|
|
|
}
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setResponseApp(ctx: any) {
|
2022-02-24 07:31:32 +13:00
|
|
|
if (ctx.body && ctx.body.appId && (!ctx.params || !ctx.params.appId)) {
|
|
|
|
ctx.params = { appId: ctx.body.appId }
|
|
|
|
}
|
|
|
|
await controller.fetchAppPackage(ctx)
|
|
|
|
}
|
2022-02-22 08:04:13 +13:00
|
|
|
|
2022-03-02 07:35:08 +13:00
|
|
|
export async function search(ctx: any, next: any) {
|
2022-02-24 07:31:32 +13:00
|
|
|
const { name } = ctx.request.body
|
|
|
|
const apps = await getAllApps({ all: true })
|
2022-03-02 03:37:35 +13:00
|
|
|
ctx.body = stringSearch(apps, name)
|
2022-03-02 07:35:08 +13:00
|
|
|
await next()
|
2022-02-24 07:31:32 +13:00
|
|
|
}
|
2022-02-22 08:04:13 +13:00
|
|
|
|
2022-03-02 07:35:08 +13:00
|
|
|
export async function create(ctx: any, next: any) {
|
2022-02-26 04:55:19 +13:00
|
|
|
if (!ctx.request.body || !ctx.request.body.useTemplate) {
|
|
|
|
ctx.request.body = {
|
|
|
|
useTemplate: false,
|
|
|
|
...ctx.request.body,
|
|
|
|
}
|
|
|
|
}
|
2022-02-24 07:31:32 +13:00
|
|
|
await controller.create(ctx)
|
|
|
|
await setResponseApp(ctx)
|
2022-03-02 07:35:08 +13:00
|
|
|
await next()
|
2022-02-24 07:31:32 +13:00
|
|
|
}
|
2022-02-22 08:04:13 +13:00
|
|
|
|
2022-03-02 07:35:08 +13:00
|
|
|
export async function read(ctx: any, next: any) {
|
2022-02-24 07:31:32 +13:00
|
|
|
updateAppId(ctx.params.appId)
|
|
|
|
await setResponseApp(ctx)
|
2022-03-02 07:35:08 +13:00
|
|
|
await next()
|
2022-02-24 07:31:32 +13:00
|
|
|
}
|
|
|
|
|
2022-03-02 07:35:08 +13:00
|
|
|
export async function update(ctx: any, next: any) {
|
2022-02-25 04:13:14 +13:00
|
|
|
ctx.request.body = fixAppID(ctx.request.body, ctx.params)
|
2022-02-24 07:31:32 +13:00
|
|
|
updateAppId(ctx.params.appId)
|
|
|
|
await controller.update(ctx)
|
|
|
|
await setResponseApp(ctx)
|
2022-03-02 07:35:08 +13:00
|
|
|
await next()
|
2022-02-24 07:31:32 +13:00
|
|
|
}
|
|
|
|
|
2022-03-02 07:35:08 +13:00
|
|
|
export async function destroy(ctx: any, next: any) {
|
2022-02-24 07:31:32 +13:00
|
|
|
updateAppId(ctx.params.appId)
|
|
|
|
// get the app before deleting it
|
|
|
|
await setResponseApp(ctx)
|
|
|
|
const body = ctx.body
|
|
|
|
await controller.delete(ctx)
|
|
|
|
// overwrite the body again
|
|
|
|
ctx.body = body
|
2022-03-02 07:35:08 +13:00
|
|
|
await next()
|
2022-02-24 07:31:32 +13:00
|
|
|
}
|
2022-02-25 04:13:14 +13:00
|
|
|
|
|
|
|
export default {
|
|
|
|
create,
|
|
|
|
update,
|
|
|
|
read,
|
|
|
|
destroy,
|
|
|
|
search,
|
|
|
|
}
|