2021-09-09 06:29:28 +12:00
|
|
|
const { MetadataTypes } = require("../../constants")
|
|
|
|
const { generateMetadataID } = require("../../db/utils")
|
2021-09-14 05:03:09 +12:00
|
|
|
const { saveEntityMetadata, deleteEntityMetadata } = require("../../utilities")
|
2022-01-28 13:05:39 +13:00
|
|
|
const { getAppDB } = require("@budibase/backend-core/context")
|
2021-09-09 06:29:28 +12:00
|
|
|
|
|
|
|
exports.getTypes = async ctx => {
|
|
|
|
ctx.body = {
|
|
|
|
types: MetadataTypes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.saveMetadata = async ctx => {
|
|
|
|
const { type, entityId } = ctx.params
|
|
|
|
if (type === MetadataTypes.AUTOMATION_TEST_HISTORY) {
|
|
|
|
ctx.throw(400, "Cannot save automation history type")
|
|
|
|
}
|
2022-01-28 13:05:39 +13:00
|
|
|
ctx.body = await saveEntityMetadata(type, entityId, ctx.request.body)
|
2021-09-09 06:29:28 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.deleteMetadata = async ctx => {
|
|
|
|
const { type, entityId } = ctx.params
|
2022-01-28 13:05:39 +13:00
|
|
|
await deleteEntityMetadata(type, entityId)
|
2021-09-09 06:29:28 +12:00
|
|
|
ctx.body = {
|
2021-09-11 00:52:41 +12:00
|
|
|
message: "Metadata deleted successfully",
|
2021-09-09 06:29:28 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.getMetadata = async ctx => {
|
|
|
|
const { type, entityId } = ctx.params
|
2022-01-28 13:05:39 +13:00
|
|
|
const db = getAppDB()
|
2021-09-09 06:29:28 +12:00
|
|
|
const id = generateMetadataID(type, entityId)
|
2021-09-11 00:52:41 +12:00
|
|
|
try {
|
|
|
|
ctx.body = await db.get(id)
|
|
|
|
} catch (err) {
|
|
|
|
if (err.status === 404) {
|
|
|
|
ctx.body = {}
|
|
|
|
} else {
|
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
|
|
|
}
|
2021-09-09 06:29:28 +12:00
|
|
|
}
|