mirror of
https://github.com/formbricks/formbricks.git
synced 2025-12-30 10:19:51 -06:00
fix: adds cached service for getting contact with attributes (#4598)
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { contactCache } from "@/lib/cache/contact";
|
||||
import { contactAttributeCache } from "@/lib/cache/contact-attribute";
|
||||
import { contactAttributeKeyCache } from "@/lib/cache/contact-attribute-key";
|
||||
import { cache as reactCache } from "react";
|
||||
import { prisma } from "@formbricks/database";
|
||||
import { cache } from "@formbricks/lib/cache";
|
||||
|
||||
export const getContactByUserIdWithAttributes = reactCache(
|
||||
(environmentId: string, userId: string, updatedAttributes: Record<string, string>) =>
|
||||
cache(
|
||||
async () => {
|
||||
const contact = await prisma.contact.findFirst({
|
||||
where: {
|
||||
environmentId,
|
||||
attributes: { some: { attributeKey: { key: "userId", environmentId }, value: userId } },
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
attributes: {
|
||||
where: {
|
||||
attributeKey: {
|
||||
key: {
|
||||
in: Object.keys(updatedAttributes),
|
||||
},
|
||||
},
|
||||
},
|
||||
select: { attributeKey: { select: { key: true } }, value: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!contact) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return contact;
|
||||
},
|
||||
[`getContactByUserId-${environmentId}-${userId}-${JSON.stringify(updatedAttributes)}`],
|
||||
{
|
||||
tags: [
|
||||
contactCache.tag.byEnvironmentIdAndUserId(environmentId, userId),
|
||||
contactAttributeCache.tag.byEnvironmentIdAndUserId(environmentId, userId),
|
||||
contactAttributeKeyCache.tag.byEnvironmentId(environmentId),
|
||||
],
|
||||
}
|
||||
)()
|
||||
);
|
||||
@@ -2,10 +2,10 @@ import { responses } from "@/app/lib/api/response";
|
||||
import { transformErrorToDetails } from "@/app/lib/api/validator";
|
||||
import { getIsContactsEnabled } from "@/modules/ee/license-check/lib/utils";
|
||||
import { NextRequest } from "next/server";
|
||||
import { prisma } from "@formbricks/database";
|
||||
import { ResourceNotFoundError } from "@formbricks/types/errors";
|
||||
import { ZJsContactsUpdateAttributeInput } from "@formbricks/types/js";
|
||||
import { updateAttributes } from "./lib/attributes";
|
||||
import { getContactByUserIdWithAttributes } from "./lib/contact";
|
||||
|
||||
export const OPTIONS = async () => {
|
||||
// cors headers
|
||||
@@ -48,25 +48,7 @@ export const PUT = async (
|
||||
// ignore userId and id
|
||||
const { userId: userIdAttr, id: idAttr, ...updatedAttributes } = parsedInput.data.attributes;
|
||||
|
||||
const contact = await prisma.contact.findFirst({
|
||||
where: {
|
||||
environmentId,
|
||||
attributes: { some: { attributeKey: { key: "userId", environmentId }, value: userId } },
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
attributes: {
|
||||
where: {
|
||||
attributeKey: {
|
||||
key: {
|
||||
in: Object.keys(updatedAttributes),
|
||||
},
|
||||
},
|
||||
},
|
||||
select: { attributeKey: { select: { key: true } }, value: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
const contact = await getContactByUserIdWithAttributes(environmentId, userId, updatedAttributes);
|
||||
|
||||
if (!contact) {
|
||||
return responses.notFoundResponse("contact", userId, true);
|
||||
|
||||
Reference in New Issue
Block a user