mirror of
https://github.com/formbricks/formbricks.git
synced 2026-03-11 11:09:01 -05:00
chore: moves prisma logic from set-attribute to person service (#840)
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
import { getUpdatedState } from "@/app/api/v1/js/sync/lib/sync";
|
||||
import { responses } from "@/lib/api/response";
|
||||
import { transformErrorToDetails } from "@/lib/api/validator";
|
||||
import { prisma } from "@formbricks/database";
|
||||
import { createAttributeClass, getAttributeClassByNameCached } from "@formbricks/lib/services/attributeClass";
|
||||
import { getPersonCached } from "@formbricks/lib/services/person";
|
||||
import { getPersonCached, updatePersonAttribute } from "@formbricks/lib/services/person";
|
||||
import { ZJsPeopleAttributeInput } from "@formbricks/types/v1/js";
|
||||
import { revalidateTag } from "next/cache";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export async function OPTIONS(): Promise<NextResponse> {
|
||||
@@ -48,33 +46,7 @@ export async function POST(req: Request, { params }): Promise<NextResponse> {
|
||||
}
|
||||
|
||||
// upsert attribute (update or create)
|
||||
await prisma.attribute.upsert({
|
||||
where: {
|
||||
attributeClassId_personId: {
|
||||
attributeClassId: attributeClass.id,
|
||||
personId,
|
||||
},
|
||||
},
|
||||
update: {
|
||||
value,
|
||||
},
|
||||
create: {
|
||||
attributeClass: {
|
||||
connect: {
|
||||
id: attributeClass.id,
|
||||
},
|
||||
},
|
||||
person: {
|
||||
connect: {
|
||||
id: personId,
|
||||
},
|
||||
},
|
||||
value,
|
||||
},
|
||||
});
|
||||
|
||||
// revalidate person
|
||||
revalidateTag(personId);
|
||||
updatePersonAttribute(personId, attributeClass.id, value);
|
||||
|
||||
const state = await getUpdatedState(environmentId, personId, sessionId);
|
||||
|
||||
|
||||
@@ -268,3 +268,37 @@ export const getMonthlyActivePeopleCount = async (environmentId: string): Promis
|
||||
revalidate: 60 * 60 * 6, // 6 hours
|
||||
}
|
||||
)();
|
||||
|
||||
export const updatePersonAttribute = async (
|
||||
personId: string,
|
||||
attributeClassId: string,
|
||||
value: string
|
||||
): Promise<void> => {
|
||||
await prisma.attribute.upsert({
|
||||
where: {
|
||||
attributeClassId_personId: {
|
||||
attributeClassId,
|
||||
personId,
|
||||
},
|
||||
},
|
||||
update: {
|
||||
value,
|
||||
},
|
||||
create: {
|
||||
attributeClass: {
|
||||
connect: {
|
||||
id: attributeClassId,
|
||||
},
|
||||
},
|
||||
person: {
|
||||
connect: {
|
||||
id: personId,
|
||||
},
|
||||
},
|
||||
value,
|
||||
},
|
||||
});
|
||||
|
||||
// revalidate person
|
||||
revalidateTag(personId);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user