mirror of
https://github.com/formbricks/formbricks.git
synced 2026-02-12 18:59:38 -06:00
chore: added expirationDays param support in personal link api (#6578)
Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com>
This commit is contained in:
committed by
GitHub
parent
fa1663d858
commit
1ced76c44d
@@ -1,7 +1,10 @@
|
||||
import { ZContactLinkParams } from "@/modules/api/v2/management/surveys/[surveyId]/contact-links/contacts/[contactId]/types/survey";
|
||||
import { makePartialSchema } from "@/modules/api/v2/types/openapi-response";
|
||||
import { z } from "zod";
|
||||
import { ZodOpenApiOperationObject } from "zod-openapi";
|
||||
import {
|
||||
ZContactLinkParams,
|
||||
ZContactLinkQuery,
|
||||
} from "@/modules/api/v2/management/surveys/[surveyId]/contact-links/contacts/[contactId]/types/survey";
|
||||
import { makePartialSchema } from "@/modules/api/v2/types/openapi-response";
|
||||
|
||||
export const getPersonalizedSurveyLink: ZodOpenApiOperationObject = {
|
||||
operationId: "getPersonalizedSurveyLink",
|
||||
@@ -9,6 +12,7 @@ export const getPersonalizedSurveyLink: ZodOpenApiOperationObject = {
|
||||
description: "Retrieves a personalized link for a specific survey.",
|
||||
requestParams: {
|
||||
path: ZContactLinkParams,
|
||||
query: ZContactLinkQuery,
|
||||
},
|
||||
tags: ["Management API - Surveys - Contact Links"],
|
||||
responses: {
|
||||
@@ -20,6 +24,10 @@ export const getPersonalizedSurveyLink: ZodOpenApiOperationObject = {
|
||||
z.object({
|
||||
data: z.object({
|
||||
surveyUrl: z.string().url(),
|
||||
expiresAt: z
|
||||
.string()
|
||||
.nullable()
|
||||
.describe("The date and time the link expires, null if no expiration"),
|
||||
}),
|
||||
})
|
||||
),
|
||||
|
||||
@@ -8,7 +8,9 @@ import { getSurvey } from "@/modules/api/v2/management/surveys/[surveyId]/contac
|
||||
import {
|
||||
TContactLinkParams,
|
||||
ZContactLinkParams,
|
||||
ZContactLinkQuery,
|
||||
} from "@/modules/api/v2/management/surveys/[surveyId]/contact-links/contacts/[contactId]/types/survey";
|
||||
import { calculateExpirationDate } from "@/modules/api/v2/management/surveys/[surveyId]/contact-links/lib/utils";
|
||||
import { ApiErrorResponseV2 } from "@/modules/api/v2/types/api-error";
|
||||
import { getContactSurveyLink } from "@/modules/ee/contacts/lib/contact-survey-link";
|
||||
import { hasPermission } from "@/modules/organization/settings/api-keys/lib/utils";
|
||||
@@ -19,9 +21,10 @@ export const GET = async (request: Request, props: { params: Promise<TContactLin
|
||||
externalParams: props.params,
|
||||
schemas: {
|
||||
params: ZContactLinkParams,
|
||||
query: ZContactLinkQuery,
|
||||
},
|
||||
handler: async ({ authentication, parsedInput }) => {
|
||||
const { params } = parsedInput;
|
||||
const { params, query } = parsedInput;
|
||||
|
||||
if (!params) {
|
||||
return handleApiError(request, {
|
||||
@@ -92,12 +95,27 @@ export const GET = async (request: Request, props: { params: Promise<TContactLin
|
||||
});
|
||||
}
|
||||
|
||||
const surveyUrlResult = await getContactSurveyLink(params.contactId, params.surveyId, 7);
|
||||
// Calculate expiration date based on expirationDays
|
||||
let expiresAt: string | null = null;
|
||||
if (query?.expirationDays) {
|
||||
expiresAt = calculateExpirationDate(query.expirationDays);
|
||||
}
|
||||
|
||||
const surveyUrlResult = await getContactSurveyLink(
|
||||
params.contactId,
|
||||
params.surveyId,
|
||||
query?.expirationDays || undefined
|
||||
);
|
||||
|
||||
if (!surveyUrlResult.ok) {
|
||||
return handleApiError(request, surveyUrlResult.error);
|
||||
}
|
||||
|
||||
return responses.successResponse({ data: { surveyUrl: surveyUrlResult.data } });
|
||||
return responses.successResponse({
|
||||
data: {
|
||||
surveyUrl: surveyUrlResult.data,
|
||||
expiresAt,
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
@@ -20,4 +20,15 @@ export const ZContactLinkParams = z.object({
|
||||
}),
|
||||
});
|
||||
|
||||
export const ZContactLinkQuery = z.object({
|
||||
expirationDays: z.coerce
|
||||
.number()
|
||||
.int()
|
||||
.min(1)
|
||||
.max(365)
|
||||
.optional()
|
||||
.describe("Number of days until the generated JWT expires. If not provided, there is no expiration."),
|
||||
});
|
||||
|
||||
export type TContactLinkParams = z.infer<typeof ZContactLinkParams>;
|
||||
export type TContactLinkQuery = z.infer<typeof ZContactLinkQuery>;
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
||||
import { calculateExpirationDate } from "./utils";
|
||||
|
||||
describe("calculateExpirationDate", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
test("calculates expiration date for positive days", () => {
|
||||
const baseDate = new Date("2024-01-15T12:00:00.000Z");
|
||||
vi.setSystemTime(baseDate);
|
||||
|
||||
const result = calculateExpirationDate(7);
|
||||
const expectedDate = new Date("2024-01-22T12:00:00.000Z");
|
||||
|
||||
expect(result).toBe(expectedDate.toISOString());
|
||||
});
|
||||
|
||||
test("handles zero expiration days", () => {
|
||||
const baseDate = new Date("2024-01-15T12:00:00.000Z");
|
||||
vi.setSystemTime(baseDate);
|
||||
|
||||
const result = calculateExpirationDate(0);
|
||||
|
||||
expect(result).toBe(baseDate.toISOString());
|
||||
});
|
||||
|
||||
test("handles negative expiration days", () => {
|
||||
const baseDate = new Date("2024-01-15T12:00:00.000Z");
|
||||
vi.setSystemTime(baseDate);
|
||||
|
||||
const result = calculateExpirationDate(-5);
|
||||
const expectedDate = new Date("2024-01-10T12:00:00.000Z");
|
||||
|
||||
expect(result).toBe(expectedDate.toISOString());
|
||||
});
|
||||
|
||||
test("returns valid ISO string format", () => {
|
||||
const baseDate = new Date("2024-01-15T12:00:00.000Z");
|
||||
vi.setSystemTime(baseDate);
|
||||
|
||||
const result = calculateExpirationDate(10);
|
||||
const isoRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
|
||||
|
||||
expect(result).toMatch(isoRegex);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,5 @@
|
||||
export const calculateExpirationDate = (expirationDays: number) => {
|
||||
const expirationDate = new Date();
|
||||
expirationDate.setDate(expirationDate.getDate() + expirationDays);
|
||||
return expirationDate.toISOString();
|
||||
};
|
||||
@@ -1,7 +1,9 @@
|
||||
import { logger } from "@formbricks/logger";
|
||||
import { authenticatedApiClient } from "@/modules/api/v2/auth/authenticated-api-client";
|
||||
import { responses } from "@/modules/api/v2/lib/response";
|
||||
import { handleApiError } from "@/modules/api/v2/lib/utils";
|
||||
import { getEnvironmentId } from "@/modules/api/v2/management/lib/helper";
|
||||
import { calculateExpirationDate } from "@/modules/api/v2/management/surveys/[surveyId]/contact-links/lib/utils";
|
||||
import { getContactsInSegment } from "@/modules/api/v2/management/surveys/[surveyId]/contact-links/segments/[segmentId]/lib/contact";
|
||||
import {
|
||||
ZContactLinksBySegmentParams,
|
||||
@@ -11,7 +13,6 @@ import { ApiErrorResponseV2 } from "@/modules/api/v2/types/api-error";
|
||||
import { getContactSurveyLink } from "@/modules/ee/contacts/lib/contact-survey-link";
|
||||
import { getIsContactsEnabled } from "@/modules/ee/license-check/lib/utils";
|
||||
import { hasPermission } from "@/modules/organization/settings/api-keys/lib/utils";
|
||||
import { logger } from "@formbricks/logger";
|
||||
|
||||
export const GET = async (
|
||||
request: Request,
|
||||
@@ -76,9 +77,7 @@ export const GET = async (
|
||||
// Calculate expiration date based on expirationDays
|
||||
let expiresAt: string | null = null;
|
||||
if (query?.expirationDays) {
|
||||
const expirationDate = new Date();
|
||||
expirationDate.setDate(expirationDate.getDate() + query.expirationDays);
|
||||
expiresAt = expirationDate.toISOString();
|
||||
expiresAt = calculateExpirationDate(query.expirationDays);
|
||||
}
|
||||
|
||||
// Generate survey links for each contact
|
||||
|
||||
@@ -2017,6 +2017,17 @@ paths:
|
||||
type: string
|
||||
description: The ID of the contact
|
||||
required: true
|
||||
- in: query
|
||||
name: expirationDays
|
||||
description: Number of days until the generated JWT expires. If not provided,
|
||||
there is no expiration.
|
||||
schema:
|
||||
type:
|
||||
- number
|
||||
- undefined
|
||||
minimum: 1
|
||||
description: Number of days until the generated JWT expires. If not provided,
|
||||
there is no expiration.
|
||||
responses:
|
||||
"200":
|
||||
description: Personalized survey link retrieved successfully.
|
||||
@@ -2031,6 +2042,11 @@ paths:
|
||||
surveyUrl:
|
||||
type: string
|
||||
format: uri
|
||||
expiresAt:
|
||||
type: string
|
||||
format: date-time
|
||||
nullable: true
|
||||
description: The date and time the link expires, null if no expiration
|
||||
required:
|
||||
- surveyUrl
|
||||
/surveys/{surveyId}/contact-links/segments/{segmentId}:
|
||||
|
||||
Reference in New Issue
Block a user