import { resolveIdempotencyOwnership, type IdempotencyOwnership } from "@hivegrid/foundation"; import { getDbClient } from "./client.server.js"; import type { IdempotencyKey, Prisma } from "../generated/prisma/client/client.js"; export const DEFAULT_IDEMPOTENCY_SCOPE = "default"; export type IdempotencyResponse = { status: number; body?: Prisma.JsonValue; headers?: Record; }; export type IdempotencyKeyLookup = IdempotencyOwnership & { scope?: string | null; key: string; }; export type StoredIdempotencyResponse = IdempotencyResponse & { requestHash: string; expiresAt: Date | null; }; export type CreateStoredIdempotencyResponseInput = IdempotencyKeyLookup & { requestHash: string; response: IdempotencyResponse; expiresAt?: Date | null; }; export type IdempotencyKeyReaderClient = { idempotencyKey: { findUnique(args: { where: { idempotency_key_partition_scope_key: { partitionKey: string; scope: string; key: string; }; }; }): Promise; }; }; export type IdempotencyKeyWriterClient = { idempotencyKey: { create(args: { data: { tenantId: string | null; partitionKey: string; systemOwned: boolean; scope: string; key: string; requestHash: string; responseStatus: number; responseBody: Prisma.JsonValue | null; responseHeaders: Record | null; expiresAt: Date | null; }; }): Promise; }; }; export type IdempotencyKeyClient = IdempotencyKeyReaderClient & IdempotencyKeyWriterClient; export function normalizeIdempotencyScope(scope?: string | null): string { return scope?.trim() || DEFAULT_IDEMPOTENCY_SCOPE; } export function mapIdempotencyKeyToResponse(row: IdempotencyKey): StoredIdempotencyResponse { return { requestHash: row.requestHash, status: row.responseStatus, body: row.responseBody ?? undefined, headers: mapResponseHeaders(row.responseHeaders), expiresAt: row.expiresAt ?? null, }; } export async function findIdempotencyKeyByScopeAndKey( input: IdempotencyKeyLookup, client: IdempotencyKeyReaderClient = getDbClient() as unknown as IdempotencyKeyReaderClient ): Promise { const ownership = resolveIdempotencyOwnership(input); return client.idempotencyKey.findUnique({ where: { idempotency_key_partition_scope_key: { partitionKey: ownership.partitionKey, scope: normalizeIdempotencyScope(input.scope), key: input.key, }, }, }); } export async function findStoredIdempotencyResponse( input: IdempotencyKeyLookup, client: IdempotencyKeyReaderClient = getDbClient() as unknown as IdempotencyKeyReaderClient ): Promise { const row = await findIdempotencyKeyByScopeAndKey(input, client); return row ? mapIdempotencyKeyToResponse(row) : null; } export async function createStoredIdempotencyResponse( input: CreateStoredIdempotencyResponseInput, client: IdempotencyKeyWriterClient = getDbClient() as unknown as IdempotencyKeyWriterClient ): Promise { const ownership = resolveIdempotencyOwnership(input); return client.idempotencyKey.create({ data: { tenantId: ownership.tenantId, partitionKey: ownership.partitionKey, systemOwned: ownership.systemOwned, scope: normalizeIdempotencyScope(input.scope), key: input.key, requestHash: input.requestHash, responseStatus: input.response.status, responseBody: input.response.body ?? null, responseHeaders: input.response.headers ?? null, expiresAt: input.expiresAt ?? null, }, }); } export const createIdempotencyKeyResponse = createStoredIdempotencyResponse; function mapResponseHeaders(headers: Prisma.JsonValue | null): Record | undefined { if (!headers || typeof headers !== "object" || Array.isArray(headers)) { return undefined; } const entries = Object.entries(headers).filter((entry): entry is [string, string] => typeof entry[1] === "string"); return entries.length > 0 ? Object.fromEntries(entries) : undefined; }