feat: personalized survey links (#4870)

Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com>
Co-authored-by: Piyush Gupta <piyushguptaa2z123@gmail.com>
This commit is contained in:
Matti Nannt
2025-03-19 08:30:39 +01:00
committed by GitHub
parent 3b126291a6
commit 214d18616f
28 changed files with 1275 additions and 146 deletions
@@ -0,0 +1,61 @@
import { beforeEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { getContact } from "./contacts";
vi.mock("@formbricks/database", () => ({
prisma: {
contact: {
findUnique: vi.fn(),
},
},
}));
describe("getContact", () => {
const mockContactId = "cm8fj8ry6000008l5daam88nc";
const mockEnvironmentId = "cm8fj8xt3000108l5art7594h";
const mockContact = {
id: mockContactId,
};
beforeEach(() => {
vi.clearAllMocks();
});
test("returns contact when found", async () => {
vi.mocked(prisma.contact.findUnique).mockResolvedValue(mockContact);
const result = await getContact(mockContactId, mockEnvironmentId);
expect(prisma.contact.findUnique).toHaveBeenCalledWith({
where: {
id: mockContactId,
environmentId: mockEnvironmentId,
},
select: {
id: true,
},
});
if (result.ok) {
expect(result.data).toEqual(mockContact);
}
});
test("returns null when contact not found", async () => {
vi.mocked(prisma.contact.findUnique).mockResolvedValue(null);
const result = await getContact(mockContactId, mockEnvironmentId);
expect(prisma.contact.findUnique).toHaveBeenCalled();
if (!result.ok) {
expect(result.error).toEqual({
details: [
{
field: "contact",
issue: "not found",
},
],
type: "not_found",
});
}
});
});
@@ -0,0 +1,37 @@
import { contactCache } from "@/lib/cache/contact";
import { ApiErrorResponseV2 } from "@/modules/api/v2/types/api-error";
import { Contact } from "@prisma/client";
import { cache as reactCache } from "react";
import { prisma } from "@formbricks/database";
import { cache } from "@formbricks/lib/cache";
import { Result, err, ok } from "@formbricks/types/error-handlers";
export const getContact = reactCache(async (contactId: string, environmentId: string) =>
cache(
async (): Promise<Result<Pick<Contact, "id">, ApiErrorResponseV2>> => {
try {
const contact = await prisma.contact.findUnique({
where: {
id: contactId,
environmentId,
},
select: {
id: true,
},
});
if (!contact) {
return err({ type: "not_found", details: [{ field: "contact", issue: "not found" }] });
}
return ok(contact);
} catch (error) {
return err({ type: "internal_server_error", details: [{ field: "contact", issue: error.message }] });
}
},
[`contact-link-getContact-${contactId}-${environmentId}`],
{
tags: [contactCache.tag.byId(contactId), contactCache.tag.byEnvironmentId(environmentId)],
}
)()
);
@@ -0,0 +1,61 @@
import { beforeEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { getResponse } from "./response";
vi.mock("@formbricks/database", () => ({
prisma: {
response: {
findFirst: vi.fn(),
},
},
}));
describe("getResponse", () => {
const mockContactId = "cm8fj8xt3000108l5art7594h";
const mockSurveyId = "cm8fj9962000208l56jcu94i5";
const mockResponse = {
id: "cm8fj9gqp000308l5ab7y800j",
};
beforeEach(() => {
vi.clearAllMocks();
});
test("returns response when found", async () => {
vi.mocked(prisma.response.findFirst).mockResolvedValue(mockResponse);
const result = await getResponse(mockContactId, mockSurveyId);
expect(prisma.response.findFirst).toHaveBeenCalledWith({
where: {
contactId: mockContactId,
surveyId: mockSurveyId,
},
select: {
id: true,
},
});
if (result.ok) {
expect(result.data).toEqual(mockResponse);
}
});
test("returns null when response not found", async () => {
vi.mocked(prisma.response.findFirst).mockResolvedValue(null);
const result = await getResponse(mockContactId, mockSurveyId);
expect(prisma.response.findFirst).toHaveBeenCalled();
if (!result.ok) {
expect(result.error).toEqual({
details: [
{
field: "response",
issue: "not found",
},
],
type: "not_found",
});
}
});
});
@@ -0,0 +1,37 @@
import { ApiErrorResponseV2 } from "@/modules/api/v2/types/api-error";
import { Response } from "@prisma/client";
import { cache as reactCache } from "react";
import { prisma } from "@formbricks/database";
import { cache } from "@formbricks/lib/cache";
import { responseCache } from "@formbricks/lib/response/cache";
import { Result, err, ok } from "@formbricks/types/error-handlers";
export const getResponse = reactCache(async (contactId: string, surveyId: string) =>
cache(
async (): Promise<Result<Pick<Response, "id">, ApiErrorResponseV2>> => {
try {
const response = await prisma.response.findFirst({
where: {
contactId,
surveyId,
},
select: {
id: true,
},
});
if (!response) {
return err({ type: "not_found", details: [{ field: "response", issue: "not found" }] });
}
return ok(response);
} catch (error) {
return err({ type: "internal_server_error", details: [{ field: "response", issue: error.message }] });
}
},
[`contact-link-getResponse-${contactId}-${surveyId}`],
{
tags: [responseCache.tag.byId(contactId), responseCache.tag.bySurveyId(surveyId)],
}
)()
);
@@ -0,0 +1,61 @@
import { beforeEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { getSurvey } from "./surveys";
vi.mock("@formbricks/database", () => ({
prisma: {
survey: {
findUnique: vi.fn(),
},
},
}));
describe("getSurvey", () => {
const mockSurveyId = "cm8fj9psb000408l50e1x4c6f";
const mockSurvey = {
id: mockSurveyId,
type: "web",
};
beforeEach(() => {
vi.clearAllMocks();
});
test("returns survey when found", async () => {
vi.mocked(prisma.survey.findUnique).mockResolvedValue(mockSurvey);
const result = await getSurvey(mockSurveyId);
expect(prisma.survey.findUnique).toHaveBeenCalledWith({
where: {
id: mockSurveyId,
},
select: {
id: true,
type: true,
},
});
if (result.ok) {
expect(result.data).toEqual(mockSurvey);
}
});
test("returns null when survey not found", async () => {
vi.mocked(prisma.survey.findUnique).mockResolvedValue(null);
const result = await getSurvey(mockSurveyId);
expect(prisma.survey.findUnique).toHaveBeenCalled();
if (!result.ok) {
expect(result.error).toEqual({
details: [
{
field: "survey",
issue: "not found",
},
],
type: "not_found",
});
}
});
});
@@ -0,0 +1,35 @@
import { ApiErrorResponseV2 } from "@/modules/api/v2/types/api-error";
import { Survey } from "@prisma/client";
import { cache as reactCache } from "react";
import { prisma } from "@formbricks/database";
import { cache } from "@formbricks/lib/cache";
import { surveyCache } from "@formbricks/lib/survey/cache";
import { Result, err, ok } from "@formbricks/types/error-handlers";
export const getSurvey = reactCache(async (surveyId: string) =>
cache(
async (): Promise<Result<Pick<Survey, "id" | "type">, ApiErrorResponseV2>> => {
try {
const survey = await prisma.survey.findUnique({
where: { id: surveyId },
select: {
id: true,
type: true,
},
});
if (!survey) {
return err({ type: "not_found", details: [{ field: "survey", issue: "not found" }] });
}
return ok(survey);
} catch (error) {
return err({ type: "internal_server_error", details: [{ field: "survey", issue: error.message }] });
}
},
[`contact-link-getSurvey-${surveyId}`],
{
tags: [surveyCache.tag.byId(surveyId)],
}
)()
);
@@ -0,0 +1,111 @@
import { responses } from "@/modules/api/v2/lib/response";
import { handleApiError } from "@/modules/api/v2/lib/utils";
import { authenticatedApiClient } from "@/modules/api/v2/management/auth/authenticated-api-client";
import { checkAuthorization } from "@/modules/api/v2/management/auth/check-authorization";
import { getEnvironmentId } from "@/modules/api/v2/management/lib/helper";
import { getContact } from "@/modules/api/v2/management/surveys/[surveyId]/contact-links/contacts/[contactId]/lib/contacts";
import { getResponse } from "@/modules/api/v2/management/surveys/[surveyId]/contact-links/contacts/[contactId]/lib/response";
import { getSurvey } from "@/modules/api/v2/management/surveys/[surveyId]/contact-links/contacts/[contactId]/lib/surveys";
import { getContactSurveyLink } from "@/modules/ee/contacts/lib/contact-survey-link";
import { z } from "zod";
import { ZId } from "@formbricks/types/common";
const ZContactLinkParams = z.object({
surveyId: ZId,
contactId: ZId,
});
export const GET = async (
request: Request,
props: { params: Promise<{ surveyId: string; contactId: string }> }
) =>
authenticatedApiClient({
request,
externalParams: props.params,
schemas: {
params: ZContactLinkParams,
},
handler: async ({ authentication, parsedInput }) => {
const { params } = parsedInput;
if (!params) {
return handleApiError(request, {
type: "bad_request",
details: [{ field: "params", issue: "missing" }],
});
}
const environmentIdResult = await getEnvironmentId(params.surveyId, false);
if (!environmentIdResult.ok) {
return handleApiError(request, environmentIdResult.error);
}
const environmentId = environmentIdResult.data;
const checkAuthorizationResult = await checkAuthorization({
authentication,
environmentId,
});
if (!checkAuthorizationResult.ok) {
return handleApiError(request, checkAuthorizationResult.error);
}
const surveyResult = await getSurvey(params.surveyId);
if (!surveyResult.ok) {
return handleApiError(request, surveyResult.error);
}
const survey = surveyResult.data;
if (!survey) {
return handleApiError(request, {
type: "not_found",
details: [{ field: "surveyId", issue: "Not found" }],
});
}
if (survey.type !== "link") {
return handleApiError(request, {
type: "bad_request",
details: [{ field: "surveyId", issue: "Not a link survey" }],
});
}
// Check if contact exists and belongs to the environment
const contactResult = await getContact(params.contactId, environmentId);
if (!contactResult.ok) {
return handleApiError(request, contactResult.error);
}
const contact = contactResult.data;
if (!contact) {
return handleApiError(request, {
type: "not_found",
details: [{ field: "contactId", issue: "Not found" }],
});
}
// Check if contact has already responded to this survey
const existingResponseResult = await getResponse(params.contactId, params.surveyId);
if (existingResponseResult.ok) {
return handleApiError(request, {
type: "bad_request",
details: [{ field: "contactId", issue: "Already responded" }],
});
}
const surveyUrlResult = getContactSurveyLink(params.contactId, params.surveyId, 7);
if (!surveyUrlResult.ok) {
return handleApiError(request, surveyUrlResult.error);
}
return responses.successResponse({ data: { surveyUrl: surveyUrlResult.data } });
},
});