diff --git a/packages/builder/src/components/portal/onboarding/tours.js b/packages/builder/src/components/portal/onboarding/tours.js index e28c638a4a..296c631bfd 100644 --- a/packages/builder/src/components/portal/onboarding/tours.js +++ b/packages/builder/src/components/portal/onboarding/tours.js @@ -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(), }) diff --git a/packages/builder/src/components/settings/ProfileModal.svelte b/packages/builder/src/components/settings/ProfileModal.svelte index daeeb791bb..caaf5154b3 100644 --- a/packages/builder/src/components/settings/ProfileModal.svelte +++ b/packages/builder/src/components/settings/ProfileModal.svelte @@ -13,6 +13,7 @@ await auth.updateSelf($values) notifications.success("Information updated successfully") } catch (error) { + console.error(error) notifications.error("Failed to update information") } } diff --git a/packages/builder/src/stores/portal/auth.js b/packages/builder/src/stores/portal/auth.js index d11033c142..2ab68b11b4 100644 --- a/packages/builder/src/stores/portal/auth.js +++ b/packages/builder/src/stores/portal/auth.js @@ -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 diff --git a/packages/types/src/api/web/auth.ts b/packages/types/src/api/web/auth.ts index e31a151c48..46b1e8cec9 100644 --- a/packages/types/src/api/web/auth.ts +++ b/packages/types/src/api/web/auth.ts @@ -17,6 +17,7 @@ export interface UpdateSelfRequest { lastName?: string password?: string forceResetPassword?: boolean + onboardedAt?: string } export interface UpdateSelfResponse { diff --git a/packages/types/src/sdk/user.ts b/packages/types/src/sdk/user.ts index 1602eeb6c8..a126d49e34 100644 --- a/packages/types/src/sdk/user.ts +++ b/packages/types/src/sdk/user.ts @@ -3,6 +3,7 @@ export interface UpdateSelf { lastName?: string password?: string forceResetPassword?: boolean + onboardedAt?: string } export interface SaveUserOpts { diff --git a/packages/worker/src/api/controllers/global/self.ts b/packages/worker/src/api/controllers/global/self.ts index 889a3e6a27..75a1b89a75 100644 --- a/packages/worker/src/api/controllers/global/self.ts +++ b/packages/worker/src/api/controllers/global/self.ts @@ -123,11 +123,12 @@ export async function updateSelf( ctx: UserCtx ) { 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) diff --git a/packages/worker/src/api/routes/global/self.ts b/packages/worker/src/api/routes/global/self.ts index 8784bb8b20..c1f1b80543 100644 --- a/packages/worker/src/api/routes/global/self.ts +++ b/packages/worker/src/api/routes/global/self.ts @@ -11,7 +11,7 @@ router .get("/api/global/self", controller.getSelf) .post( "/api/global/self", - users.buildUserSaveValidation(true), + users.buildSelfSaveValidation(), controller.updateSelf ) diff --git a/packages/worker/src/api/routes/validation/users.ts b/packages/worker/src/api/routes/validation/users.ts index 35f293ce87..bce1cf582b 100644 --- a/packages/worker/src/api/routes/validation/users.ts +++ b/packages/worker/src/api/routes/validation/users.ts @@ -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)) } diff --git a/packages/worker/src/sdk/users/users.ts b/packages/worker/src/sdk/users/users.ts index 126f91d3e1..c8295ff400 100644 --- a/packages/worker/src/sdk/users/users.ts +++ b/packages/worker/src/sdk/users/users.ts @@ -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 (