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,
|
||||
|
||||
@@ -8,14 +8,18 @@ import Fastify from 'fastify';
|
||||
import metricsPlugin from 'fastify-metrics';
|
||||
|
||||
import { generateId } from '@openpanel/common';
|
||||
import type { IServiceClientWithProject } from '@openpanel/db';
|
||||
import { getRedisPub } from '@openpanel/redis';
|
||||
import {
|
||||
type IServiceClientWithProject,
|
||||
runWithAlsSession,
|
||||
} from '@openpanel/db';
|
||||
import { getCache, getRedisPub } from '@openpanel/redis';
|
||||
import type { AppRouter } from '@openpanel/trpc';
|
||||
import { appRouter, createContext } from '@openpanel/trpc';
|
||||
|
||||
import {
|
||||
EMPTY_SESSION,
|
||||
type SessionValidationResult,
|
||||
decodeSessionToken,
|
||||
validateSessionToken,
|
||||
} from '@openpanel/auth';
|
||||
import sourceMapSupport from 'source-map-support';
|
||||
@@ -140,7 +144,14 @@ const startServer = async () => {
|
||||
instance.addHook('onRequest', async (req) => {
|
||||
if (req.cookies?.session) {
|
||||
try {
|
||||
const session = await validateSessionToken(req.cookies.session);
|
||||
const sessionId = decodeSessionToken(req.cookies.session);
|
||||
const session = await runWithAlsSession(sessionId, () =>
|
||||
sessionId
|
||||
? getCache(`validateSession:${sessionId}`, 60 * 5, async () =>
|
||||
validateSessionToken(req.cookies.session),
|
||||
)
|
||||
: validateSessionToken(req.cookies.session),
|
||||
);
|
||||
if (session.session) {
|
||||
req.session = session;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user