mirror of
https://github.com/formbricks/formbricks.git
synced 2025-12-30 02:10:12 -06:00
fix: format survey date fields back to Date type (#1096)
This commit is contained in:
@@ -6,6 +6,7 @@ import { canUserAccessSurvey } from "@formbricks/lib/survey/auth";
|
||||
import { authOptions } from "@formbricks/lib/authOptions";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { AuthorizationError } from "@formbricks/types/v1/errors";
|
||||
import { formatSurveyDateFields } from "@formbricks/lib/survey/util";
|
||||
|
||||
export async function updateSurveyAction(survey: TSurvey): Promise<TSurvey> {
|
||||
const session = await getServerSession(authOptions);
|
||||
@@ -14,14 +15,12 @@ export async function updateSurveyAction(survey: TSurvey): Promise<TSurvey> {
|
||||
const isAuthorized = await canUserAccessSurvey(session.user.id, survey.id);
|
||||
if (!isAuthorized) throw new AuthorizationError("Not authorized");
|
||||
|
||||
if (typeof survey.createdAt === "string") {
|
||||
survey.createdAt = new Date(survey.createdAt);
|
||||
}
|
||||
if (typeof survey.updatedAt === "string") {
|
||||
survey.updatedAt = new Date(survey.updatedAt);
|
||||
}
|
||||
const _survey = {
|
||||
...survey,
|
||||
...formatSurveyDateFields(survey),
|
||||
};
|
||||
|
||||
return await updateSurvey(survey);
|
||||
return await updateSurvey(_survey);
|
||||
}
|
||||
|
||||
export const deleteSurveyAction = async (surveyId: string) => {
|
||||
|
||||
@@ -21,6 +21,7 @@ import { getResponsesCacheTag } from "../response/service";
|
||||
import { ZString } from "@formbricks/types/v1/common";
|
||||
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";
|
||||
import { getActionClasses } from "../actionClass/service";
|
||||
import { formatSurveyDateFields } from "./util";
|
||||
|
||||
// surveys cache key and tags
|
||||
const getSurveysCacheTag = (environmentId: string): string => `environments-${environmentId}-surveys`;
|
||||
@@ -141,7 +142,7 @@ export const getSurveyWithAnalytics = async (surveyId: string): Promise<TSurveyW
|
||||
};
|
||||
|
||||
try {
|
||||
const survey = ZSurveyWithAnalytics.parse(transformedSurvey);
|
||||
const survey: TSurveyWithAnalytics = ZSurveyWithAnalytics.parse(transformedSurvey);
|
||||
return survey;
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
@@ -168,8 +169,7 @@ export const getSurveyWithAnalytics = async (surveyId: string): Promise<TSurveyW
|
||||
// https://github.com/vercel/next.js/issues/51613
|
||||
return {
|
||||
...survey,
|
||||
createdAt: new Date(survey.createdAt),
|
||||
updatedAt: new Date(survey.updatedAt),
|
||||
...formatSurveyDateFields(survey),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -233,8 +233,7 @@ export const getSurvey = async (surveyId: string): Promise<TSurvey | null> => {
|
||||
// https://github.com/vercel/next.js/issues/51613
|
||||
return {
|
||||
...survey,
|
||||
createdAt: new Date(survey.createdAt),
|
||||
updatedAt: new Date(survey.updatedAt),
|
||||
...formatSurveyDateFields(survey),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -366,8 +365,7 @@ export const getSurveys = async (environmentId: string): Promise<TSurvey[]> => {
|
||||
// https://github.com/vercel/next.js/issues/51613
|
||||
return surveys.map((survey) => ({
|
||||
...survey,
|
||||
createdAt: new Date(survey.createdAt),
|
||||
updatedAt: new Date(survey.updatedAt),
|
||||
...formatSurveyDateFields(survey),
|
||||
}));
|
||||
};
|
||||
|
||||
@@ -437,8 +435,7 @@ export const getSurveysWithAnalytics = async (environmentId: string): Promise<TS
|
||||
// https://github.com/vercel/next.js/issues/51613
|
||||
return surveysWithAnalytics.map((survey) => ({
|
||||
...survey,
|
||||
createdAt: new Date(survey.createdAt),
|
||||
updatedAt: new Date(survey.updatedAt),
|
||||
...formatSurveyDateFields(survey),
|
||||
}));
|
||||
};
|
||||
|
||||
|
||||
17
packages/lib/survey/util.ts
Normal file
17
packages/lib/survey/util.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import "server-only";
|
||||
|
||||
import { TSurveyDates } from "@formbricks/types/v1/surveys";
|
||||
|
||||
export const formatSurveyDateFields = (survey: TSurveyDates): TSurveyDates => {
|
||||
if (typeof survey.createdAt === "string") {
|
||||
survey.createdAt = new Date(survey.createdAt);
|
||||
}
|
||||
if (typeof survey.updatedAt === "string") {
|
||||
survey.updatedAt = new Date(survey.updatedAt);
|
||||
}
|
||||
if (typeof survey.closeOnDate === "string") {
|
||||
survey.closeOnDate = new Date(survey.closeOnDate);
|
||||
}
|
||||
|
||||
return survey;
|
||||
};
|
||||
@@ -309,6 +309,11 @@ export const ZSurveyInput = z.object({
|
||||
});
|
||||
|
||||
export type TSurvey = z.infer<typeof ZSurvey>;
|
||||
export type TSurveyDates = {
|
||||
createdAt: TSurvey["createdAt"];
|
||||
updatedAt: TSurvey["updatedAt"];
|
||||
closeOnDate: TSurvey["closeOnDate"];
|
||||
};
|
||||
export type TSurveyInput = z.infer<typeof ZSurveyInput>;
|
||||
|
||||
export const ZSurveyWithAnalytics = ZSurvey.extend({
|
||||
|
||||
Reference in New Issue
Block a user