Refactor onboardUsers endpoint.
This commit is contained in:
parent
b29cfc600c
commit
822c03b0ef
|
@ -10,6 +10,7 @@ export interface SaveUserResponse {
|
||||||
export interface UserDetails {
|
export interface UserDetails {
|
||||||
_id: string
|
_id: string
|
||||||
email: string
|
email: string
|
||||||
|
password?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface BulkUserRequest {
|
export interface BulkUserRequest {
|
||||||
|
|
|
@ -251,58 +251,32 @@ export const tenantUserLookup = async (ctx: any) => {
|
||||||
Encapsulate the app user onboarding flows here.
|
Encapsulate the app user onboarding flows here.
|
||||||
*/
|
*/
|
||||||
export const onboardUsers = async (ctx: Ctx<InviteUsersRequest>) => {
|
export const onboardUsers = async (ctx: Ctx<InviteUsersRequest>) => {
|
||||||
const request = ctx.request.body
|
if (await isEmailConfigured()) {
|
||||||
const isBulkCreate = "create" in request
|
await inviteMultiple(ctx)
|
||||||
|
return
|
||||||
const emailConfigured = await isEmailConfigured()
|
|
||||||
|
|
||||||
let onboardingResponse
|
|
||||||
|
|
||||||
if (isBulkCreate) {
|
|
||||||
// @ts-ignore
|
|
||||||
const { users, groups, roles } = request.create
|
|
||||||
const assignUsers = users.map((user: User) => (user.roles = roles))
|
|
||||||
onboardingResponse = await userSdk.db.bulkCreate(assignUsers, groups)
|
|
||||||
ctx.body = onboardingResponse
|
|
||||||
} else if (emailConfigured) {
|
|
||||||
onboardingResponse = await inviteMultiple(ctx)
|
|
||||||
} else if (!emailConfigured) {
|
|
||||||
const inviteRequest = ctx.request.body
|
|
||||||
|
|
||||||
let createdPasswords: any = {}
|
|
||||||
|
|
||||||
const users: User[] = inviteRequest.map(invite => {
|
|
||||||
let password = Math.random().toString(36).substring(2, 22)
|
|
||||||
|
|
||||||
// Temp password to be passed to the user.
|
|
||||||
createdPasswords[invite.email] = password
|
|
||||||
|
|
||||||
return {
|
|
||||||
email: invite.email,
|
|
||||||
password,
|
|
||||||
forceResetPassword: true,
|
|
||||||
roles: invite.userInfo.apps,
|
|
||||||
admin: invite.userInfo.admin,
|
|
||||||
builder: invite.userInfo.builder,
|
|
||||||
tenantId: tenancy.getTenantId(),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
let bulkCreateReponse = await userSdk.db.bulkCreate(users, [])
|
|
||||||
|
|
||||||
// Apply temporary credentials
|
|
||||||
ctx.body = {
|
|
||||||
...bulkCreateReponse,
|
|
||||||
successful: bulkCreateReponse?.successful.map(user => {
|
|
||||||
return {
|
|
||||||
...user,
|
|
||||||
password: createdPasswords[user.email],
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
created: true,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ctx.throw(400, "User onboarding failed")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let createdPasswords: Record<string, string> = {}
|
||||||
|
const users: User[] = ctx.request.body.map(invite => {
|
||||||
|
let password = Math.random().toString(36).substring(2, 22)
|
||||||
|
createdPasswords[invite.email] = password
|
||||||
|
|
||||||
|
return {
|
||||||
|
email: invite.email,
|
||||||
|
password,
|
||||||
|
forceResetPassword: true,
|
||||||
|
roles: invite.userInfo.apps,
|
||||||
|
admin: invite.userInfo.admin,
|
||||||
|
builder: invite.userInfo.builder,
|
||||||
|
tenantId: tenancy.getTenantId(),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
let resp = await userSdk.db.bulkCreate(users, [])
|
||||||
|
resp.successful.forEach(user => {
|
||||||
|
user.password = createdPasswords[user.email]
|
||||||
|
})
|
||||||
|
ctx.body = { ...resp, created: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
export const invite = async (ctx: Ctx<InviteUserRequest>) => {
|
export const invite = async (ctx: Ctx<InviteUserRequest>) => {
|
||||||
|
@ -329,8 +303,7 @@ export const invite = async (ctx: Ctx<InviteUserRequest>) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const inviteMultiple = async (ctx: Ctx<InviteUsersRequest>) => {
|
export const inviteMultiple = async (ctx: Ctx<InviteUsersRequest>) => {
|
||||||
const request = ctx.request.body
|
ctx.body = await userSdk.invite(ctx.request.body)
|
||||||
ctx.body = await userSdk.invite(request)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const checkInvite = async (ctx: any) => {
|
export const checkInvite = async (ctx: any) => {
|
||||||
|
|
Loading…
Reference in New Issue