chore: moves prisma logic from set-attribute to person service (#840)

This commit is contained in:
Anshuman Pandey
2023-09-26 13:27:25 +05:30
committed by GitHub
parent 8181f74ad4
commit 95c466dbba
2 changed files with 36 additions and 30 deletions

View File

@@ -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);

View File

@@ -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);
};