fix: harden download history and attempt ledger

This commit is contained in:
Leon Tonhäuser committed 2026-08-10 16:45:40 +02:00
1 parent 510fde7afa
commit ba673ad732
19 files changed
+2287 -311

No files matched your search

+16 -4
View File
@@ -2,7 +2,7 @@ import "server-only";
import { randomUUID } from "node:crypto";
import { and, desc, eq, inArray, isNull, sql } from "drizzle-orm";
import { and, desc, eq, exists, inArray, isNull, sql } from "drizzle-orm";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { db } from "@/db/client";
@@ -16,6 +16,7 @@ import {
import type * as schema from "@/db/schema";
import { immichClient } from "@/modules/immich/client";
import type { ImmichAlbumSummary } from "@/modules/immich/types";
import { deliveredOriginalEventCondition } from "@/modules/downloads/eligibility";
import {
type CreateNewUsageInput,
@@ -28,7 +29,8 @@ import {
UsageAccountingError,
} from "./types";
export type PersistedDownloadIntent = DownloadIntent & {
export type PersistedDownloadIntent = Omit<DownloadIntent, "status"> & {
status: typeof downloadEvents.$inferSelect.status;
requestFingerprint: string;
kind: "new_usage" | "repeat_download";
};
@@ -243,6 +245,10 @@ export function createUsageServiceRepository(
},
async listPriorUsages(editorialId, assetId) {
const deliveredOriginal = database
.select({ id: downloadEvents.id })
.from(downloadEvents)
.where(deliveredOriginalEventCondition(editorialId, usages.id));
const rows = await database
.select({
id: usages.id,
@@ -262,6 +268,7 @@ export function createUsageServiceRepository(
eq(usages.editorialAccountId, editorialId),
eq(usages.immichAssetId, assetId),
inArray(usages.billingStatus, ["open", "billed", "reopened"]),
exists(deliveredOriginal),
),
)
.orderBy(desc(usages.createdAt));
@@ -331,8 +338,11 @@ export function resolveIntent(
if (persisted.requestFingerprint !== fingerprint || persisted.kind !== kind) {
throw new UsageAccountingError("IDEMPOTENCY_CONFLICT");
}
if (persisted.status !== "authorized") {
throw new UsageAccountingError("USAGE_NOT_FOUND");
}
const { requestFingerprint: _fingerprint, kind: _kind, ...intent } = persisted;
return intent;
return { ...intent, status: "authorized" };
}
class IdempotencyRaceError extends Error {}
@@ -382,6 +392,7 @@ const intentSelection = {
usageId: downloadEvents.usageId,
assetId: downloadEvents.immichAssetId,
authorizedAt: downloadEvents.authorizedAt,
eventStatus: downloadEvents.status,
intentKind: downloadEvents.intentKind,
requestFingerprint: downloadEvents.requestFingerprint,
usagePriceCents: usages.priceCents,
@@ -392,6 +403,7 @@ function persistedIntent(row: {
usageId: string;
assetId: string;
authorizedAt: Date;
eventStatus: PersistedDownloadIntent["status"];
intentKind: string;
requestFingerprint: string;
usagePriceCents: number;
@@ -408,7 +420,7 @@ function persistedIntent(row: {
assetId: row.assetId,
priceCents: row.intentKind === "new_usage" ? row.usagePriceCents : 0,
billable: row.intentKind === "new_usage",
status: "authorized",
status: row.eventStatus,
authorizedAt: row.authorizedAt,
requestFingerprint: row.requestFingerprint,
kind: row.intentKind,