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
@@ -8,6 +8,11 @@ import {
|
||||
} from "@/modules/billing/reports";
|
||||
import { monthPeriod, quarterPeriod } from "@/modules/billing/period";
|
||||
import { assertSameOrigin, CrossOriginRequestError } from "@/modules/security/origin";
|
||||
import { emailService } from "@/modules/email/service";
|
||||
import { getReportPdf } from "@/modules/billing/reports";
|
||||
import { db } from "@/db/client";
|
||||
import { editorialAccounts } from "@/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
const finalizePayload = z
|
||||
.object({
|
||||
@@ -45,6 +50,35 @@ export function createFinalizeHandler(dependencies: FinalizeHandlerDependencies)
|
||||
adminUserId: principal.userId,
|
||||
});
|
||||
|
||||
// Email dispatch
|
||||
try {
|
||||
const [editorial] = await db
|
||||
.select({ email: editorialAccounts.loginEmail })
|
||||
.from(editorialAccounts)
|
||||
.where(eq(editorialAccounts.id, editorialId))
|
||||
.limit(1);
|
||||
|
||||
if (editorial?.email) {
|
||||
const reportPdf = await getReportPdf(result.reportId);
|
||||
if (reportPdf) {
|
||||
const periodString = month
|
||||
? `${month.toString().padStart(2, "0")}/${year}`
|
||||
: `Q${quarter}/${year}`;
|
||||
|
||||
// Fire and forget (don't await or catch if we want to not block, but awaiting is safer for serverless if not in background)
|
||||
await emailService.sendBillingReport(
|
||||
editorial.email,
|
||||
periodString,
|
||||
reportPdf.bytes,
|
||||
result.reportNumber
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (emailError) {
|
||||
console.error("Failed to send billing email:", emailError);
|
||||
// Do not fail the request if email fails
|
||||
}
|
||||
|
||||
return Response.json(
|
||||
{
|
||||
reportId: result.reportId,
|
||||
|
||||
Reference in new issue
Block a user