fix: personByUserId not cached properly (#1644)

This commit is contained in:
Matti Nannt
2023-11-20 21:05:58 +01:00
committed by GitHub
parent 8c38495812
commit 91ceffba01
3 changed files with 13 additions and 29 deletions

View File

@@ -25,6 +25,6 @@ export const canUserAccessPerson = async (userId: string, personId: string): Pro
[`canUserAccessPerson-${userId}-people-${personId}`],
{
revalidate: SERVICES_REVALIDATION_INTERVAL,
tags: [personCache.tag.byId(personId), personCache.tag.byUserId(userId)],
tags: [personCache.tag.byId(personId)],
}
)();

View File

@@ -14,11 +14,8 @@ export const personCache = {
byEnvironmentId(environmentId: string): string {
return `environments-${environmentId}-people`;
},
byUserId(userId: string): string {
return `users-${userId}-people`;
},
byEnvironmentIdAndUserId(environmentId: string, userId: string): string {
return `environments-${environmentId}-users-${userId}-people`;
return `environments-${environmentId}-personByUserId-${userId}`;
},
},
revalidate({ id, environmentId, userId }: RevalidateProps): void {
@@ -26,16 +23,12 @@ export const personCache = {
revalidateTag(this.tag.byId(id));
}
if (environmentId) {
revalidateTag(this.tag.byEnvironmentId(environmentId));
}
if (userId) {
revalidateTag(this.tag.byUserId(userId));
}
if (environmentId && userId) {
revalidateTag(this.tag.byEnvironmentIdAndUserId(environmentId, userId));
}
if (environmentId) {
revalidateTag(this.tag.byEnvironmentId(environmentId));
}
},
};

View File

@@ -291,10 +291,10 @@ export const updatePerson = async (personId: string, personInput: TPersonUpdateI
}
};
export const getPersonByUserId = async (environmentId: string, userId: string): Promise<TPerson | null> => {
const personPrisma = await unstable_cache(
export const getPersonByUserId = async (environmentId: string, userId: string): Promise<TPerson | null> =>
await unstable_cache(
async () => {
validateInputs([userId, ZString], [environmentId, ZId]);
validateInputs([environmentId, ZId], [userId, ZString]);
// check if userId exists as a column
const personWithUserId = await prisma.person.findFirst({
@@ -306,7 +306,7 @@ export const getPersonByUserId = async (environmentId: string, userId: string):
});
if (personWithUserId) {
return personWithUserId;
return transformPrismaPerson(personWithUserId);
}
// Check if a person with the userId attribute exists
@@ -352,23 +352,14 @@ export const getPersonByUserId = async (environmentId: string, userId: string):
userId,
});
return personWithUserIdAttribute;
return transformPrismaPerson(personWithUserIdAttribute);
},
[`getPersonByUserId-${userId}-${environmentId}`],
[`getPersonByUserId-${environmentId}-${userId}`],
{
tags: [
personCache.tag.byEnvironmentIdAndUserId(environmentId, userId),
personCache.tag.byUserId(userId), // fix for caching issue on vercel
personCache.tag.byEnvironmentId(environmentId), // fix for caching issue on vercel
],
tags: [personCache.tag.byEnvironmentIdAndUserId(environmentId, userId)],
revalidate: SERVICES_REVALIDATION_INTERVAL,
}
)();
if (!personPrisma) {
return null;
}
return transformPrismaPerson(personPrisma);
};
export const updatePersonAttribute = async (
personId: string,