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,25 @@
|
||||
import { expect, test } from "vitest";
|
||||
|
||||
import { formatCents } from "./format";
|
||||
|
||||
test("formats whole euros without fractional cents", () => {
|
||||
expect(formatCents(6_000)).toBe("60,00 EUR");
|
||||
});
|
||||
|
||||
test("formats fractional cents with zero padding", () => {
|
||||
expect(formatCents(1_500)).toBe("15,00 EUR");
|
||||
expect(formatCents(1_599)).toBe("15,99 EUR");
|
||||
expect(formatCents(99)).toBe("0,99 EUR");
|
||||
});
|
||||
|
||||
test("formats zero as 0,00 EUR", () => {
|
||||
expect(formatCents(0)).toBe("0,00 EUR");
|
||||
});
|
||||
|
||||
test("formats large amounts with German thousands separators", () => {
|
||||
expect(formatCents(1_234_567)).toBe("12.345,67 EUR");
|
||||
});
|
||||
|
||||
test("formats negative amounts with a leading minus", () => {
|
||||
expect(formatCents(-1_500)).toBe("-15,00 EUR");
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Formats an integer-Euro-cents amount as a German EUR display string using
|
||||
* only integer math (division and modulo). Never converts to floating point.
|
||||
*
|
||||
* Examples: 6000 -> "60,00 EUR", 1500 -> "15,00 EUR", 0 -> "0,00 EUR"
|
||||
*/
|
||||
export function formatCents(cents: number): string {
|
||||
const sign = cents < 0 ? "-" : "";
|
||||
const absCents = Math.abs(Math.trunc(cents));
|
||||
const euros = Math.floor(absCents / 100);
|
||||
const remainingCents = absCents % 100;
|
||||
const euroString = new Intl.NumberFormat("de-DE").format(euros);
|
||||
const centsString = remainingCents.toString().padStart(2, "0");
|
||||
return `${sign}${euroString},${centsString} EUR`;
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import { expect, test } from "vitest";
|
||||
|
||||
import { monthPeriod, quarterPeriod, type BillingPeriod } from "./period";
|
||||
|
||||
test("converts a Berlin month to an exclusive UTC range", () => {
|
||||
expect(monthPeriod(2026, 3)).toEqual({
|
||||
fromUtc: new Date("2026-02-28T23:00:00.000Z"),
|
||||
toUtcExclusive: new Date("2026-03-31T22:00:00.000Z"),
|
||||
});
|
||||
});
|
||||
|
||||
test("converts a Berlin month in full summer (CEST) to an exclusive UTC range", () => {
|
||||
expect(monthPeriod(2026, 8)).toEqual({
|
||||
fromUtc: new Date("2026-07-31T22:00:00.000Z"),
|
||||
toUtcExclusive: new Date("2026-08-31T22:00:00.000Z"),
|
||||
});
|
||||
});
|
||||
|
||||
test("converts a Berlin month in full winter (CET) to an exclusive UTC range", () => {
|
||||
expect(monthPeriod(2026, 1)).toEqual({
|
||||
fromUtc: new Date("2025-12-31T23:00:00.000Z"),
|
||||
toUtcExclusive: new Date("2026-01-31T23:00:00.000Z"),
|
||||
});
|
||||
});
|
||||
|
||||
test("converts a Berlin quarter spanning a DST transition to an exclusive UTC range", () => {
|
||||
// Q1 2026: Jan 1 to Apr 1. Start is CET (+1), end is CEST (+2) after Mar 29 switch.
|
||||
expect(quarterPeriod(2026, 1)).toEqual({
|
||||
fromUtc: new Date("2025-12-31T23:00:00.000Z"),
|
||||
toUtcExclusive: new Date("2026-03-31T22:00:00.000Z"),
|
||||
});
|
||||
});
|
||||
|
||||
test("converts a Berlin quarter fully in summer (CEST) to an exclusive UTC range", () => {
|
||||
// Q3 2026: Jul 1 to Oct 1. Both CEST (+2) until Oct 25 switch. Oct 1 is still CEST.
|
||||
expect(quarterPeriod(2026, 3)).toEqual({
|
||||
fromUtc: new Date("2026-06-30T22:00:00.000Z"),
|
||||
toUtcExclusive: new Date("2026-09-30T22:00:00.000Z"),
|
||||
});
|
||||
});
|
||||
|
||||
test("converts a Berlin quarter spanning the autumn DST transition", () => {
|
||||
// Q4 2026: Oct 1 to Jan 1 2027. Start is CEST (+2), end is CET (+1) after Oct 25 switch.
|
||||
expect(quarterPeriod(2026, 4)).toEqual({
|
||||
fromUtc: new Date("2026-09-30T22:00:00.000Z"),
|
||||
toUtcExclusive: new Date("2026-12-31T23:00:00.000Z"),
|
||||
});
|
||||
});
|
||||
|
||||
test("BillingPeriod has fromUtc and toUtcExclusive Date fields", () => {
|
||||
const period: BillingPeriod = monthPeriod(2026, 8);
|
||||
expect(period.fromUtc).toBeInstanceOf(Date);
|
||||
expect(period.toUtcExclusive).toBeInstanceOf(Date);
|
||||
expect(period.toUtcExclusive.getTime()).toBeGreaterThan(period.fromUtc.getTime());
|
||||
});
|
||||
|
||||
test("rolls over the year boundary for December", () => {
|
||||
expect(monthPeriod(2026, 12)).toEqual({
|
||||
fromUtc: new Date("2026-11-30T23:00:00.000Z"),
|
||||
toUtcExclusive: new Date("2026-12-31T23:00:00.000Z"),
|
||||
});
|
||||
});
|
||||
|
||||
test("supports an explicit timezone override", () => {
|
||||
// America/New_York is UTC-5 in winter (EST), UTC-4 in summer (EDT).
|
||||
// March 2026: Mar 1 00:00 EST = Mar 1 05:00 UTC; Apr 1 00:00 EDT = Apr 1 04:00 UTC.
|
||||
expect(monthPeriod(2026, 3, "America/New_York")).toEqual({
|
||||
fromUtc: new Date("2026-03-01T05:00:00.000Z"),
|
||||
toUtcExclusive: new Date("2026-04-01T04:00:00.000Z"),
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,74 @@
|
||||
import { Temporal } from "@js-temporal/polyfill";
|
||||
|
||||
/**
|
||||
* A half-open UTC date range covering a billing period (month or quarter)
|
||||
* as observed in a specific IANA time zone.
|
||||
*
|
||||
* `fromUtc` is inclusive; `toUtcExclusive` is exclusive. A usage/download
|
||||
* belongs to the period when its timestamp is in `[fromUtc, toUtcExclusive)`.
|
||||
*/
|
||||
export type BillingPeriod = {
|
||||
fromUtc: Date;
|
||||
toUtcExclusive: Date;
|
||||
};
|
||||
|
||||
const MIDNIGHT = Temporal.PlainTime.from("00:00:00");
|
||||
|
||||
/**
|
||||
* Returns the half-open UTC range for the given calendar month in the
|
||||
* given IANA time zone (default `Europe/Berlin`).
|
||||
*
|
||||
* `month` is 1-indexed (1 = January, 12 = December). Boundaries are computed
|
||||
* via Temporal so that DST transitions between the start and end of the
|
||||
* month are handled correctly — the start and end may have different UTC
|
||||
* offsets (e.g. March 2026 starts at CET +1 and ends at CEST +2).
|
||||
*/
|
||||
export function monthPeriod(year: number, month: number, zone = "Europe/Berlin"): BillingPeriod {
|
||||
if (!Number.isInteger(month) || month < 1 || month > 12) {
|
||||
throw new RangeError(`month must be 1–12, received ${month}`);
|
||||
}
|
||||
|
||||
const startPlain = Temporal.PlainDate.from({ year, month, day: 1 });
|
||||
const { year: endYear, month: endMonth } = addMonths(year, month, 1);
|
||||
const endPlain = Temporal.PlainDate.from({ year: endYear, month: endMonth, day: 1 });
|
||||
|
||||
return toBillingPeriod(startPlain, endPlain, zone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the half-open UTC range for the given calendar quarter in the
|
||||
* given IANA time zone (default `Europe/Berlin`).
|
||||
*
|
||||
* `quarter` is 1-indexed (1 = Jan–Mar, 4 = Oct–Dec). As with `monthPeriod`,
|
||||
* boundaries are computed via Temporal to handle DST transitions correctly.
|
||||
*/
|
||||
export function quarterPeriod(year: number, quarter: number, zone = "Europe/Berlin"): BillingPeriod {
|
||||
if (!Number.isInteger(quarter) || quarter < 1 || quarter > 4) {
|
||||
throw new RangeError(`quarter must be 1–4, received ${quarter}`);
|
||||
}
|
||||
|
||||
const startMonth = (quarter - 1) * 3 + 1;
|
||||
const startPlain = Temporal.PlainDate.from({ year, month: startMonth, day: 1 });
|
||||
const { year: endYear, month: endMonth } = addMonths(year, startMonth, 3);
|
||||
const endPlain = Temporal.PlainDate.from({ year: endYear, month: endMonth, day: 1 });
|
||||
|
||||
return toBillingPeriod(startPlain, endPlain, zone);
|
||||
}
|
||||
|
||||
function toBillingPeriod(
|
||||
startPlain: Temporal.PlainDate,
|
||||
endPlain: Temporal.PlainDate,
|
||||
zone: string,
|
||||
): BillingPeriod {
|
||||
const fromInstant = startPlain.toZonedDateTime({ timeZone: zone, plainTime: MIDNIGHT }).toInstant();
|
||||
const toInstant = endPlain.toZonedDateTime({ timeZone: zone, plainTime: MIDNIGHT }).toInstant();
|
||||
return {
|
||||
fromUtc: new Date(fromInstant.epochMilliseconds),
|
||||
toUtcExclusive: new Date(toInstant.epochMilliseconds),
|
||||
};
|
||||
}
|
||||
|
||||
function addMonths(year: number, month: number, count: number): { year: number; month: number } {
|
||||
const total = year * 12 + (month - 1) + count;
|
||||
return { year: Math.floor(total / 12), month: (total % 12) + 1 };
|
||||
}
|
||||
@@ -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([]);
|
||||
});
|
||||
@@ -0,0 +1,206 @@
|
||||
import "server-only";
|
||||
|
||||
import { and, desc, eq, gte, inArray, lt } from "drizzle-orm";
|
||||
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
|
||||
|
||||
import { db } from "@/db/client";
|
||||
import { billingReports, downloadEvents, usages } from "@/db/schema";
|
||||
import type * as schema from "@/db/schema";
|
||||
|
||||
import type { BillingPeriod } from "./period";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Domain types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export type BillingPosition = {
|
||||
id: string;
|
||||
assetId: string;
|
||||
filename: string;
|
||||
albumName: string;
|
||||
purpose: string;
|
||||
reference: string | null;
|
||||
priceCents: number;
|
||||
status: "open" | "billed" | "reopened";
|
||||
selectable: boolean;
|
||||
createdAt: Date;
|
||||
reportNumber: number | null;
|
||||
invoiceNumber: string | null;
|
||||
};
|
||||
|
||||
export type RepeatDownloadPosition = {
|
||||
id: string;
|
||||
assetId: string;
|
||||
usageId: string;
|
||||
authorizedAt: Date;
|
||||
status: string;
|
||||
repeatDownloadNote: string | null;
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Repository interface (raw DB rows)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export type RawBillableUsage = {
|
||||
id: string;
|
||||
immichAssetId: string;
|
||||
filenameSnapshot: string;
|
||||
albumNameSnapshot: string;
|
||||
purpose: string;
|
||||
internalReference: string | null;
|
||||
priceCents: number;
|
||||
billingStatus: "open" | "billed" | "reopened" | "voided";
|
||||
createdAt: Date;
|
||||
billingReportId: string | null;
|
||||
reportNumber: number | null;
|
||||
invoiceNumber: string | null;
|
||||
};
|
||||
|
||||
export type RawRepeatDownload = {
|
||||
id: string;
|
||||
immichAssetId: string;
|
||||
usageId: string;
|
||||
authorizedAt: Date;
|
||||
status: string;
|
||||
repeatDownloadNote: string | null;
|
||||
};
|
||||
|
||||
export type BillingRepository = {
|
||||
findBillableUsages(
|
||||
editorialId: string,
|
||||
fromUtc: Date,
|
||||
toUtcExclusive: Date,
|
||||
): Promise<RawBillableUsage[]>;
|
||||
findRepeatDownloads(
|
||||
editorialId: string,
|
||||
fromUtc: Date,
|
||||
toUtcExclusive: Date,
|
||||
): Promise<RawRepeatDownload[]>;
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Service factory
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export function createBillingQueries({ repository }: { repository: BillingRepository }) {
|
||||
return {
|
||||
async listBillablePositions(
|
||||
editorialId: string,
|
||||
period: BillingPeriod,
|
||||
): Promise<BillingPosition[]> {
|
||||
const rows = await repository.findBillableUsages(
|
||||
editorialId,
|
||||
period.fromUtc,
|
||||
period.toUtcExclusive,
|
||||
);
|
||||
return rows
|
||||
.filter((row) => row.billingStatus !== "voided")
|
||||
.map((row) => toBillingPosition(row));
|
||||
},
|
||||
|
||||
async listNonBillableDownloads(
|
||||
editorialId: string,
|
||||
period: BillingPeriod,
|
||||
): Promise<RepeatDownloadPosition[]> {
|
||||
const rows = await repository.findRepeatDownloads(
|
||||
editorialId,
|
||||
period.fromUtc,
|
||||
period.toUtcExclusive,
|
||||
);
|
||||
return rows.map((row) => ({
|
||||
id: row.id,
|
||||
assetId: row.immichAssetId,
|
||||
usageId: row.usageId,
|
||||
authorizedAt: row.authorizedAt,
|
||||
status: row.status,
|
||||
repeatDownloadNote: row.repeatDownloadNote,
|
||||
}));
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function toBillingPosition(row: RawBillableUsage): BillingPosition {
|
||||
const status = row.billingStatus as BillingPosition["status"];
|
||||
return {
|
||||
id: row.id,
|
||||
assetId: row.immichAssetId,
|
||||
filename: row.filenameSnapshot,
|
||||
albumName: row.albumNameSnapshot,
|
||||
purpose: row.purpose,
|
||||
reference: row.internalReference,
|
||||
priceCents: row.priceCents,
|
||||
status,
|
||||
selectable: status === "open" || status === "reopened",
|
||||
createdAt: row.createdAt,
|
||||
reportNumber: row.reportNumber,
|
||||
invoiceNumber: row.invoiceNumber,
|
||||
};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Drizzle repository implementation
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export function createBillingRepository(
|
||||
database: NodePgDatabase<typeof schema>,
|
||||
): BillingRepository {
|
||||
return {
|
||||
async findBillableUsages(editorialId, fromUtc, toUtcExclusive) {
|
||||
return database
|
||||
.select({
|
||||
id: usages.id,
|
||||
immichAssetId: usages.immichAssetId,
|
||||
filenameSnapshot: usages.filenameSnapshot,
|
||||
albumNameSnapshot: usages.albumNameSnapshot,
|
||||
purpose: usages.purpose,
|
||||
internalReference: usages.internalReference,
|
||||
priceCents: usages.priceCents,
|
||||
billingStatus: usages.billingStatus,
|
||||
createdAt: usages.createdAt,
|
||||
billingReportId: usages.billingReportId,
|
||||
reportNumber: billingReports.reportNumber,
|
||||
invoiceNumber: billingReports.invoiceNumber,
|
||||
})
|
||||
.from(usages)
|
||||
.leftJoin(billingReports, eq(usages.billingReportId, billingReports.id))
|
||||
.where(
|
||||
and(
|
||||
eq(usages.editorialAccountId, editorialId),
|
||||
gte(usages.createdAt, fromUtc),
|
||||
lt(usages.createdAt, toUtcExclusive),
|
||||
inArray(usages.billingStatus, ["open", "billed", "reopened"]),
|
||||
),
|
||||
)
|
||||
.orderBy(desc(usages.createdAt), desc(usages.id));
|
||||
},
|
||||
|
||||
async findRepeatDownloads(editorialId, fromUtc, toUtcExclusive) {
|
||||
return database
|
||||
.select({
|
||||
id: downloadEvents.id,
|
||||
immichAssetId: downloadEvents.immichAssetId,
|
||||
usageId: downloadEvents.usageId,
|
||||
authorizedAt: downloadEvents.authorizedAt,
|
||||
status: downloadEvents.status,
|
||||
repeatDownloadNote: downloadEvents.repeatDownloadNote,
|
||||
})
|
||||
.from(downloadEvents)
|
||||
.where(
|
||||
and(
|
||||
eq(downloadEvents.editorialAccountId, editorialId),
|
||||
gte(downloadEvents.authorizedAt, fromUtc),
|
||||
lt(downloadEvents.authorizedAt, toUtcExclusive),
|
||||
eq(downloadEvents.intentKind, "repeat_download"),
|
||||
inArray(downloadEvents.status, ["completed", "interrupted"]),
|
||||
),
|
||||
)
|
||||
.orderBy(desc(downloadEvents.authorizedAt), desc(downloadEvents.id));
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Default singleton wired to the real database
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export const queries = createBillingQueries({ repository: createBillingRepository(db) });
|
||||
Reference in new issue
Block a user