fix(housekeeping): harden preference persistence

This commit is contained in:
simoleo89
2026-08-27 17:44:53 +02:00
parent 64bb230de5
commit 4e0bf598ed
7 changed files with 272 additions and 82 deletions
+25 -27
View File
@@ -1,42 +1,41 @@
"use server";
import { authorizeHousekeeping } from "@/features/housekeeping/foundation/authorization";
import {
anyCapability,
fail,
type HousekeepingCapabilityContext,
type HousekeepingResult,
ok,
} from "@/features/housekeeping/foundation/contracts";
import { createCorrelationId } from "@/features/housekeeping/foundation/correlation";
import { reconcilePreferences } from "@/features/housekeeping/foundation/preferences/reconcile";
import type { HousekeepingPreferencesRepository } from "@/features/housekeeping/foundation/preferences/repository";
import {
type HousekeepingPreferences,
housekeepingPreferencesSchema,
} from "@/features/housekeeping/foundation/preferences/schema";
import type { HousekeepingRegistry } from "@/features/housekeeping/foundation/registry";
import { createHousekeepingRegistry } from "@/features/housekeeping/foundation/registry";
import { getHousekeepingCapabilityContext } from "@/features/housekeeping/foundation/server-capability-context";
import { HOUSEKEEPING_MANIFESTS } from "@/features/housekeeping/manifests";
import { housekeepingPreferencesRepository } from "@/lib/housekeeping-preferences-repository";
import { PERMS } from "@/lib/permission-slugs";
const preferencesCapability = anyCapability(PERMS.ADMIN_DASHBOARD);
const housekeepingRegistry = createHousekeepingRegistry(HOUSEKEEPING_MANIFESTS);
export interface HousekeepingPreferencesActionDependencies {
repository: HousekeepingPreferencesRepository;
registry: HousekeepingRegistry;
getContext?: () => Promise<HousekeepingCapabilityContext>;
}
export async function loadHousekeepingPreferences(
dependencies: HousekeepingPreferencesActionDependencies,
): Promise<HousekeepingResult<HousekeepingPreferences>> {
const context = await resolveContext(dependencies);
export async function loadHousekeepingPreferences(): Promise<
HousekeepingResult<HousekeepingPreferences>
> {
const context = await getHousekeepingCapabilityContext();
const authorization = authorizeHousekeeping(context, preferencesCapability);
if (!authorization.ok) return authorization;
const correlationId = createCorrelationId();
try {
const stored = await dependencies.repository.read(context.actor.id);
const stored = await housekeepingPreferencesRepository.read(
context.actor.id,
);
return ok(
reconcilePreferences(stored, dependencies.registry, context),
reconcilePreferences(stored, housekeepingRegistry, context),
correlationId,
);
} catch {
@@ -50,9 +49,8 @@ export async function loadHousekeepingPreferences(
export async function saveHousekeepingPreferences(
input: unknown,
dependencies: HousekeepingPreferencesActionDependencies,
): Promise<HousekeepingResult<HousekeepingPreferences>> {
const context = await resolveContext(dependencies);
const context = await getHousekeepingCapabilityContext();
const authorization = authorizeHousekeeping(context, preferencesCapability);
if (!authorization.ok) return authorization;
@@ -66,9 +64,17 @@ export async function saveHousekeepingPreferences(
);
}
const reconciled = reconcilePreferences(
parsed.data,
housekeepingRegistry,
context,
);
try {
await dependencies.repository.upsert(context.actor.id, parsed.data);
return ok(parsed.data, correlationId);
await housekeepingPreferencesRepository.upsert(
context.actor.id,
reconciled,
);
return ok(reconciled, correlationId);
} catch {
return fail(
"INTERNAL",
@@ -77,11 +83,3 @@ export async function saveHousekeepingPreferences(
);
}
}
async function resolveContext(
dependencies: HousekeepingPreferencesActionDependencies,
): Promise<HousekeepingCapabilityContext> {
return dependencies.getContext
? dependencies.getContext()
: getHousekeepingCapabilityContext();
}