This commit is contained in:
Alex Holliday committed 2025-12-01 00:35:53 +00:00
1 parent 6bca0ed00b
commit 7b207f2ccb
15 files changed
+204 -15

No files matched your search

@@ -0,0 +1,42 @@
import type { Request, Response, NextFunction } from "express";
import BillingService, {
type IBillingService,
} from "@/services/system/BillingService.js";
export interface IBillingController {
listPlans: (req: Request, res: Response, next: NextFunction) => Promise<void>;
subscribePlan: (
req: Request,
res: Response,
next: NextFunction
) => Promise<void>;
}
class BillingController implements IBillingController {
private billingService: IBillingService;
constructor(billingService: IBillingService) {
this.billingService = billingService;
}
listPlans = async (_req: Request, res: Response, next: NextFunction) => {
try {
const plans = await this.billingService.listPlans();
res.json({ message: "OK", data: plans });
} catch (error) {
next(error);
}
};
subscribePlan = async (req: Request, res: Response, next: NextFunction) => {
try {
res.json({
message: "OK",
data: { redirectUrl: "https://www.google.com" },
});
} catch (error) {
next(error);
}
};
}
export default BillingController;
+1
View File
@@ -14,4 +14,5 @@ export { default as RecoveryController } from "./RecoveryController.js";
export { default as ProfileController } from "./ProfileController.js";
export { default as IncidentsController } from "./IncidentsController.js";
export { default as SettingsController } from "./SettingsController.js";
export { default as BillingController } from "./BillingController.js";
export { default as StripeController } from "./StripeController.js";
+4
View File
@@ -16,6 +16,7 @@ import {
IncidentsController,
SettingsController,
StripeController,
BillingController,
} from "@/controllers/index.js";
export const initControllers = (services: any) => {
@@ -76,6 +77,9 @@ export const initControllers = (services: any) => {
);
controllers.stripeController = new StripeController();
controllers.billingController = new BillingController(
services.billingService
);
return controllers;
};
+3 -2
View File
@@ -17,9 +17,9 @@ import {
IncidentsRoutes,
SettingsRoutes,
MeRoutes,
BillingRoutes,
} from "@/routes/index.js";
import StripeRoutes from "@/routes/stripe.js";
import StripeController from "@/controllers/StripeController.js";
import { errorHandler } from "@/middleware/ErrorHandler.js";
export const initRoutes = (controllers: any, app: Express) => {
const authRoutes = new AuthRoutes(controllers.authController);
@@ -51,6 +51,7 @@ export const initRoutes = (controllers: any, app: Express) => {
const settingsRoutes = new SettingsRoutes(controllers.settingsController);
const meRoutes = new MeRoutes();
const stripeRoutes = new StripeRoutes(controllers.stripeController);
const billingRoutes = new BillingRoutes(controllers.billingController);
app.use("/api/v1/auth", authRoutes.getRouter());
app.use("/api/v1/invite", inviteRoutes.getRouter());
@@ -72,7 +73,7 @@ export const initRoutes = (controllers: any, app: Express) => {
app.use("/api/v1/incidents", incidentsRoutes.getRouter());
app.use("/api/v1/settings", settingsRoutes.getRouter());
app.use("/api/v1/me", meRoutes.getRouter());
// Stripe webhook uses raw body; mounted separately in index.ts
app.use("/api/v1/stripe", stripeRoutes.getRouter());
app.use("/api/v1/billing", billingRoutes.getRouter());
app.use(errorHandler);
};
+3
View File
@@ -28,6 +28,7 @@ import {
RecoveryService,
IncidentService,
SettingsService,
BillingService,
} from "@/services/index.js";
export const initServices = async () => {
@@ -43,6 +44,7 @@ export const initServices = async () => {
const networkService = new NetworkService(got);
const statusService = new StatusService();
const settingsService = new SettingsService();
const billingService = new BillingService();
const emailService = new EmailService(userService, settingsService);
settingsService.setEmailService(emailService);
const notificationService = new NotificationService(
@@ -96,6 +98,7 @@ export const initServices = async () => {
recoveryService,
incidentService,
settingsService,
billingService,
};
Object.values(services).forEach((service) => {
+24
View File
@@ -0,0 +1,24 @@
import { Router } from "express";
import BillingController from "@/controllers/BillingController.js";
import { verifyToken } from "@/middleware/VerifyToken.js";
class BillingRoutes {
private router;
private controller;
constructor(controller: BillingController) {
this.router = Router();
this.controller = controller;
this.initRoutes();
}
initRoutes = () => {
this.router.get("/plans", verifyToken, this.controller.listPlans);
this.router.post("/subscribe", verifyToken, this.controller.subscribePlan);
};
getRouter() {
return this.router;
}
}
export default BillingRoutes;
+1
View File
@@ -15,3 +15,4 @@ export { default as ProfileRoutes } from "./profile.js";
export { default as IncidentsRoutes } from "./incidents.js";
export { default as SettingsRoutes } from "./settings.js";
export { default as MeRoutes } from "./me.js";
export { default as BillingRoutes } from "./billing.js";
+1
View File
@@ -23,5 +23,6 @@ export { default as JobGenerator } from "@/services/infrastructure/JobGenerator.
export { default as ServiceRegistry } from "@/services/system/ServiceRegistry.js";
export { default as SettingsService } from "@/services/system/SettingsService.js";
export { default as BillingService } from "@/services/system/BillingService.js";
export * from "@/services/infrastructure/NotificationServices/index.js";
@@ -0,0 +1,20 @@
import { Plans, type Entitlements, type PlanKey } from "@/types/entitlements.js";
export interface IBillingService {
listPlans(): Promise<Entitlements[]>;
}
const SERVICE_NAME = "BillingService";
class BillingService implements IBillingService {
public SERVICE_NAME: string;
constructor() {
this.SERVICE_NAME = SERVICE_NAME;
}
async listPlans(): Promise<Entitlements[]> {
const order: PlanKey[] = ["free", "pro", "business", "enterprise"];
return order.map((key) => Plans[key]);
}
}
export default BillingService;