Various fixes for update self behaviour

This commit is contained in:
Dean 2023-03-07 13:35:18 +00:00
parent ccbe02f976
commit 5b9fbbc3b4
9 changed files with 38 additions and 21 deletions

View File

@ -3,6 +3,7 @@ import { store } from "builderStore"
import { users, auth } from "stores/portal"
import analytics from "analytics"
import { OnboardingData, OnboardingDesign, OnboardingPublish } from "./steps"
import { API } from "api"
const ONBOARDING_EVENT_PREFIX = "onboarding"
export const TOUR_STEP_KEYS = {
@ -83,8 +84,7 @@ const getTours = () => {
// Mark the users onboarding as complete
// Clear all tour related state
if (get(auth).user) {
await users.save({
...get(auth).user,
await API.updateSelf({
onboardedAt: new Date().toISOString(),
})
@ -114,8 +114,7 @@ const getTours = () => {
onComplete: async () => {
// Push the onboarding forward
if (get(auth).user) {
await users.save({
...get(auth).user,
await API.updateSelf({
onboardedAt: new Date().toISOString(),
})

View File

@ -13,6 +13,7 @@
await auth.updateSelf($values)
notifications.success("Information updated successfully")
} catch (error) {
console.error(error)
notifications.error("Failed to update information")
}
}

View File

@ -154,9 +154,14 @@ export function createAuthStore() {
await setInitInfo({})
},
updateSelf: async fields => {
const newUser = { ...get(auth).user, ...fields }
await API.updateSelf(newUser)
setUser(newUser)
await API.updateSelf({ ...fields })
// Refetch to enrich after update.
try {
const user = await API.fetchBuilderSelf()
setUser(user)
} catch (error) {
setUser(null)
}
},
forgotPassword: async email => {
const tenantId = get(store).tenantId

View File

@ -17,6 +17,7 @@ export interface UpdateSelfRequest {
lastName?: string
password?: string
forceResetPassword?: boolean
onboardedAt?: string
}
export interface UpdateSelfResponse {

View File

@ -3,6 +3,7 @@ export interface UpdateSelf {
lastName?: string
password?: string
forceResetPassword?: boolean
onboardedAt?: string
}
export interface SaveUserOpts {

View File

@ -123,11 +123,12 @@ export async function updateSelf(
ctx: UserCtx<UpdateSelfRequest, UpdateSelfResponse>
) {
const body = ctx.request.body
const update: UpdateSelf = {
firstName: body.firstName,
lastName: body.lastName,
password: body.password,
forceResetPassword: body.forceResetPassword,
const update: UpdateSelf = {}
for (let [key, value] of Object.entries(body)) {
if (value) {
update[key as keyof UpdateSelf] = value
}
}
const user = await userSdk.updateSelf(ctx.user._id!, update)

View File

@ -11,7 +11,7 @@ router
.get("/api/global/self", controller.getSelf)
.post(
"/api/global/self",
users.buildUserSaveValidation(true),
users.buildSelfSaveValidation(),
controller.updateSelf
)

View File

@ -17,13 +17,22 @@ let schema: any = {
roles: Joi.object().pattern(/.*/, Joi.string()).required().unknown(true),
}
export const buildUserSaveValidation = (isSelf = false) => {
if (!isSelf) {
schema = {
...schema,
_id: Joi.string(),
_rev: Joi.string(),
}
export const buildSelfSaveValidation = () => {
schema = {
password: Joi.string().allow(null, ""),
forceResetPassword: Joi.boolean().optional(),
firstName: Joi.string().allow(null, ""),
lastName: Joi.string().allow(null, ""),
onboardedAt: Joi.string().allow(null, ""),
}
return auth.joiValidator.body(Joi.object(schema).required().unknown(false))
}
export const buildUserSaveValidation = () => {
schema = {
...schema,
_id: Joi.string(),
_rev: Joi.string(),
}
return auth.joiValidator.body(Joi.object(schema).required().unknown(true))
}

View File

@ -233,7 +233,7 @@ export async function updateSelf(id: string, data: UpdateSelf) {
...user,
...data,
}
return save(user)
return save(user, { requirePassword: false })
}
export const save = async (