From 95c466dbba39a797da9d4aa9c0d227ad7db75d39 Mon Sep 17 00:00:00 2001 From: Anshuman Pandey <54475686+pandeymangg@users.noreply.github.com> Date: Tue, 26 Sep 2023 13:27:25 +0530 Subject: [PATCH] chore: moves prisma logic from set-attribute to person service (#840) --- .../people/[personId]/set-attribute/route.ts | 32 ++--------------- packages/lib/services/person.ts | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/apps/web/app/api/v1/js/people/[personId]/set-attribute/route.ts b/apps/web/app/api/v1/js/people/[personId]/set-attribute/route.ts index f0050b31cd..10cafc9191 100644 --- a/apps/web/app/api/v1/js/people/[personId]/set-attribute/route.ts +++ b/apps/web/app/api/v1/js/people/[personId]/set-attribute/route.ts @@ -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 { @@ -48,33 +46,7 @@ export async function POST(req: Request, { params }): Promise { } // 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); diff --git a/packages/lib/services/person.ts b/packages/lib/services/person.ts index 253ece71b9..e3aa9d6c34 100644 --- a/packages/lib/services/person.ts +++ b/packages/lib/services/person.ts @@ -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 => { + 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); +};