mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-08 20:42:43 +00:00
perf(ai): read the deletion lock with the usage level, not on its own
The capability gate's check for an account pending deletion added a point read of users to every request that resolves AI capabilities, one statement over the budget on /api/auth/me, the dashboard snapshot and the batch ingest. The usage-level read already loads the record's row once per request; it now carries the deletion lock too.
This commit is contained in:
1 parent
52a80957f9
commit
a42b29a160
2 files changed
+14
-7
No files matched your search
@@ -41,7 +41,11 @@ import {
|
||||
type AiRecordKind,
|
||||
} from "./resolve";
|
||||
import type { AiCapabilities } from "./types";
|
||||
import { loadAiUsageState, loadRecordUsageLevel } from "./usage";
|
||||
import {
|
||||
loadAiUsageState,
|
||||
loadRecordAiUsage,
|
||||
loadRecordUsageLevel,
|
||||
} from "./usage";
|
||||
|
||||
/** Whose record, seen by whom, through which grant. */
|
||||
export interface AiCapabilityScope {
|
||||
@@ -89,11 +93,8 @@ function loadActiveConsents(
|
||||
* (`src/lib/account-deletion/active-account.ts`).
|
||||
*/
|
||||
async function loadRecordDeletionPending(recordId: string): Promise<boolean> {
|
||||
const row = await prisma.user.findUnique({
|
||||
where: { id: recordId },
|
||||
select: { deletionRequestedAt: true },
|
||||
});
|
||||
return row?.deletionRequestedAt != null;
|
||||
// The same memoised point read as the usage level.
|
||||
return (await loadRecordAiUsage(recordId)).deletionPending;
|
||||
}
|
||||
|
||||
function scopeKey(scope: AiCapabilityScope): string {
|
||||
|
||||
@@ -106,6 +106,11 @@ export function loadAiUsageSettings(): Promise<AiUsageSettings> {
|
||||
export interface RecordAiUsage extends AiUsageResolution {
|
||||
preference: AiUsageLevel | null;
|
||||
settings: AiUsageSettings;
|
||||
/**
|
||||
* The record is pending deletion. Read in the same point read as the level,
|
||||
* so the capability gate's deletion check costs no statement of its own.
|
||||
*/
|
||||
deletionPending: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -118,11 +123,12 @@ export function loadRecordAiUsage(recordId: string): Promise<RecordAiUsage> {
|
||||
loadAiUsageSettings(),
|
||||
prisma.user.findUnique({
|
||||
where: { id: recordId },
|
||||
select: { aiUsageLevel: true },
|
||||
select: { aiUsageLevel: true, deletionRequestedAt: true },
|
||||
}),
|
||||
]);
|
||||
const preference = usageLevelFromDb(row?.aiUsageLevel);
|
||||
return {
|
||||
deletionPending: row?.deletionRequestedAt != null,
|
||||
...resolveAiUsageLevel({
|
||||
preference,
|
||||
defaultLevel: settings.defaultLevel,
|
||||
|
||||
Reference in new issue
Block a user