Merge branch 'dev' into staging

This commit is contained in:
Vins 2024-04-16 16:34:36 +02:00
commit 56ef91c4e4
5 changed files with 98 additions and 50 deletions

View File

@ -1,7 +1,7 @@
import authHandler from "@App/middlewares/AuthHandler"; import authHandler from "@App/middlewares/AuthHandler";
// import roleHandler from "@App/middlewares/RolesHandler"; // import roleHandler from "@App/middlewares/RolesHandler";
import ApiController from "@Common/system/controller-pattern/ApiController"; import ApiController from "@Common/system/controller-pattern/ApiController";
import { Controller, Get, Post, Put } from "@ControllerPattern/index"; import { Controller, Get, Post} from "@ControllerPattern/index";
import StripeService from "@Services/common/StripeService/StripeService"; import StripeService from "@Services/common/StripeService/StripeService";
import { validateOrReject } from "class-validator"; import { validateOrReject } from "class-validator";
import { Request, Response } from "express"; import { Request, Response } from "express";
@ -41,31 +41,31 @@ export default class StripeController extends ApiController {
} }
} }
@Put("/api/v1/admin/stripe/:uid") // @Put("/api/v1/admin/stripe/:uid")
protected async createStripeSubscriptionUpdateCheckout(req: Request, response: Response) { // protected async createStripeSubscriptionUpdateCheckout(req: Request, response: Response) {
try { // try {
const uid = req.params["uid"]; // const uid = req.params["uid"];
if (!uid) { // if (!uid) {
this.httpBadRequest(response, "No uid provided"); // this.httpBadRequest(response, "No uid provided");
return; // return;
} // }
const officeId: string = req.body.user.office_Id; // const officeId: string = req.body.user.office_Id;
//add office id to request body // //add office id to request body
req.body.office = { uid: officeId }; // req.body.office = { uid: officeId };
//init Subscription resource with request body values // //init Subscription resource with request body values
const subscriptionEntity = Subscription.hydrate<Subscription>(req.body, { strategy: "excludeAll" }); // const subscriptionEntity = Subscription.hydrate<Subscription>(req.body, { strategy: "excludeAll" });
await validateOrReject(subscriptionEntity, { groups: ["updateSubscription"], forbidUnknownValues: false }); // await validateOrReject(subscriptionEntity, { groups: ["updateSubscription"], forbidUnknownValues: false });
const stripeSession = await this.stripeService.createCheckoutSessionUpdate(uid, subscriptionEntity); // const stripeSession = await this.stripeService.createCheckoutSessionUpdate(uid, subscriptionEntity);
this.httpCreated(response, stripeSession); // this.httpCreated(response, stripeSession);
} catch (error) { // } catch (error) {
this.httpInternalError(response, error); // this.httpInternalError(response, error);
return; // return;
} // }
} // }
@Get("/api/v1/admin/stripe/:uid", [authHandler]) @Get("/api/v1/admin/stripe/:uid", [authHandler])
protected async getClientPortalSession(req: Request, response: Response) { protected async getClientPortalSession(req: Request, response: Response) {

View File

@ -8,7 +8,6 @@ import { Service } from "typedi";
import { Prisma } from "@prisma/client"; import { Prisma } from "@prisma/client";
import SubscriptionsService from "@Services/admin/SubscriptionsService/SubscriptionsService.ts"; import SubscriptionsService from "@Services/admin/SubscriptionsService/SubscriptionsService.ts";
import { Subscription } from "le-coffre-resources/dist/Admin"; import { Subscription } from "le-coffre-resources/dist/Admin";
import { validateOrReject } from "class-validator";
import ObjectHydrate from "@Common/helpers/ObjectHydrate"; import ObjectHydrate from "@Common/helpers/ObjectHydrate";
import roleHandler from "@App/middlewares/RolesHandler"; import roleHandler from "@App/middlewares/RolesHandler";
import authHandler from "@App/middlewares/AuthHandler"; import authHandler from "@App/middlewares/AuthHandler";
@ -81,29 +80,29 @@ export default class SubscriptionsController extends ApiController {
} }
} }
/** // /**
* @description Create a new documentType // * @description Create a new documentType
*/ // */
@Post("/api/v1/admin/subscriptions", [authHandler, roleHandler]) // @Post("/api/v1/admin/subscriptions", [authHandler, roleHandler])
protected async post(req: Request, response: Response) { // protected async post(req: Request, response: Response) {
try { // try {
//init Subscription resource with request body values // //init Subscription resource with request body values
const subscriptionEntity = Subscription.hydrate<Subscription>(req.body); // const subscriptionEntity = Subscription.hydrate<Subscription>(req.body);
//validate subscription // //validate subscription
await validateOrReject(subscriptionEntity, { groups: ["createSubscription"], forbidUnknownValues: false }); // await validateOrReject(subscriptionEntity, { groups: ["createSubscription"], forbidUnknownValues: false });
//call service to get prisma entity // //call service to get prisma entity
const subscriptionEntityCreated = await this.subscriptionsService.create(subscriptionEntity); // const subscriptionEntityCreated = await this.subscriptionsService.create(subscriptionEntity);
//Hydrate ressource with prisma entity // //Hydrate ressource with prisma entity
const subscription = Subscription.hydrate<Subscription>(subscriptionEntityCreated, { // const subscription = Subscription.hydrate<Subscription>(subscriptionEntityCreated, {
strategy: "excludeAll", // strategy: "excludeAll",
}); // });
//success // //success
this.httpCreated(response, subscription); // this.httpCreated(response, subscription);
} catch (error) { // } catch (error) {
this.httpInternalError(response, error); // this.httpInternalError(response, error);
return; // return;
} // }
} // }
/** /**
* @description Update a subscription * @description Update a subscription

View File

@ -148,9 +148,15 @@ export class BackendVariables {
@IsNotEmpty() @IsNotEmpty()
public readonly STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID!: string; public readonly STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID!: string;
@IsNotEmpty()
public readonly STRIPE_STANDARD_ANNUAL_SUBSCRIPTION_PRICE_ID!: string;
@IsNotEmpty() @IsNotEmpty()
public readonly STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID!: string; public readonly STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID!: string;
@IsNotEmpty()
public readonly STRIPE_UNLIMITED_ANNUAL_SUBSCRIPTION_PRICE_ID!: string;
@IsNotEmpty() @IsNotEmpty()
public readonly STRIPE_PAYMENT_SUCCESS_URL!: string; public readonly STRIPE_PAYMENT_SUCCESS_URL!: string;
@ -210,7 +216,9 @@ export class BackendVariables {
this.SCW_BUCKET_NAME = process.env["BUCKET_NAME"]!; this.SCW_BUCKET_NAME = process.env["BUCKET_NAME"]!;
this.STRIPE_SECRET_KEY = process.env["STRIPE_SECRET_KEY"]!; this.STRIPE_SECRET_KEY = process.env["STRIPE_SECRET_KEY"]!;
this.STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID = process.env["STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID"]!; this.STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID = process.env["STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID"]!;
this.STRIPE_STANDARD_ANNUAL_SUBSCRIPTION_PRICE_ID = process.env["STRIPE_STANDARD_ANNUAL_SUBSCRIPTION_PRICE_ID"]!;
this.STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID = process.env["STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID"]!; this.STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID = process.env["STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID"]!;
this.STRIPE_UNLIMITED_ANNUAL_SUBSCRIPTION_PRICE_ID = process.env["STRIPE_UNLIMITED_ANNUAL_SUBSCRIPTION_PRICE_ID"]!;
this.STRIPE_PAYMENT_SUCCESS_URL = process.env["STRIPE_PAYMENT_SUCCESS_URL"]!; this.STRIPE_PAYMENT_SUCCESS_URL = process.env["STRIPE_PAYMENT_SUCCESS_URL"]!;
this.STRIPE_PAYMENT_CANCEL_URL = process.env["STRIPE_PAYMENT_CANCEL_URL"]!; this.STRIPE_PAYMENT_CANCEL_URL = process.env["STRIPE_PAYMENT_CANCEL_URL"]!;
this.IDNOT_PROD_BASE_URL = process.env["IDNOT_PROD_BASE_URL"]!; this.IDNOT_PROD_BASE_URL = process.env["IDNOT_PROD_BASE_URL"]!;

View File

@ -795,6 +795,41 @@ export default async function main() {
updated_at: new Date(), updated_at: new Date(),
namespace: "notary", namespace: "notary",
}, },
{
name: "GET subscriptions",
label: "Récupérer les abonnements",
created_at: new Date(),
updated_at: new Date(),
namespace: "notary",
},
{
name: "POST subscriptions",
label: "Inviter un collaborateur à l'abonnement",
created_at: new Date(),
updated_at: new Date(),
namespace: "notary",
},
{
name: "PUT subscriptions",
label: "Modifier l'abonnement",
created_at: new Date(),
updated_at: new Date(),
namespace: "notary",
},
{
name: "GET stripe",
label: "Gérer l'abonnement de l'office",
created_at: new Date(),
updated_at: new Date(),
namespace: "notary",
},
{
name: "POST stripe",
label: "Payer un abonnement",
created_at: new Date(),
updated_at: new Date(),
namespace: "notary",
},
]; ];
const collaboratorRules = rules.filter((rule) => rule.namespace === "collaborator"); const collaboratorRules = rules.filter((rule) => rule.namespace === "collaborator");

View File

@ -24,7 +24,7 @@ export default class StripeWebhooks extends ApiController {
const event = req.body; const event = req.body;
switch (event.type) { switch (event.type) {
case "invoice.payment_succeeded": case "invoice.payment_succeeded":
if (event.data.object.billing_reason !== "subscription_update") break; if (event.data.object.billing_reason !== "subscription_update") break;
const stripeSubscription = await this.stripeService.getClient().subscriptions.retrieve(event.data.object.subscription); const stripeSubscription = await this.stripeService.getClient().subscriptions.retrieve(event.data.object.subscription);
const existingSubscription = await this.subscriptionsService.get({where : {stripe_subscription_id : stripeSubscription.id}}); const existingSubscription = await this.subscriptionsService.get({where : {stripe_subscription_id : stripeSubscription.id}});
@ -34,11 +34,17 @@ export default class StripeWebhooks extends ApiController {
subscriptionUpdate.start_date = new Date(stripeSubscription.current_period_start * 1000); subscriptionUpdate.start_date = new Date(stripeSubscription.current_period_start * 1000);
subscriptionUpdate.end_date = new Date(stripeSubscription.current_period_end * 1000); subscriptionUpdate.end_date = new Date(stripeSubscription.current_period_end * 1000);
subscriptionUpdate.nb_seats = stripeSubscription.items.data[0]?.quantity; subscriptionUpdate.nb_seats = stripeSubscription.items.data[0]?.quantity;
subscriptionUpdate.type = stripeSubscription.items.data[0]?.price?.id === this.backendVariables.STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID ? "STANDARD" : "UNLIMITED";
const subscriptionEntityUpdate = Subscription.hydrate<Subscription>(subscriptionUpdate); if(stripeSubscription.items.data[0]?.price?.id === this.backendVariables.STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID || stripeSubscription.items.data[0]?.price?.id === this.backendVariables.STRIPE_STANDARD_ANNUAL_SUBSCRIPTION_PRICE_ID){
subscriptionUpdate.type = "STANDARD";
}
else{
subscriptionUpdate.type = "UNLIMITED";
}
await validateOrReject(subscriptionEntityUpdate, { groups: ["updateSubscription"], forbidUnknownValues: false }); const subscriptionEntityUpdate = Subscription.hydrate<Subscription>(subscriptionUpdate);
await validateOrReject(subscriptionEntityUpdate, { groups: ["updateSubscription"], forbidUnknownValues: false });
await this.subscriptionsService.update(existingSubscription[0].uid ,subscriptionEntityUpdate); await this.subscriptionsService.update(existingSubscription[0].uid ,subscriptionEntityUpdate);