fix: read-after-write issues (#215)
* fix: read-after-write issues * fix: coderabbit comments * fix: clear cache on invite * fix: use primary after a read
This commit is contained in:
committed by
GitHub
parent
abacf66155
commit
f454449365
@@ -9,7 +9,7 @@ import {
|
||||
} from '@/utils/ai-tools';
|
||||
import { HttpError } from '@/utils/errors';
|
||||
import { db, getOrganizationByProjectIdCached } from '@openpanel/db';
|
||||
import { getProjectAccessCached } from '@openpanel/trpc/src/access';
|
||||
import { getProjectAccess } from '@openpanel/trpc/src/access';
|
||||
import { type Message, appendResponseMessages, streamText } from 'ai';
|
||||
import type { FastifyReply, FastifyRequest } from 'fastify';
|
||||
|
||||
@@ -37,7 +37,7 @@ export async function chat(
|
||||
}
|
||||
|
||||
const organization = await getOrganizationByProjectIdCached(projectId);
|
||||
const access = await getProjectAccessCached({
|
||||
const access = await getProjectAccess({
|
||||
projectId,
|
||||
userId: session.userId,
|
||||
});
|
||||
|
||||
@@ -113,6 +113,17 @@ export async function slackWebhook(
|
||||
}
|
||||
}
|
||||
|
||||
async function clearOrganizationCache(organizationId: string) {
|
||||
const projects = await db.project.findMany({
|
||||
where: {
|
||||
organizationId,
|
||||
},
|
||||
});
|
||||
for (const project of projects) {
|
||||
await getOrganizationByProjectIdCached.clear(project.id);
|
||||
}
|
||||
}
|
||||
|
||||
export async function polarWebhook(
|
||||
request: FastifyRequest<{
|
||||
Querystring: unknown;
|
||||
@@ -141,8 +152,11 @@ export async function polarWebhook(
|
||||
},
|
||||
data: {
|
||||
subscriptionPeriodEventsCount: 0,
|
||||
subscriptionPeriodEventsCountExceededAt: null,
|
||||
},
|
||||
});
|
||||
|
||||
await clearOrganizationCache(metadata.organizationId);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -205,15 +219,7 @@ export async function polarWebhook(
|
||||
},
|
||||
});
|
||||
|
||||
const projects = await db.project.findMany({
|
||||
where: {
|
||||
organizationId: metadata.organizationId,
|
||||
},
|
||||
});
|
||||
|
||||
for (const project of projects) {
|
||||
await getOrganizationByProjectIdCached.clear(project.id);
|
||||
}
|
||||
await clearOrganizationCache(metadata.organizationId);
|
||||
|
||||
await publishEvent('organization', 'subscription_updated', {
|
||||
organizationId: metadata.organizationId,
|
||||
|
||||
Reference in New Issue
Block a user