fixed duplicating users in user creation frontend
This commit is contained in:
parent
6758574e7f
commit
6b83fcb8d9
|
@ -8,12 +8,10 @@
|
||||||
Layout,
|
Layout,
|
||||||
} from "@budibase/bbui"
|
} from "@budibase/bbui"
|
||||||
import { groups } from "stores/portal"
|
import { groups } from "stores/portal"
|
||||||
import { createEventDispatcher } from "svelte"
|
|
||||||
import { Constants } from "@budibase/frontend-core"
|
import { Constants } from "@budibase/frontend-core"
|
||||||
|
|
||||||
export let showOnboardingTypeModal
|
export let showOnboardingTypeModal
|
||||||
const password = Math.random().toString(36).substring(2, 22)
|
const password = Math.random().toString(36).substring(2, 22)
|
||||||
const dispatch = createEventDispatcher()
|
|
||||||
let disabled
|
let disabled
|
||||||
let userGroups = []
|
let userGroups = []
|
||||||
|
|
||||||
|
@ -39,10 +37,8 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<ModalContent
|
<ModalContent
|
||||||
onConfirm={() => {
|
onConfirm={async () =>
|
||||||
showOnboardingTypeModal()
|
showOnboardingTypeModal({ users: userData, groups: userGroups })}
|
||||||
dispatch("change", { users: userData, groups: userGroups })
|
|
||||||
}}
|
|
||||||
size="M"
|
size="M"
|
||||||
title="Add new user"
|
title="Add new user"
|
||||||
confirmText="Add user"
|
confirmText="Add user"
|
||||||
|
|
|
@ -97,12 +97,15 @@
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
function showOnboardingTypeModal() {
|
const showOnboardingTypeModal = async addUsersData => {
|
||||||
|
userData = await removingDuplicities(addUsersData)
|
||||||
|
if (!userData?.users?.length) return
|
||||||
|
|
||||||
onboardingTypeModal.show()
|
onboardingTypeModal.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createUserFlow() {
|
async function createUserFlow() {
|
||||||
let emails = userData.map(x => x.email)
|
let emails = userData?.users?.map(x => x.email) || []
|
||||||
try {
|
try {
|
||||||
const res = await users.invite({
|
const res = await users.invite({
|
||||||
emails: emails,
|
emails: emails,
|
||||||
|
@ -117,8 +120,29 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const removingDuplicities = async userData => {
|
||||||
|
const currentUserEmails = (await users.fetch())?.map(x => x.email) || []
|
||||||
|
const newUsers = []
|
||||||
|
|
||||||
|
for (const user of userData?.users) {
|
||||||
|
const { email } = user
|
||||||
|
|
||||||
|
if (
|
||||||
|
newUsers.find(x => x.email === email) ||
|
||||||
|
currentUserEmails.includes(email)
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
|
newUsers.push(user)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!newUsers.length)
|
||||||
|
notifications.info("Duplicated! There is no new users to add.")
|
||||||
|
return { ...userData, users: newUsers }
|
||||||
|
}
|
||||||
|
|
||||||
const createUsersFromCsv = async userCsvData => {
|
const createUsersFromCsv = async userCsvData => {
|
||||||
const { userEmails, usersRole, userGroups } = userCsvData
|
const { userEmails, usersRole, userGroups: groups } = userCsvData
|
||||||
|
|
||||||
const users = []
|
const users = []
|
||||||
for (const email of userEmails) {
|
for (const email of userEmails) {
|
||||||
|
@ -132,13 +156,15 @@
|
||||||
users.push(newUser)
|
users.push(newUser)
|
||||||
}
|
}
|
||||||
|
|
||||||
userData = { groups: userGroups, users: users }
|
userData = await removingDuplicities({ groups, users })
|
||||||
|
if (!userData.users.length) return
|
||||||
|
|
||||||
return createUser()
|
return createUser()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createUser() {
|
async function createUser() {
|
||||||
try {
|
try {
|
||||||
await users.create(userData)
|
await users.create(await removingDuplicities(userData))
|
||||||
notifications.success("Successfully created user")
|
notifications.success("Successfully created user")
|
||||||
await groups.actions.init()
|
await groups.actions.init()
|
||||||
passwordModal.show()
|
passwordModal.show()
|
||||||
|
@ -245,14 +271,7 @@
|
||||||
</Layout>
|
</Layout>
|
||||||
|
|
||||||
<Modal bind:this={createUserModal}>
|
<Modal bind:this={createUserModal}>
|
||||||
<AddUserModal
|
<AddUserModal {showOnboardingTypeModal} />
|
||||||
on:change={e => (userData = e.detail)}
|
|
||||||
on:created={async () => {
|
|
||||||
pageInfo.reset()
|
|
||||||
await fetchUsers()
|
|
||||||
}}
|
|
||||||
{showOnboardingTypeModal}
|
|
||||||
/>
|
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
||||||
<Modal bind:this={inviteConfirmationModal}>
|
<Modal bind:this={inviteConfirmationModal}>
|
||||||
|
|
|
@ -23,6 +23,10 @@ export function createUsersStore() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fetch = async () => {
|
||||||
|
return await API.getUsers()
|
||||||
|
}
|
||||||
|
|
||||||
async function invite({ emails, builder, admin }) {
|
async function invite({ emails, builder, admin }) {
|
||||||
return API.inviteUsers({
|
return API.inviteUsers({
|
||||||
emails,
|
emails,
|
||||||
|
@ -83,6 +87,7 @@ export function createUsersStore() {
|
||||||
subscribe,
|
subscribe,
|
||||||
search,
|
search,
|
||||||
get,
|
get,
|
||||||
|
fetch,
|
||||||
invite,
|
invite,
|
||||||
acceptInvite,
|
acceptInvite,
|
||||||
create,
|
create,
|
||||||
|
|
Loading…
Reference in New Issue