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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -17,13 +17,22 @@ let schema: any = {
roles: Joi.object().pattern(/.*/, Joi.string()).required().unknown(true), roles: Joi.object().pattern(/.*/, Joi.string()).required().unknown(true),
} }
export const buildUserSaveValidation = (isSelf = false) => { export const buildSelfSaveValidation = () => {
if (!isSelf) { schema = {
schema = { password: Joi.string().allow(null, ""),
...schema, forceResetPassword: Joi.boolean().optional(),
_id: Joi.string(), firstName: Joi.string().allow(null, ""),
_rev: Joi.string(), 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)) 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, ...user,
...data, ...data,
} }
return save(user) return save(user, { requirePassword: false })
} }
export const save = async ( export const save = async (