2021-08-05 20:59:08 +12:00
|
|
|
const { getWorkspaceParams, generateWorkspaceID } = require("@budibase/auth/db")
|
|
|
|
const { getGlobalDB } = require("@budibase/auth/tenancy")
|
2021-04-20 03:16:46 +12:00
|
|
|
|
2021-05-03 19:31:09 +12:00
|
|
|
exports.save = async function (ctx) {
|
2021-08-05 20:59:08 +12:00
|
|
|
const db = getGlobalDB()
|
|
|
|
const workspaceDoc = ctx.request.body
|
2021-04-20 03:16:46 +12:00
|
|
|
|
2021-08-05 20:59:08 +12:00
|
|
|
// workspace does not exist yet
|
|
|
|
if (!workspaceDoc._id) {
|
|
|
|
workspaceDoc._id = generateWorkspaceID()
|
2021-04-20 03:16:46 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-09-22 03:07:04 +12:00
|
|
|
const response = await db.put(workspaceDoc)
|
2021-04-20 03:16:46 +12:00
|
|
|
ctx.body = {
|
|
|
|
_id: response.id,
|
|
|
|
_rev: response.rev,
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(err.status, err)
|
2021-04-19 22:38:54 +12:00
|
|
|
}
|
2021-04-19 22:34:07 +12:00
|
|
|
}
|
|
|
|
|
2021-05-03 19:31:09 +12:00
|
|
|
exports.fetch = async function (ctx) {
|
2021-08-05 20:59:08 +12:00
|
|
|
const db = getGlobalDB()
|
2021-04-20 03:16:46 +12:00
|
|
|
const response = await db.allDocs(
|
2021-08-05 20:59:08 +12:00
|
|
|
getWorkspaceParams(undefined, {
|
2021-04-20 03:16:46 +12:00
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2021-05-04 22:32:22 +12:00
|
|
|
ctx.body = response.rows.map(row => row.doc)
|
2021-04-19 22:34:07 +12:00
|
|
|
}
|
|
|
|
|
2021-05-03 19:31:09 +12:00
|
|
|
exports.find = async function (ctx) {
|
2021-08-05 20:59:08 +12:00
|
|
|
const db = getGlobalDB()
|
2021-04-20 03:16:46 +12:00
|
|
|
try {
|
2021-04-21 04:17:44 +12:00
|
|
|
ctx.body = await db.get(ctx.params.id)
|
2021-04-20 03:16:46 +12:00
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
2021-04-19 22:34:07 +12:00
|
|
|
}
|
|
|
|
|
2021-05-03 19:31:09 +12:00
|
|
|
exports.destroy = async function (ctx) {
|
2021-08-05 20:59:08 +12:00
|
|
|
const db = getGlobalDB()
|
2021-04-20 03:16:46 +12:00
|
|
|
const { id, rev } = ctx.params
|
|
|
|
|
|
|
|
try {
|
|
|
|
await db.remove(id, rev)
|
2021-08-05 20:59:08 +12:00
|
|
|
ctx.body = { message: "Workspace deleted successfully" }
|
2021-04-20 03:16:46 +12:00
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
2021-04-19 22:34:07 +12:00
|
|
|
}
|