1
0
Fork 0
mirror of synced 2024-07-02 21:10:43 +12:00
budibase/packages/worker/src/api/controllers/admin/groups.js

60 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-04-19 22:34:07 +12:00
const CouchDB = require("../../../db")
2021-04-22 03:46:51 +12:00
const {
getGroupParams,
generateGroupID,
StaticDatabases,
} = require("@budibase/auth").db
2021-04-20 03:16:46 +12:00
const GLOBAL_DB = StaticDatabases.GLOBAL.name
2021-05-03 19:31:09 +12:00
exports.save = async function (ctx) {
2021-04-20 03:16:46 +12:00
const db = new CouchDB(GLOBAL_DB)
const groupDoc = ctx.request.body
// Group does not exist yet
if (!groupDoc._id) {
groupDoc._id = generateGroupID()
}
try {
const response = await db.post(groupDoc)
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-04-20 03:16:46 +12:00
const db = new CouchDB(GLOBAL_DB)
const response = await db.allDocs(
getGroupParams(undefined, {
include_docs: true,
})
)
2021-05-03 19:31:09 +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-04-20 03:16:46 +12:00
const db = new CouchDB(GLOBAL_DB)
try {
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-04-20 03:16:46 +12:00
const db = new CouchDB(GLOBAL_DB)
const { id, rev } = ctx.params
try {
await db.remove(id, rev)
ctx.body = { message: "Group deleted successfully" }
} catch (err) {
ctx.throw(err.status, err)
}
2021-04-19 22:34:07 +12:00
}