feat: add billing period selection
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
ba673ad732
commit
d37affc80f
15 files changed
+1625
No files matched your search
@@ -0,0 +1,243 @@
|
||||
import { expect, test } from "vitest";
|
||||
|
||||
import {
|
||||
createBillingQueries,
|
||||
type BillingRepository,
|
||||
type RawBillableUsage,
|
||||
type RawRepeatDownload,
|
||||
} from "./queries";
|
||||
import { monthPeriod, type BillingPeriod } from "./period";
|
||||
|
||||
const august2026: BillingPeriod = monthPeriod(2026, 8);
|
||||
const fromUtc = august2026.fromUtc;
|
||||
const toUtcExclusive = august2026.toUtcExclusive;
|
||||
|
||||
function createRepository() {
|
||||
const billable: RawBillableUsage[] = [];
|
||||
const repeatDownloads: RawRepeatDownload[] = [];
|
||||
|
||||
const repository: BillingRepository = {
|
||||
async findBillableUsages(editorialId, from, to) {
|
||||
expect(editorialId).toBe("e1");
|
||||
expect(from).toBe(fromUtc);
|
||||
expect(to).toBe(toUtcExclusive);
|
||||
return billable;
|
||||
},
|
||||
async findRepeatDownloads(editorialId, from, to) {
|
||||
expect(editorialId).toBe("e1");
|
||||
expect(from).toBe(fromUtc);
|
||||
expect(to).toBe(toUtcExclusive);
|
||||
return repeatDownloads;
|
||||
},
|
||||
};
|
||||
|
||||
return { repository, billable, repeatDownloads };
|
||||
}
|
||||
|
||||
function createQueries() {
|
||||
const state = createRepository();
|
||||
return { ...state, queries: createBillingQueries({ repository: state.repository }) };
|
||||
}
|
||||
|
||||
test("returns billed rows disabled and open rows selectable", async () => {
|
||||
const { queries, billable } = createQueries();
|
||||
billable.push(
|
||||
{
|
||||
id: "u1",
|
||||
immichAssetId: "asset-1",
|
||||
filenameSnapshot: "jubel.jpg",
|
||||
albumNameSnapshot: "Presse 2026",
|
||||
purpose: "Titelgeschichte",
|
||||
internalReference: "Ausgabe 8/26",
|
||||
priceCents: 1_500,
|
||||
billingStatus: "open",
|
||||
createdAt: new Date("2026-08-05T10:00:00.000Z"),
|
||||
billingReportId: null,
|
||||
reportNumber: null,
|
||||
invoiceNumber: null,
|
||||
},
|
||||
{
|
||||
id: "u2",
|
||||
immichAssetId: "asset-2",
|
||||
filenameSnapshot: "sport.jpg",
|
||||
albumNameSnapshot: "Presse 2026",
|
||||
purpose: "Sportbericht",
|
||||
internalReference: null,
|
||||
priceCents: 1_500,
|
||||
billingStatus: "billed",
|
||||
createdAt: new Date("2026-08-06T10:00:00.000Z"),
|
||||
billingReportId: "r1",
|
||||
reportNumber: 42,
|
||||
invoiceNumber: "RE-2026-042",
|
||||
},
|
||||
{
|
||||
id: "u3",
|
||||
immichAssetId: "asset-3",
|
||||
filenameSnapshot: "redaktion.jpg",
|
||||
albumNameSnapshot: "Presse 2026",
|
||||
purpose: "Reopened story",
|
||||
internalReference: null,
|
||||
priceCents: 1_500,
|
||||
billingStatus: "reopened",
|
||||
createdAt: new Date("2026-08-07T10:00:00.000Z"),
|
||||
billingReportId: null,
|
||||
reportNumber: null,
|
||||
invoiceNumber: null,
|
||||
},
|
||||
);
|
||||
|
||||
const result = await queries.listBillablePositions("e1", august2026);
|
||||
|
||||
expect(result.find((x) => x.status === "billed")?.selectable).toBe(false);
|
||||
expect(result.find((x) => x.status === "open")?.selectable).toBe(true);
|
||||
expect(result.find((x) => x.status === "reopened")?.selectable).toBe(true);
|
||||
});
|
||||
|
||||
test("maps raw billable usages to billing positions with snapshot fields", async () => {
|
||||
const { queries, billable } = createQueries();
|
||||
billable.push({
|
||||
id: "u1",
|
||||
immichAssetId: "asset-1",
|
||||
filenameSnapshot: "jubel.jpg",
|
||||
albumNameSnapshot: "Presse 2026",
|
||||
purpose: "Titelgeschichte",
|
||||
internalReference: "Ausgabe 8/26",
|
||||
priceCents: 6_000,
|
||||
billingStatus: "open",
|
||||
createdAt: new Date("2026-08-05T10:00:00.000Z"),
|
||||
billingReportId: null,
|
||||
reportNumber: null,
|
||||
invoiceNumber: null,
|
||||
});
|
||||
|
||||
const [position] = await queries.listBillablePositions("e1", august2026);
|
||||
|
||||
expect(position).toEqual({
|
||||
id: "u1",
|
||||
assetId: "asset-1",
|
||||
filename: "jubel.jpg",
|
||||
albumName: "Presse 2026",
|
||||
purpose: "Titelgeschichte",
|
||||
reference: "Ausgabe 8/26",
|
||||
priceCents: 6_000,
|
||||
status: "open",
|
||||
selectable: true,
|
||||
createdAt: new Date("2026-08-05T10:00:00.000Z"),
|
||||
reportNumber: null,
|
||||
invoiceNumber: null,
|
||||
});
|
||||
});
|
||||
|
||||
test("surfaces report and invoice references on billed positions", async () => {
|
||||
const { queries, billable } = createQueries();
|
||||
billable.push({
|
||||
id: "u2",
|
||||
immichAssetId: "asset-2",
|
||||
filenameSnapshot: "sport.jpg",
|
||||
albumNameSnapshot: "Presse 2026",
|
||||
purpose: "Sportbericht",
|
||||
internalReference: null,
|
||||
priceCents: 1_500,
|
||||
billingStatus: "billed",
|
||||
createdAt: new Date("2026-08-06T10:00:00.000Z"),
|
||||
billingReportId: "r1",
|
||||
reportNumber: 42,
|
||||
invoiceNumber: "RE-2026-042",
|
||||
});
|
||||
|
||||
const [position] = await queries.listBillablePositions("e1", august2026);
|
||||
|
||||
expect(position.status).toBe("billed");
|
||||
expect(position.selectable).toBe(false);
|
||||
expect(position.reportNumber).toBe(42);
|
||||
expect(position.invoiceNumber).toBe("RE-2026-042");
|
||||
});
|
||||
|
||||
test("excludes voided usages from billable positions", async () => {
|
||||
const { queries, billable } = createQueries();
|
||||
billable.push(
|
||||
{
|
||||
id: "u-void",
|
||||
immichAssetId: "asset-void",
|
||||
filenameSnapshot: "void.jpg",
|
||||
albumNameSnapshot: "Presse 2026",
|
||||
purpose: "Voided",
|
||||
internalReference: null,
|
||||
priceCents: 1_500,
|
||||
billingStatus: "voided",
|
||||
createdAt: new Date("2026-08-03T10:00:00.000Z"),
|
||||
billingReportId: null,
|
||||
reportNumber: null,
|
||||
invoiceNumber: null,
|
||||
},
|
||||
{
|
||||
id: "u-open",
|
||||
immichAssetId: "asset-open",
|
||||
filenameSnapshot: "open.jpg",
|
||||
albumNameSnapshot: "Presse 2026",
|
||||
purpose: "Open",
|
||||
internalReference: null,
|
||||
priceCents: 1_500,
|
||||
billingStatus: "open",
|
||||
createdAt: new Date("2026-08-04T10:00:00.000Z"),
|
||||
billingReportId: null,
|
||||
reportNumber: null,
|
||||
invoiceNumber: null,
|
||||
},
|
||||
);
|
||||
|
||||
const result = await queries.listBillablePositions("e1", august2026);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].id).toBe("u-open");
|
||||
});
|
||||
|
||||
test("returns repeat download events in the period as non-billable positions", async () => {
|
||||
const { queries, repeatDownloads } = createQueries();
|
||||
repeatDownloads.push({
|
||||
id: "d1",
|
||||
immichAssetId: "asset-1",
|
||||
usageId: "u1",
|
||||
authorizedAt: new Date("2026-08-10T12:00:00.000Z"),
|
||||
status: "completed",
|
||||
repeatDownloadNote: "Zweite Nutzung",
|
||||
});
|
||||
|
||||
const result = await queries.listNonBillableDownloads("e1", august2026);
|
||||
|
||||
expect(result).toEqual([
|
||||
{
|
||||
id: "d1",
|
||||
assetId: "asset-1",
|
||||
usageId: "u1",
|
||||
authorizedAt: new Date("2026-08-10T12:00:00.000Z"),
|
||||
status: "completed",
|
||||
repeatDownloadNote: "Zweite Nutzung",
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test("includes repeat downloads even if the original usage was already billed", async () => {
|
||||
const { queries, repeatDownloads } = createQueries();
|
||||
repeatDownloads.push({
|
||||
id: "d1",
|
||||
immichAssetId: "asset-1",
|
||||
usageId: "u1",
|
||||
authorizedAt: new Date("2026-08-10T12:00:00.000Z"),
|
||||
status: "interrupted",
|
||||
repeatDownloadNote: null,
|
||||
});
|
||||
|
||||
const result = await queries.listNonBillableDownloads("e1", august2026);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].status).toBe("interrupted");
|
||||
});
|
||||
|
||||
test("returns an empty array when no repeat downloads exist", async () => {
|
||||
const { queries } = createQueries();
|
||||
|
||||
const result = await queries.listNonBillableDownloads("e1", august2026);
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
Reference in new issue
Block a user