mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-26 03:35:38 -05:00
Merge branch 'main' of github.com:formbricks/formbricks into lp/concierge
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@formbricks/database";
|
||||
import { responses } from "@/lib/api/response";
|
||||
import { createPersonWithUser } from "@/lib/api/clientPerson";
|
||||
|
||||
export async function GET(req: Request): Promise<NextResponse> {
|
||||
const { searchParams } = new URL(req.url);
|
||||
const userId = searchParams.get("userId");
|
||||
if (!userId) {
|
||||
return responses.badRequestResponse("Fields are missing or incorrectly formatted", { userId: "" }, true);
|
||||
}
|
||||
const environmentId = searchParams.get("environmentId");
|
||||
if (!environmentId) {
|
||||
return responses.badRequestResponse(
|
||||
"Fields are missing or incorrectly formatted",
|
||||
{ environmentId: "" },
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
const person = await prisma.person.findFirst({
|
||||
where: {
|
||||
attributes: {
|
||||
some: {
|
||||
attributeClass: {
|
||||
name: "userId",
|
||||
},
|
||||
value: userId,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
environmentId: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!person) {
|
||||
const newPerson = await createPersonWithUser(environmentId, userId);
|
||||
return responses.successResponse({ person: newPerson }, true);
|
||||
}
|
||||
return responses.successResponse({ person }, true);
|
||||
}
|
||||
@@ -1,6 +1,23 @@
|
||||
import { prisma } from "@formbricks/database";
|
||||
import type { Person } from "@formbricks/types/js";
|
||||
|
||||
const select = {
|
||||
id: true,
|
||||
environmentId: true,
|
||||
attributes: {
|
||||
select: {
|
||||
id: true,
|
||||
value: true,
|
||||
attributeClass: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export const createPerson = async (environmentId: string): Promise<Person> => {
|
||||
return await prisma.person.create({
|
||||
data: {
|
||||
@@ -10,21 +27,45 @@ export const createPerson = async (environmentId: string): Promise<Person> => {
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
environmentId: true,
|
||||
attributes: {
|
||||
select: {
|
||||
id: true,
|
||||
value: true,
|
||||
attributeClass: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
select,
|
||||
});
|
||||
};
|
||||
|
||||
export const createPersonWithUser = async (environmentId: string, userId: string): Promise<Person> => {
|
||||
const userIdAttributeClass = await prisma.attributeClass.findFirst({
|
||||
where: {
|
||||
environmentId,
|
||||
name: "userId",
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!userIdAttributeClass) {
|
||||
throw new Error("Attribute class not found for the given environmentId");
|
||||
}
|
||||
|
||||
return await prisma.person.create({
|
||||
data: {
|
||||
environment: {
|
||||
connect: {
|
||||
id: environmentId,
|
||||
},
|
||||
},
|
||||
attributes: {
|
||||
create: [
|
||||
{
|
||||
attributeClass: {
|
||||
connect: {
|
||||
id: userIdAttributeClass.id,
|
||||
},
|
||||
},
|
||||
value: userId,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
select,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -7,6 +7,7 @@ import { TResponseInput } from "@formbricks/types/v1/responses";
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
import type { Survey } from "@formbricks/types/surveys";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useGetOrCreatePerson } from "../people/people";
|
||||
|
||||
export const useLinkSurvey = (surveyId: string) => {
|
||||
const { data, error, mutate, isLoading } = useSWR(`/api/v1/client/surveys/${surveyId}`, fetcher);
|
||||
@@ -36,22 +37,28 @@ export const useLinkSurveyUtils = (survey: Survey) => {
|
||||
|
||||
const lastQuestion = currentQuestion?.id === survey.questions[survey.questions.length - 1].id;
|
||||
|
||||
const userId = URLParams.get("userId");
|
||||
const { person, isLoadingPerson } = useGetOrCreatePerson(survey.environmentId, isPreview ? null : userId);
|
||||
const personId = person?.data.person.id ?? null;
|
||||
|
||||
useEffect(() => {
|
||||
if (survey) {
|
||||
setCurrentQuestion(survey.questions[0]);
|
||||
if (!isLoadingPerson) {
|
||||
if (survey) {
|
||||
setCurrentQuestion(survey.questions[0]);
|
||||
|
||||
if (isPreview) return;
|
||||
if (isPreview) return;
|
||||
|
||||
// create display
|
||||
createDisplay(
|
||||
{ surveyId: survey.id },
|
||||
`${window.location.protocol}//${window.location.host}`,
|
||||
survey.environmentId
|
||||
).then((display) => {
|
||||
setDisplayId(display.id);
|
||||
});
|
||||
// create display
|
||||
createDisplay(
|
||||
{ surveyId: survey.id },
|
||||
`${window.location.protocol}//${window.location.host}`,
|
||||
survey.environmentId
|
||||
).then((display) => {
|
||||
setDisplayId(display.id);
|
||||
});
|
||||
}
|
||||
}
|
||||
}, [survey, isPreview]);
|
||||
}, [survey, isPreview, isLoadingPerson]);
|
||||
|
||||
useEffect(() => {
|
||||
if (currentQuestion && survey) {
|
||||
@@ -100,7 +107,7 @@ export const useLinkSurveyUtils = (survey: Survey) => {
|
||||
// build response
|
||||
const responseRequest: TResponseInput = {
|
||||
surveyId: survey.id,
|
||||
personId: null,
|
||||
personId: personId,
|
||||
finished,
|
||||
data,
|
||||
};
|
||||
|
||||
@@ -41,3 +41,15 @@ export const deletePerson = async (environmentId: string, personId: string) => {
|
||||
throw Error(`deletePerson: unable to delete person: ${error.message}`);
|
||||
}
|
||||
};
|
||||
|
||||
export const useGetOrCreatePerson = (environmentId: string, personId?: string | null) => {
|
||||
const { data, isLoading } = useSWR(
|
||||
personId ? `/api/v1/client/people/getOrCreate?userId=${personId}&environmentId=${environmentId}` : null,
|
||||
fetcher
|
||||
);
|
||||
|
||||
return {
|
||||
person: data,
|
||||
isLoadingPerson: isLoading,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user