From 94e272f47b91375d692f4cd366e4a480c2cfe2b9 Mon Sep 17 00:00:00 2001 From: Matti Nannt Date: Tue, 3 Oct 2023 22:03:20 +0200 Subject: [PATCH] fix: validation error in profile update input (#937) --- .../app/(app)/onboarding/components/Objective.tsx | 6 +++++- packages/lib/services/profile.ts | 9 +++++++-- packages/types/v1/profile.ts | 12 +++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/apps/web/app/(app)/onboarding/components/Objective.tsx b/apps/web/app/(app)/onboarding/components/Objective.tsx index c69bd36c85..d0b406604e 100644 --- a/apps/web/app/(app)/onboarding/components/Objective.tsx +++ b/apps/web/app/(app)/onboarding/components/Objective.tsx @@ -41,7 +41,11 @@ const Objective: React.FC = ({ next, skip, formbricksResponseId, if (selectedObjective) { try { setIsProfileUpdating(true); - const updatedProfile = { ...profile, objective: selectedObjective.id }; + const updatedProfile = { + ...profile, + objective: selectedObjective.id, + name: profile.name ?? undefined, + }; await updateProfileAction(updatedProfile); setIsProfileUpdating(false); } catch (e) { diff --git a/packages/lib/services/profile.ts b/packages/lib/services/profile.ts index efa9faa552..74ca017421 100644 --- a/packages/lib/services/profile.ts +++ b/packages/lib/services/profile.ts @@ -4,7 +4,12 @@ import { prisma } from "@formbricks/database"; import { ZId } from "@formbricks/types/v1/environment"; import { DatabaseError, ResourceNotFoundError } from "@formbricks/types/v1/errors"; import { TMembership, TMembershipRole, ZMembershipRole } from "@formbricks/types/v1/memberships"; -import { TProfile, TProfileUpdateInput, ZProfileUpdateInput } from "@formbricks/types/v1/profile"; +import { + TProfile, + TProfileCreateInput, + TProfileUpdateInput, + ZProfileUpdateInput, +} from "@formbricks/types/v1/profile"; import { MembershipRole, Prisma } from "@prisma/client"; import { unstable_cache, revalidateTag } from "next/cache"; import { validateInputs } from "../utils/validate"; @@ -149,7 +154,7 @@ const deleteUser = async (userId: string): Promise => { return profile; }; -export const createProfile = async (data: TProfileUpdateInput): Promise => { +export const createProfile = async (data: TProfileCreateInput): Promise => { validateInputs([data, ZProfileUpdateInput]); const profile = await prisma.user.create({ data: data, diff --git a/packages/types/v1/profile.ts b/packages/types/v1/profile.ts index 6999cd3185..0130acc3bb 100644 --- a/packages/types/v1/profile.ts +++ b/packages/types/v1/profile.ts @@ -21,7 +21,7 @@ export const ZProfile = z.object({ export type TProfile = z.infer; export const ZProfileUpdateInput = z.object({ - name: z.string().optional(), + name: z.string().nullish(), email: z.string().optional(), onboardingCompleted: z.boolean().optional(), role: ZRole.optional(), @@ -29,3 +29,13 @@ export const ZProfileUpdateInput = z.object({ }); export type TProfileUpdateInput = z.infer; + +export const ZProfileCreateInput = z.object({ + name: z.string().optional(), + email: z.string(), + onboardingCompleted: z.boolean().optional(), + role: ZRole.optional(), + objective: ZObjective.optional(), +}); + +export type TProfileCreateInput = z.infer;