fix: onboarding ending in endless loop because of validation error (#1745)

This commit is contained in:
Matti Nannt
2023-12-05 14:56:32 +01:00
committed by GitHub
parent d2c703ef60
commit 85f5425d89
4 changed files with 43 additions and 13 deletions

View File

@@ -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<TProfile | null> =>
unstable_cache(
export const getProfile = async (id: string): Promise<TProfile | null> => {
const profile = await unstable_cache(
async () => {
validateInputs([id, ZId]);
@@ -67,8 +68,18 @@ export const getProfile = async (id: string): Promise<TProfile | null> =>
}
)();
export const getProfileByEmail = async (email: string): Promise<TProfile | null> =>
unstable_cache(
if (!profile) {
return null;
}
return {
...profile,
...formatProfileDateFields(profile),
} as TProfile;
};
export const getProfileByEmail = async (email: string): Promise<TProfile | null> => {
const profile = await unstable_cache(
async () => {
validateInputs([email, z.string().email()]);
@@ -100,6 +111,16 @@ export const getProfileByEmail = async (email: string): Promise<TProfile | null>
}
)();
if (!profile) {
return null;
}
return {
...profile,
...formatProfileDateFields(profile),
} as TProfile;
};
const getAdminMemberships = (memberships: TMembership[]): TMembership[] =>
memberships.filter((membership) => membership.role === "admin");

View File

@@ -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;
};

View File

@@ -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);
}

View File

@@ -66,12 +66,6 @@ export const ZResponse = z.object({
export type TResponse = z.infer<typeof ZResponse>;
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(),