import authHandler from "@App/middlewares/AuthHandler"; import ruleHandler from "@App/middlewares/RulesHandler"; // import roleHandler from "@App/middlewares/RolesHandler"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Controller, Get, Post} from "@ControllerPattern/index"; import StripeService from "@Services/common/StripeService/StripeService"; import { validateOrReject } from "class-validator"; import { Request, Response } from "express"; import { Subscription } from "le-coffre-resources/dist/Admin"; import { EPaymentFrequency } from "le-coffre-resources/dist/Admin/Subscription"; import { Service } from "typedi"; @Controller() @Service() export default class StripeController extends ApiController { constructor(private stripeService: StripeService) { super(); } /** * @description Create a new checkout session */ @Post("/api/v1/admin/stripe", [authHandler, ruleHandler]) protected async createStripeSubscriptionCheckout(req: Request, response: Response) { try { const officeId: string = req.body.user.office_Id; //add office id to request body req.body.office = { uid: officeId }; const frequency : EPaymentFrequency = req.body.frequency; if(!frequency || !Object.values(EPaymentFrequency).includes(frequency)) { this.httpBadRequest(response, "Invalid frequency"); return; } //init Subscription resource with request body values const subscriptionEntity = Subscription.hydrate(req.body, { strategy: "excludeAll" }); await validateOrReject(subscriptionEntity, { groups: ["createSubscription"], forbidUnknownValues: false }); const stripeSession = await this.stripeService.createCheckoutSession(subscriptionEntity, frequency); this.httpCreated(response, stripeSession); } catch (error) { this.httpInternalError(response, error); return; } } @Get("/api/v1/admin/stripe/:uid") protected async getStripeSubscriptionByUid(req: Request, response: Response) { try { const uid = req.params["uid"]; if (!uid) { this.httpBadRequest(response, "No uid provided"); return; } const stripe_subscription = await this.stripeService.getStripeSubscriptionByUid(uid); this.httpSuccess(response, stripe_subscription); } catch (error) { this.httpInternalError(response, error); return; } } @Get("/api/v1/admin/stripe/:uid/client-portal", [authHandler, ruleHandler]) protected async getClientPortalSession(req: Request, response: Response) { try { const uid = req.params["uid"]; if (!uid) { this.httpBadRequest(response, "No uid provided"); return; } const client_portal = await this.stripeService.createClientPortalSession(uid); this.httpSuccess(response, client_portal); } catch (error) { this.httpInternalError(response, error); return; } } @Get("/api/v1/admin/stripe/:uid/customer") protected async getCustomerBySubscription(req: Request, response: Response) { try { const uid = req.params["uid"]; if (!uid) { this.httpBadRequest(response, "No uid provided"); return; } const customer = await this.stripeService.getCustomerBySubscription(uid); this.httpSuccess(response, customer); } catch (error) { this.httpInternalError(response, error); return; } } }