feat: Add flatrate billing model for editorial accounts
This commit is contained in:
1 parent
699580a6a0
commit
0b1ffdfdfc
39 files changed
+4900
-57
No files matched your search
@@ -135,6 +135,8 @@ export function createUsageServiceRepository(
|
||||
id: editorialAccounts.id,
|
||||
status: editorialAccounts.status,
|
||||
currentPriceCents: editorialAccounts.currentPriceCents,
|
||||
flatrateActive: editorialAccounts.flatrateActive,
|
||||
flatrateAssetLimit: editorialAccounts.flatrateAssetLimit,
|
||||
})
|
||||
.from(editorialAccounts)
|
||||
.where(eq(editorialAccounts.id, input.editorialId))
|
||||
@@ -164,6 +166,39 @@ export function createUsageServiceRepository(
|
||||
.limit(1);
|
||||
if (!grant) throw new UsageAccountingError("ALBUM_NOT_GRANTED");
|
||||
|
||||
let priceCents = editorial.currentPriceCents!;
|
||||
|
||||
if (editorial.flatrateActive) {
|
||||
if (editorial.flatrateAssetLimit === null) {
|
||||
priceCents = 0; // Unlimited flatrate
|
||||
} else {
|
||||
// Limited flatrate - check how many usages this month
|
||||
const now = new Date();
|
||||
const startOfMonth = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1));
|
||||
const endOfMonth = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth() + 1, 0, 23, 59, 59, 999));
|
||||
|
||||
const [{ count }] = await transaction
|
||||
.select({ count: sql<number>`cast(count(*) as integer)` })
|
||||
.from(usages)
|
||||
.where(
|
||||
and(
|
||||
eq(usages.editorialAccountId, input.editorialId),
|
||||
sql`${usages.createdAt} >= ${startOfMonth}`,
|
||||
sql`${usages.createdAt} <= ${endOfMonth}`,
|
||||
inArray(usages.billingStatus, ["open", "billed", "reopened"]),
|
||||
// For flatrate, we only count usages that were originally free via the flatrate
|
||||
// so we compare against priceCents = 0. Wait, any usage counts towards the limit?
|
||||
// Yes, any billable usage this month counts towards the limit.
|
||||
eq(usages.priceCents, 0)
|
||||
)
|
||||
);
|
||||
|
||||
if (count < editorial.flatrateAssetLimit) {
|
||||
priceCents = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This is the last cross-system authorization snapshot available to the
|
||||
// portal. Immich may still change after it returns, but no portal work is
|
||||
// allowed between this refresh and the accounting inserts.
|
||||
@@ -180,7 +215,7 @@ export function createUsageServiceRepository(
|
||||
albumNameSnapshot: currentAsset.albumName,
|
||||
purpose: input.purpose,
|
||||
internalReference: input.reference,
|
||||
priceCents: editorial.currentPriceCents!,
|
||||
priceCents: priceCents,
|
||||
billingStatus: "open",
|
||||
createdAt,
|
||||
})
|
||||
@@ -214,7 +249,7 @@ export function createUsageServiceRepository(
|
||||
editorialId: input.editorialId,
|
||||
assetId: input.assetId,
|
||||
albumId: input.albumId,
|
||||
priceCents: editorial.currentPriceCents,
|
||||
priceCents: priceCents,
|
||||
downloadId: event.id,
|
||||
},
|
||||
occurredAt: createdAt,
|
||||
@@ -224,7 +259,7 @@ export function createUsageServiceRepository(
|
||||
id: event.id,
|
||||
usageId: usage.id,
|
||||
assetId: input.assetId,
|
||||
priceCents: editorial.currentPriceCents!,
|
||||
priceCents: priceCents,
|
||||
billable: true,
|
||||
status: "authorized" as const,
|
||||
authorizedAt: createdAt,
|
||||
|
||||
Reference in new issue
Block a user