2021-04-20 19:14:36 +02:00
|
|
|
const CouchDB = require("../../../db")
|
2021-04-21 13:12:22 +02:00
|
|
|
const { StaticDatabases, DocumentTypes } = require("@budibase/auth")
|
|
|
|
const { generateConfigID, getConfigParams } = require("@budibase/auth")
|
|
|
|
const { SEPARATOR } = require("@budibase/auth/src/db/utils")
|
|
|
|
const { Configs } = require("../../../constants")
|
2021-04-20 19:14:36 +02:00
|
|
|
|
|
|
|
const GLOBAL_DB = StaticDatabases.GLOBAL.name
|
|
|
|
|
2021-04-21 13:12:22 +02:00
|
|
|
exports.configStatus = async function(ctx) {
|
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
|
|
|
let configured = {}
|
|
|
|
|
|
|
|
// check for super admin user
|
|
|
|
try {
|
|
|
|
configured.user = true
|
|
|
|
} catch (err) {
|
|
|
|
configured.user = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for SMTP config
|
|
|
|
try {
|
|
|
|
const response = await db.allDocs(
|
|
|
|
getConfigParams(`${DocumentTypes.CONFIG}${SEPARATOR}${Configs.SMTP}`)
|
|
|
|
)
|
|
|
|
console.log(response)
|
|
|
|
configured.smtp = true
|
|
|
|
} catch (err) {
|
|
|
|
configured.smtp = false
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.body = configured
|
|
|
|
}
|
|
|
|
|
2021-04-20 19:14:36 +02:00
|
|
|
exports.save = async function(ctx) {
|
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
|
|
|
const configDoc = ctx.request.body
|
|
|
|
|
|
|
|
// Config does not exist yet
|
|
|
|
if (!configDoc._id) {
|
2021-04-21 13:12:22 +02:00
|
|
|
configDoc._id = generateConfigID(
|
|
|
|
configDoc.type,
|
|
|
|
configDoc.group,
|
|
|
|
configDoc.user
|
|
|
|
)
|
2021-04-20 19:14:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await db.post(configDoc)
|
|
|
|
ctx.body = {
|
|
|
|
type: configDoc.type,
|
|
|
|
_id: response.id,
|
|
|
|
_rev: response.rev,
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.fetch = async function(ctx) {
|
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
|
|
|
const response = await db.allDocs(
|
|
|
|
getConfigParams(undefined, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
const groups = response.rows.map(row => row.doc)
|
|
|
|
ctx.body = groups
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.find = async function(ctx) {
|
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
2021-04-21 19:40:32 +02:00
|
|
|
const response = await db.allDocs(
|
|
|
|
getConfigParams(undefined, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
const groups = response.rows.map(row => row.doc)
|
|
|
|
ctx.body = groups
|
2021-04-20 19:14:36 +02:00
|
|
|
try {
|
|
|
|
const record = await db.get(ctx.params.id)
|
|
|
|
ctx.body = record
|
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.destroy = async function(ctx) {
|
|
|
|
const db = new CouchDB(GLOBAL_DB)
|
|
|
|
const { id, rev } = ctx.params
|
|
|
|
|
|
|
|
try {
|
|
|
|
await db.remove(id, rev)
|
|
|
|
ctx.body = { message: "Config deleted successfully" }
|
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
|
|
|
}
|