Convert portal user store to TS

This commit is contained in:
Andrew Kingston 2025-01-07 14:06:03 +00:00
parent 5d999b8e3a
commit 52916f11a8
No known key found for this signature in database
4 changed files with 85 additions and 70 deletions

View File

@ -442,13 +442,11 @@
const onUpdateUserInvite = async (invite, role) => { const onUpdateUserInvite = async (invite, role) => {
let updateBody = { let updateBody = {
code: invite.code,
apps: { apps: {
...invite.apps, ...invite.apps,
[prodAppId]: role, [prodAppId]: role,
}, },
} }
if (role === Constants.Roles.CREATOR) { if (role === Constants.Roles.CREATOR) {
updateBody.builder = updateBody.builder || {} updateBody.builder = updateBody.builder || {}
updateBody.builder.apps = [...(updateBody.builder.apps ?? []), prodAppId] updateBody.builder.apps = [...(updateBody.builder.apps ?? []), prodAppId]
@ -456,7 +454,7 @@
} else if (role !== Constants.Roles.CREATOR && invite?.builder?.apps) { } else if (role !== Constants.Roles.CREATOR && invite?.builder?.apps) {
invite.builder.apps = [] invite.builder.apps = []
} }
await users.updateInvite(updateBody) await users.updateInvite(invite.code, updateBody)
await filterInvites(query) await filterInvites(query)
} }
@ -470,8 +468,7 @@
let updated = { ...invite } let updated = { ...invite }
delete updated.info.apps[prodAppId] delete updated.info.apps[prodAppId]
return await users.updateInvite({ return await users.updateInvite(updated.code, {
code: updated.code,
apps: updated.apps, apps: updated.apps,
}) })
} }

View File

@ -1,41 +1,68 @@
import { writable } from "svelte/store"
import { API } from "@/api" import { API } from "@/api"
import { update } from "lodash"
import { licensing } from "." import { licensing } from "."
import { sdk } from "@budibase/shared-core" import { sdk } from "@budibase/shared-core"
import { Constants } from "@budibase/frontend-core" import { Constants } from "@budibase/frontend-core"
import {
DeleteInviteUsersRequest,
InviteUsersRequest,
SearchUsersRequest,
SearchUsersResponse,
UpdateInviteRequest,
User,
} from "@budibase/types"
import { BudiStore } from "../BudiStore"
export function createUsersStore() { type UserState = SearchUsersResponse & SearchUsersRequest
const { subscribe, set } = writable({})
// opts can contain page and search params class UserStore extends BudiStore<UserState> {
async function search(opts = {}) { constructor() {
super({
data: [],
})
// Update quotas after any add or remove operation
this.create = this.refreshUsage(this.create)
this.save = this.refreshUsage(this.save)
this.delete = this.refreshUsage(this.delete)
this.bulkDelete = this.refreshUsage(this.bulkDelete)
}
async search(opts: SearchUsersRequest = {}) {
const paged = await API.searchUsers(opts) const paged = await API.searchUsers(opts)
set({ this.set({
...paged, ...paged,
...opts, ...opts,
}) })
return paged return paged
} }
async function get(userId) { async get(userId: string) {
try { try {
return await API.getUser(userId) return await API.getUser(userId)
} catch (err) { } catch (err) {
return null return null
} }
} }
const fetch = async () => {
async fetch() {
return await API.getUsers() return await API.getUsers()
} }
// One or more users. async onboard(payload: InviteUsersRequest) {
async function onboard(payload) {
return await API.onboardUsers(payload) return await API.onboardUsers(payload)
} }
async function invite(payload) { async invite(
const users = payload.map(user => { payload: {
admin?: boolean
builder?: boolean
creator?: boolean
email: string
apps?: any[]
groups?: any[]
}[]
) {
const users: InviteUsersRequest = payload.map(user => {
let builder = undefined let builder = undefined
if (user.admin || user.builder) { if (user.admin || user.builder) {
builder = { global: true } builder = { global: true }
@ -55,11 +82,16 @@ export function createUsersStore() {
return API.inviteUsers(users) return API.inviteUsers(users)
} }
async function removeInvites(payload) { async removeInvites(payload: DeleteInviteUsersRequest) {
return API.removeUserInvites(payload) return API.removeUserInvites(payload)
} }
async function acceptInvite(inviteCode, password, firstName, lastName) { async acceptInvite(
inviteCode: string,
password: string,
firstName: string,
lastName?: string
) {
return API.acceptInvite({ return API.acceptInvite({
inviteCode, inviteCode,
password, password,
@ -68,21 +100,25 @@ export function createUsersStore() {
}) })
} }
async function fetchInvite(inviteCode) { async fetchInvite(inviteCode: string) {
return API.getUserInvite(inviteCode) return API.getUserInvite(inviteCode)
} }
async function getInvites() { async getInvites() {
return API.getUserInvites() return API.getUserInvites()
} }
async function updateInvite(invite) { async updateInvite(code: string, invite: UpdateInviteRequest) {
return API.updateUserInvite(invite.code, invite) return API.updateUserInvite(code, invite)
} }
async function create(data) { async getUserCountByApp(appId: string) {
let mappedUsers = data.users.map(user => { return await API.getUserCountByApp(appId)
const body = { }
async create(data: any) {
let mappedUsers: Omit<User, "tenantId">[] = data.users.map((user: any) => {
const body: Omit<User, "tenantId"> = {
email: user.email, email: user.email,
password: user.password, password: user.password,
roles: {}, roles: {},
@ -113,41 +149,44 @@ export function createUsersStore() {
const response = await API.createUsers(mappedUsers, data.groups) const response = await API.createUsers(mappedUsers, data.groups)
// re-search from first page // re-search from first page
await search() await this.search()
return response return response
} }
async function del(id) { async delete(id: string) {
await API.deleteUser(id) await API.deleteUser(id)
update(users => users.filter(user => user._id !== id))
} }
async function getUserCountByApp(appId) { async bulkDelete(
return await API.getUserCountByApp(appId) users: Array<{
} userId: string
email: string
async function bulkDelete(users) { }>
) {
return API.deleteUsers(users) return API.deleteUsers(users)
} }
async function save(user) { async save(user: User) {
return await API.saveUser(user) return await API.saveUser(user)
} }
async function addAppBuilder(userId, appId) { async addAppBuilder(userId: string, appId: string) {
return await API.addAppBuilder(userId, appId) return await API.addAppBuilder(userId, appId)
} }
async function removeAppBuilder(userId, appId) { async removeAppBuilder(userId: string, appId: string) {
return await API.removeAppBuilder(userId, appId) return await API.removeAppBuilder(userId, appId)
} }
async function getAccountHolder() { async getAccountHolder() {
return await API.getAccountHolder() return await API.getAccountHolder()
} }
const getUserRole = user => { getUserRole(user?: User & { tenantOwnerEmail?: string }) {
if (user && user.email === user.tenantOwnerEmail) { if (!user) {
return Constants.BudibaseRoles.AppUser
}
if (user.email === user.tenantOwnerEmail) {
return Constants.BudibaseRoles.Owner return Constants.BudibaseRoles.Owner
} else if (sdk.users.isAdmin(user)) { } else if (sdk.users.isAdmin(user)) {
return Constants.BudibaseRoles.Admin return Constants.BudibaseRoles.Admin
@ -160,37 +199,16 @@ export function createUsersStore() {
} }
} }
const refreshUsage = foo = this.refreshUsage(this.create)
fn => bar = this.refreshUsage(this.save)
async (...args) => {
refreshUsage<T extends any[], U>(fn: (...args: T) => Promise<U>) {
return async function (...args: T) {
const response = await fn(...args) const response = await fn(...args)
await licensing.setQuotaUsage() await licensing.setQuotaUsage()
return response return response
} }
return {
subscribe,
search,
get,
getUserRole,
fetch,
invite,
onboard,
fetchInvite,
getInvites,
removeInvites,
updateInvite,
getUserCountByApp,
addAppBuilder,
removeAppBuilder,
// any operation that adds or deletes users
acceptInvite,
create: refreshUsage(create),
save: refreshUsage(save),
bulkDelete: refreshUsage(bulkDelete),
delete: refreshUsage(del),
getAccountHolder,
} }
} }
export const users = createUsersStore() export const users = new UserStore()

View File

@ -60,7 +60,7 @@ export interface UserEndpoints {
getAccountHolder: () => Promise<LookupAccountHolderResponse> getAccountHolder: () => Promise<LookupAccountHolderResponse>
searchUsers: (data: SearchUsersRequest) => Promise<SearchUsersResponse> searchUsers: (data: SearchUsersRequest) => Promise<SearchUsersResponse>
createUsers: ( createUsers: (
users: User[], users: Omit<User, "tenantId">[],
groups: any[] groups: any[]
) => Promise<BulkUserCreated | undefined> ) => Promise<BulkUserCreated | undefined>
updateUserInvite: ( updateUserInvite: (

View File

@ -124,7 +124,7 @@ export interface AcceptUserInviteRequest {
inviteCode: string inviteCode: string
password: string password: string
firstName: string firstName: string
lastName: string lastName?: string
} }
export interface AcceptUserInviteResponse { export interface AcceptUserInviteResponse {