From 85f5425d892d8ca74abc50db83f78b326e08aa71 Mon Sep 17 00:00:00 2001 From: Matti Nannt Date: Tue, 5 Dec 2023 14:56:32 +0100 Subject: [PATCH] fix: onboarding ending in endless loop because of validation error (#1745) --- packages/lib/profile/service.ts | 31 ++++++++++++++++++++++++++----- packages/lib/profile/util.ts | 15 +++++++++++++++ packages/lib/response/util.ts | 4 ++-- packages/types/responses.ts | 6 ------ 4 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 packages/lib/profile/util.ts diff --git a/packages/lib/profile/service.ts b/packages/lib/profile/service.ts index cc7824d095..3d7b245f25 100644 --- a/packages/lib/profile/service.ts +++ b/packages/lib/profile/service.ts @@ -14,10 +14,11 @@ import { Prisma } from "@prisma/client"; import { unstable_cache } from "next/cache"; import { z } from "zod"; import { SERVICES_REVALIDATION_INTERVAL } from "../constants"; +import { updateMembership } from "../membership/service"; import { deleteTeam } from "../team/service"; import { validateInputs } from "../utils/validate"; import { profileCache } from "./cache"; -import { updateMembership } from "../membership/service"; +import { formatProfileDateFields } from "./util"; const responseSelection = { id: true, @@ -34,8 +35,8 @@ const responseSelection = { }; // function to retrive basic information about a user's profile -export const getProfile = async (id: string): Promise => - unstable_cache( +export const getProfile = async (id: string): Promise => { + const profile = await unstable_cache( async () => { validateInputs([id, ZId]); @@ -67,8 +68,18 @@ export const getProfile = async (id: string): Promise => } )(); -export const getProfileByEmail = async (email: string): Promise => - unstable_cache( + if (!profile) { + return null; + } + + return { + ...profile, + ...formatProfileDateFields(profile), + } as TProfile; +}; + +export const getProfileByEmail = async (email: string): Promise => { + const profile = await unstable_cache( async () => { validateInputs([email, z.string().email()]); @@ -100,6 +111,16 @@ export const getProfileByEmail = async (email: string): Promise } )(); + if (!profile) { + return null; + } + + return { + ...profile, + ...formatProfileDateFields(profile), + } as TProfile; +}; + const getAdminMemberships = (memberships: TMembership[]): TMembership[] => memberships.filter((membership) => membership.role === "admin"); diff --git a/packages/lib/profile/util.ts b/packages/lib/profile/util.ts new file mode 100644 index 0000000000..dd03720642 --- /dev/null +++ b/packages/lib/profile/util.ts @@ -0,0 +1,15 @@ +import { TProfile } from "@formbricks/types/profile"; + +export const formatProfileDateFields = (profile: TProfile): TProfile => { + if (typeof profile.createdAt === "string") { + profile.createdAt = new Date(profile.createdAt); + } + if (typeof profile.updatedAt === "string") { + profile.updatedAt = new Date(profile.updatedAt); + } + if (typeof profile.emailVerified === "string") { + profile.emailVerified = new Date(profile.emailVerified); + } + + return profile; +}; diff --git a/packages/lib/response/util.ts b/packages/lib/response/util.ts index 59f3c08050..7a73ba8cd9 100644 --- a/packages/lib/response/util.ts +++ b/packages/lib/response/util.ts @@ -1,8 +1,8 @@ import "server-only"; -import { TResponseDates, TResponseTtc } from "@formbricks/types/responses"; +import { TResponse, TResponseTtc } from "@formbricks/types/responses"; -export const formatResponseDateFields = (response: TResponseDates): TResponseDates => { +export const formatResponseDateFields = (response: TResponse): TResponse => { if (typeof response.createdAt === "string") { response.createdAt = new Date(response.createdAt); } diff --git a/packages/types/responses.ts b/packages/types/responses.ts index d5bf66b74e..1cb330a18a 100644 --- a/packages/types/responses.ts +++ b/packages/types/responses.ts @@ -66,12 +66,6 @@ export const ZResponse = z.object({ export type TResponse = z.infer; -export type TResponseDates = { - createdAt: TResponse["createdAt"]; - updatedAt: TResponse["updatedAt"]; - notes: TResponse["notes"]; -}; - export const ZResponseInput = z.object({ environmentId: z.string().cuid2(), surveyId: z.string().cuid2(),