saving and fetching groups from couch
This commit is contained in:
parent
d529d7456f
commit
b7cc71a623
|
@ -3,5 +3,8 @@
|
|||
"editor.codeActionsOnSave": {
|
||||
"source.fixAll": true
|
||||
},
|
||||
"editor.defaultFormatter": "svelte.svelte-vscode"
|
||||
"editor.defaultFormatter": "svelte.svelte-vscode",
|
||||
"[javascript]": {
|
||||
"editor.defaultFormatter": "vscode.typescript-language-features"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ exports.DocumentTypes = {
|
|||
ROLE: "role",
|
||||
MIGRATIONS: "migrations",
|
||||
DEV_INFO: "devinfo",
|
||||
GROUP: "gr"
|
||||
}
|
||||
|
||||
exports.StaticDatabases = {
|
||||
|
|
|
@ -148,6 +148,29 @@ exports.getTemplateParams = (ownerId, templateId, otherProps = {}) => {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a new user group ID
|
||||
* @returns {string} The new user group ID which info can be stored under.
|
||||
*/
|
||||
exports.generateUserGroupID = () => {
|
||||
return `${DocumentTypes.GROUP}${SEPARATOR}${newid()}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets parameters for retrieving groups.
|
||||
*/
|
||||
exports.getUserGroupsParams = (groupId, otherProps = {}) => {
|
||||
if (!groupId) {
|
||||
groupId = ""
|
||||
}
|
||||
return {
|
||||
...otherProps,
|
||||
startkey: `${DocumentTypes.GROUP}${SEPARATOR}${groupId}`,
|
||||
endkey: `${DocumentTypes.GROUP}${SEPARATOR}${groupId}${UNICODE_MAX}`,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates a new role ID.
|
||||
* @returns {string} The new role ID which the role doc can be stored under.
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
<div class="desktop tableElement">
|
||||
<Icon name="User" />
|
||||
<div style="margin-left: var(--spacing-l">
|
||||
{parseInt(userCount)} app{parseInt(userCount) === 1 ? "" : "s"}
|
||||
{parseInt(userCount)} user{parseInt(userCount) === 1 ? "" : "s"}
|
||||
</div>
|
||||
</div>
|
||||
<div class="desktop tableElement">
|
||||
|
|
|
@ -12,30 +12,37 @@
|
|||
Tags,
|
||||
IconPicker,
|
||||
} from "@budibase/bbui"
|
||||
import { API } from "api"
|
||||
import { groups } from "stores/portal"
|
||||
import { onMount } from "svelte"
|
||||
|
||||
import UserGroupsRow from "./_components/UserGroupsRow.svelte"
|
||||
|
||||
let modal
|
||||
let selectedColor
|
||||
let selectedIcon
|
||||
let groupName
|
||||
let proPlan = true
|
||||
|
||||
let userGroupData = [
|
||||
{
|
||||
_id: "gr_123456",
|
||||
color: "green",
|
||||
icon: "Anchor",
|
||||
name: "Core Team",
|
||||
userCount: 5,
|
||||
appCount: 2,
|
||||
},
|
||||
{
|
||||
_id: "gr_45678",
|
||||
color: "red",
|
||||
icon: "Beaker",
|
||||
name: "QA Team",
|
||||
userCount: 3,
|
||||
appCount: 7,
|
||||
},
|
||||
]
|
||||
async function saveConfig() {
|
||||
try {
|
||||
API.saveGroup({
|
||||
color: selectedColor,
|
||||
icon: selectedIcon,
|
||||
name: groupName,
|
||||
})
|
||||
} catch (error) {
|
||||
notifications.error(`Failed to save group`)
|
||||
}
|
||||
}
|
||||
|
||||
onMount(async () => {
|
||||
try {
|
||||
await groups.init()
|
||||
} catch (error) {
|
||||
notifications.error("Error getting User groups")
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<Layout noPadding>
|
||||
|
@ -72,7 +79,7 @@
|
|||
</div>
|
||||
|
||||
<div class="groupTable">
|
||||
{#each userGroupData as group}
|
||||
{#each $groups as group}
|
||||
<div>
|
||||
<UserGroupsRow {group} />
|
||||
</div>
|
||||
|
@ -81,8 +88,13 @@
|
|||
</Layout>
|
||||
|
||||
<Modal bind:this={modal}>
|
||||
<ModalContent size="M" title="Create User Group" confirmText="Save">
|
||||
<Input label="Team name" />
|
||||
<ModalContent
|
||||
onConfirm={saveConfig}
|
||||
size="M"
|
||||
title="Create User Group"
|
||||
confirmText="Save"
|
||||
>
|
||||
<Input bind:value={groupName} label="Team name" />
|
||||
<div class="modal-format">
|
||||
<div class="modal-inner">
|
||||
<Body size="XS">Icon</Body>
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
import { writable } from "svelte/store"
|
||||
import { API } from "api"
|
||||
import { update } from "lodash"
|
||||
|
||||
export function createGroupsStore() {
|
||||
const { subscribe, set } = writable([])
|
||||
|
||||
async function init() {
|
||||
const users = await API.getGroups()
|
||||
set(users)
|
||||
}
|
||||
|
||||
async function save(data) {
|
||||
await API.saveGroup(data)
|
||||
}
|
||||
|
||||
return {
|
||||
subscribe,
|
||||
init,
|
||||
save,
|
||||
}
|
||||
}
|
||||
|
||||
export const groups = createGroupsStore()
|
|
@ -7,3 +7,4 @@ export { auth } from "./auth"
|
|||
export { oidc } from "./oidc"
|
||||
export { templates } from "./templates"
|
||||
export { licensing } from "./licensing"
|
||||
export { groups } from "./groups"
|
|
@ -0,0 +1,18 @@
|
|||
export const buildGroupsEndpoints = API => ({
|
||||
/**
|
||||
* Creates or updates a user in the current tenant.
|
||||
* @param user the new user to create
|
||||
*/
|
||||
saveGroup: async group => {
|
||||
return await API.post({
|
||||
url: "/api/global/groups",
|
||||
body: group,
|
||||
})
|
||||
},
|
||||
getGroups: async () => {
|
||||
return await API.get({
|
||||
url: "/api/global/groups",
|
||||
})
|
||||
|
||||
}
|
||||
})
|
|
@ -23,6 +23,7 @@ import { buildUserEndpoints } from "./user"
|
|||
import { buildSelfEndpoints } from "./self"
|
||||
import { buildViewEndpoints } from "./views"
|
||||
import { buildLicensingEndpoints } from "./licensing"
|
||||
import { buildGroupsEndpoints } from "./groups"
|
||||
|
||||
const defaultAPIClientConfig = {
|
||||
/**
|
||||
|
@ -235,5 +236,6 @@ export const createAPIClient = config => {
|
|||
...buildViewEndpoints(API),
|
||||
...buildSelfEndpoints(API),
|
||||
...buildLicensingEndpoints(API),
|
||||
...buildGroupsEndpoints(API)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
const {
|
||||
generateUserGroupID,
|
||||
getUserGroupsParams
|
||||
} = require("@budibase/backend-core/db")
|
||||
const { Configs } = require("../../../constants")
|
||||
const email = require("../../../utilities/email")
|
||||
const { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy")
|
||||
const env = require("../../../environment")
|
||||
const {
|
||||
withCache,
|
||||
CacheKeys,
|
||||
bustCache,
|
||||
} = require("@budibase/backend-core/cache")
|
||||
|
||||
|
||||
exports.save = async function (ctx) {
|
||||
const db = getGlobalDB()
|
||||
|
||||
// Config does not exist yet
|
||||
if (!ctx.request.body._id) {
|
||||
ctx.request.body._id = generateUserGroupID(ctx.request.body.name)
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await db.put(ctx.request.body)
|
||||
ctx.body = {
|
||||
_id: response.id,
|
||||
_rev: response.rev,
|
||||
}
|
||||
} catch (err) {
|
||||
ctx.throw(400, err)
|
||||
}
|
||||
}
|
||||
|
||||
exports.fetch = async function (ctx) {
|
||||
const db = getGlobalDB()
|
||||
console.log('in here')
|
||||
const response = await db.allDocs(
|
||||
getUserGroupsParams(null, {
|
||||
include_docs: true,
|
||||
})
|
||||
)
|
||||
ctx.body = response.rows.map(row => row.doc)
|
||||
|
||||
}
|
||||
|
||||
|
||||
exports.destroy = async function (ctx) {
|
||||
const db = getGlobalDB()
|
||||
const { id, rev } = ctx.params
|
||||
|
||||
try {
|
||||
await db.remove(id, rev)
|
||||
ctx.body = { message: "Group deleted successfully" }
|
||||
} catch (err) {
|
||||
ctx.throw(err.status, err)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
const Router = require("@koa/router")
|
||||
const controller = require("../../controllers/global/groups")
|
||||
const joiValidator = require("../../../middleware/joi-validator")
|
||||
const adminOnly = require("../../../middleware/adminOnly")
|
||||
const Joi = require("joi")
|
||||
|
||||
const router = Router()
|
||||
|
||||
function buildGroupSaveValidation() {
|
||||
// prettier-ignore
|
||||
return joiValidator.body(Joi.object({
|
||||
color: Joi.string().required(),
|
||||
icon: Joi.string().required(),
|
||||
name: Joi.string().required()
|
||||
}).required())
|
||||
}
|
||||
|
||||
router.post(
|
||||
"/api/global/groups",
|
||||
adminOnly,
|
||||
buildGroupSaveValidation(),
|
||||
controller.save
|
||||
)
|
||||
.get("/api/global/groups", controller.fetch)
|
||||
|
||||
|
||||
module.exports = router
|
|
@ -11,7 +11,7 @@ const tenantsRoutes = require("./system/tenants")
|
|||
const statusRoutes = require("./system/status")
|
||||
const selfRoutes = require("./global/self")
|
||||
const licenseRoutes = require("./global/license")
|
||||
|
||||
const userGroupRoutes = require("./global/groups")
|
||||
exports.routes = [
|
||||
configRoutes,
|
||||
userRoutes,
|
||||
|
@ -26,4 +26,5 @@ exports.routes = [
|
|||
statusRoutes,
|
||||
selfRoutes,
|
||||
licenseRoutes,
|
||||
userGroupRoutes
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue