2022-02-24 07:31:32 +13:00
|
|
|
const { getAllApps } = require("@budibase/backend-core/db")
|
2022-07-22 03:39:55 +12:00
|
|
|
const { doInAppContext } = require("@budibase/backend-core/context")
|
2022-07-06 05:52:53 +12:00
|
|
|
import { search as stringSearch, addRev } from "./utils"
|
2022-03-22 06:13:16 +13:00
|
|
|
import * as controller from "../application"
|
2022-02-25 04:13:14 +13:00
|
|
|
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-07-22 03:39:55 +12:00
|
|
|
await doInAppContext(ctx.params.appId, async () => {
|
|
|
|
await setResponseApp(ctx)
|
|
|
|
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-07-06 05:52:53 +12:00
|
|
|
ctx.request.body = await addRev(fixAppID(ctx.request.body, ctx.params))
|
2022-07-22 03:39:55 +12:00
|
|
|
await doInAppContext(ctx.params.appId, async () => {
|
|
|
|
await controller.update(ctx)
|
|
|
|
await setResponseApp(ctx)
|
|
|
|
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-07-22 03:39:55 +12:00
|
|
|
await doInAppContext(ctx.params.appId, async () => {
|
|
|
|
// get the app before deleting it
|
|
|
|
await setResponseApp(ctx)
|
|
|
|
const body = ctx.body
|
|
|
|
await controller.destroy(ctx)
|
|
|
|
// overwrite the body again
|
|
|
|
ctx.body = body
|
|
|
|
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,
|
|
|
|
}
|