Skip to content

Commit

Permalink
fix: adds cached service for getting contact with attributes (formbri…
Browse files Browse the repository at this point in the history
  • Loading branch information
pandeymangg authored Jan 15, 2025
1 parent 7c60c57 commit d3adc16
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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),
],
}
)()
);
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d3adc16

Please sign in to comment.