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
@@ -3,6 +3,9 @@
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { headers } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "@/db/client";
|
||||
import { editorialAccounts } from "@/db/schema";
|
||||
|
||||
import { requireAdmin } from "@/modules/auth/principal";
|
||||
import { editorialService, EditorialServiceError } from "@/modules/editorials/service";
|
||||
@@ -74,6 +77,42 @@ export async function reactivateEditorialAction(editorialId: string): Promise<vo
|
||||
revalidatePath(`/admin/redaktionen/${editorialId}`);
|
||||
}
|
||||
|
||||
export async function updateFlatrateAction(editorialId: string, formData: FormData): Promise<void> {
|
||||
const admin = await requireAdmin(new Headers(await headers()));
|
||||
const safeEditorialId = requiredId(editorialId);
|
||||
|
||||
const flatrateActive = formData.get("flatrateActive") === "on";
|
||||
|
||||
let flatratePriceCents: number | null = null;
|
||||
const rawPrice = formData.get("flatratePriceEuros");
|
||||
if (rawPrice && typeof rawPrice === "string" && rawPrice.trim() !== "") {
|
||||
const price = parsePriceCents(rawPrice);
|
||||
if (price.ok) {
|
||||
flatratePriceCents = price.cents;
|
||||
}
|
||||
}
|
||||
|
||||
let flatrateAssetLimit: number | null = null;
|
||||
const rawLimit = formData.get("flatrateAssetLimit");
|
||||
if (rawLimit && typeof rawLimit === "string" && rawLimit.trim() !== "") {
|
||||
const limit = parseInt(rawLimit, 10);
|
||||
if (Number.isFinite(limit) && limit >= 1) {
|
||||
flatrateAssetLimit = limit;
|
||||
}
|
||||
}
|
||||
|
||||
await db
|
||||
.update(editorialAccounts)
|
||||
.set({
|
||||
flatrateActive,
|
||||
flatratePriceCents,
|
||||
flatrateAssetLimit,
|
||||
})
|
||||
.where(eq(editorialAccounts.id, safeEditorialId));
|
||||
|
||||
revalidatePath(`/admin/redaktionen/${safeEditorialId}`);
|
||||
}
|
||||
|
||||
function parsePriceCents(
|
||||
value: unknown,
|
||||
): { ok: true; cents: number } | { ok: false; message: string } {
|
||||
|
||||
Reference in new issue
Block a user