Make sure to always check retention first

Fixes #2061
This commit is contained in:
Owen
2025-12-12 18:39:13 -05:00
parent ba99614d58
commit a012369f83

View File

@@ -148,7 +148,7 @@ export async function cleanUpOldLogs(orgId: string, retentionDays: number) {
} }
} }
export function logRequestAudit( export async function logRequestAudit(
data: { data: {
action: boolean; action: boolean;
reason: number; reason: number;
@@ -174,14 +174,13 @@ export function logRequestAudit(
} }
) { ) {
try { try {
// Quick synchronous check - if org has 0 retention, skip immediately // Check retention before buffering any logs
if (data.orgId) { if (data.orgId) {
const cached = cache.get<number>(`org_${data.orgId}_retentionDays`); const retentionDays = await getRetentionDays(data.orgId);
if (cached === 0) { if (retentionDays === 0) {
// do not log // do not log
return; return;
} }
// If not cached or > 0, we'll log it (async retention check happens in background)
} }
let actorType: string | undefined; let actorType: string | undefined;
@@ -261,16 +260,6 @@ export function logRequestAudit(
} else { } else {
scheduleFlush(); scheduleFlush();
} }
// Async retention check in background (don't await)
if (
data.orgId &&
cache.get<number>(`org_${data.orgId}_retentionDays`) === undefined
) {
getRetentionDays(data.orgId).catch((err) =>
logger.error("Error checking retention days:", err)
);
}
} catch (error) { } catch (error) {
logger.error(error); logger.error(error);
} }