fix: harden usage accounting idempotency
This commit is contained in:
1 parent
22fe6bf659
commit
3de1530074
14 files changed
+708
-107
No files matched your search
@@ -8,9 +8,10 @@ import {
|
||||
} from "./service";
|
||||
import { UsageAccountingError } from "@/modules/usages/types";
|
||||
|
||||
const editorialId = "11111111-1111-4111-8111-111111111111";
|
||||
const usageId = "44444444-4444-4444-8444-444444444444";
|
||||
const idempotencyKey = "55555555-5555-4555-8555-555555555555";
|
||||
const editorialId = "1111111a-1111-4111-8111-11111111111a";
|
||||
const usageId = "4444444d-4444-4444-8444-44444444444d";
|
||||
const idempotencyKey = "5555555e-5555-4555-8555-55555555555e";
|
||||
const assetId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
||||
|
||||
function createService() {
|
||||
const intents = new Map<string, PersistedRepeatIntent>();
|
||||
@@ -33,7 +34,7 @@ function createService() {
|
||||
const intent: PersistedRepeatIntent = {
|
||||
id: `download-${events.length}`,
|
||||
usageId: input.usageId,
|
||||
assetId: "asset-1",
|
||||
assetId,
|
||||
priceCents: 0,
|
||||
billable: false,
|
||||
status: "authorized",
|
||||
@@ -77,7 +78,7 @@ test("creates only a free authorized event for an owned prior usage", async () =
|
||||
|
||||
expect(intent).toMatchObject({
|
||||
usageId,
|
||||
assetId: "asset-1",
|
||||
assetId,
|
||||
priceCents: 0,
|
||||
billable: false,
|
||||
status: "authorized",
|
||||
@@ -126,6 +127,23 @@ test("returns the same free intent once for an equivalent retry", async () => {
|
||||
expect(events).toHaveLength(1);
|
||||
});
|
||||
|
||||
test("treats mixed-case UUID input as the same repeat intent", async () => {
|
||||
const { service, events } = createService();
|
||||
const uppercaseInput = {
|
||||
editorialId: editorialId.toUpperCase(),
|
||||
usageId: usageId.toUpperCase(),
|
||||
idempotencyKey: idempotencyKey.toUpperCase(),
|
||||
};
|
||||
|
||||
const first = await service.createRepeatDownload(uppercaseInput);
|
||||
const second = await service.createRepeatDownload({ editorialId, usageId, idempotencyKey });
|
||||
|
||||
expect(second).toEqual(first);
|
||||
expect(events).toEqual([
|
||||
expect.objectContaining({ editorialId, usageId, idempotencyKey }),
|
||||
]);
|
||||
});
|
||||
|
||||
test("rejects reuse of a repeat key with a materially different note", async () => {
|
||||
const { service, events } = createService();
|
||||
await service.createRepeatDownload({ editorialId, usageId, note: "Web", idempotencyKey });
|
||||
|
||||
@@ -9,10 +9,10 @@ import { db } from "@/db/client";
|
||||
import { downloadEvents, editorialAccounts, usages } from "@/db/schema";
|
||||
import type * as schema from "@/db/schema";
|
||||
import {
|
||||
assertUuid,
|
||||
type CreateRepeatDownloadInput,
|
||||
type DownloadIntent,
|
||||
normalizeOptionalText,
|
||||
normalizeUuid,
|
||||
requestFingerprint,
|
||||
UsageAccountingError,
|
||||
} from "@/modules/usages/types";
|
||||
@@ -71,6 +71,13 @@ export function createRepeatDownloadRepository(
|
||||
try {
|
||||
return await database.transaction(async (transaction) => {
|
||||
await lockEditorialAccounting(transaction, input.editorialId);
|
||||
const existing = await findPersistedIntent(
|
||||
transaction,
|
||||
input.editorialId,
|
||||
input.idempotencyKey,
|
||||
);
|
||||
if (existing) return asRepeatIntent(existing);
|
||||
|
||||
const [editorial] = await transaction
|
||||
.select({ id: editorialAccounts.id, status: editorialAccounts.status })
|
||||
.from(editorialAccounts)
|
||||
@@ -81,13 +88,6 @@ export function createRepeatDownloadRepository(
|
||||
throw new UsageAccountingError("EDITORIAL_NOT_ACTIVE");
|
||||
}
|
||||
|
||||
const existing = await findPersistedIntent(
|
||||
transaction,
|
||||
input.editorialId,
|
||||
input.idempotencyKey,
|
||||
);
|
||||
if (existing) return asRepeatIntent(existing);
|
||||
|
||||
const [usage] = await transaction
|
||||
.select({ id: usages.id, assetId: usages.immichAssetId })
|
||||
.from(usages)
|
||||
@@ -149,15 +149,12 @@ export function createRepeatDownloadRepository(
|
||||
|
||||
function normalizeRepeatInput(input: CreateRepeatDownloadInput): CreateRepeatDownloadInput {
|
||||
if (!input || typeof input !== "object") throw new UsageAccountingError("INVALID_INPUT");
|
||||
assertUuid(input.editorialId);
|
||||
assertUuid(input.usageId);
|
||||
assertUuid(input.idempotencyKey);
|
||||
const note = normalizeOptionalText(input.note, 200);
|
||||
return {
|
||||
editorialId: input.editorialId,
|
||||
usageId: input.usageId,
|
||||
editorialId: normalizeUuid(input.editorialId),
|
||||
usageId: normalizeUuid(input.usageId),
|
||||
...(note === undefined ? {} : { note }),
|
||||
idempotencyKey: input.idempotencyKey,
|
||||
idempotencyKey: normalizeUuid(input.idempotencyKey),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user