diff --git a/src/app/api/admin/StripeController.ts b/src/app/api/admin/StripeController.ts index ba8a87b2..ae4c211d 100644 --- a/src/app/api/admin/StripeController.ts +++ b/src/app/api/admin/StripeController.ts @@ -1,4 +1,4 @@ -// import authHandler from "@App/middlewares/AuthHandler"; +import authHandler from "@App/middlewares/AuthHandler"; // import roleHandler from "@App/middlewares/RolesHandler"; import ApiController from "@Common/system/controller-pattern/ApiController"; import { Controller, Post } from "@ControllerPattern/index"; @@ -18,15 +18,20 @@ export default class StripeController extends ApiController { /** * @description Create a new checkout session */ - @Post("/api/v1/admin/stripe") + @Post("/api/v1/admin/stripe", [authHandler]) protected async post(req: Request, response: Response) { - try { + try { + const officeId: string = req.body.user.office_Id; + + //add office id to request body + req.body.office = {uid: officeId}; + //init Subscription resource with request body values - const subscriptionEntity = Subscription.hydrate(req.body); + const subscriptionEntity = Subscription.hydrate(req.body, { strategy: "excludeAll" }); - await validateOrReject(subscriptionEntity, { groups: ["createSubscription"], forbidUnknownValues: false }); + await validateOrReject(subscriptionEntity, { groups: ["createSubscription"], forbidUnknownValues: false }); - const stripeSession = await this.stripeService.createCheckoutSession(subscriptionEntity); + const stripeSession = await this.stripeService.createCheckoutSession(subscriptionEntity); this.httpCreated(response, stripeSession); } catch (error) { diff --git a/src/app/api/idnot/UserController.ts b/src/app/api/idnot/UserController.ts index b2063e72..fcf4b7b3 100644 --- a/src/app/api/idnot/UserController.ts +++ b/src/app/api/idnot/UserController.ts @@ -6,17 +6,17 @@ import AuthService, { IUserJwtPayload } from "@Services/common/AuthService/AuthS import IdNotService from "@Services/common/IdNotService/IdNotService"; import WhitelistService from "@Services/common/WhitelistService/WhitelistService"; -import User from "le-coffre-resources/dist/SuperAdmin"; +import User from "le-coffre-resources/dist/Admin"; import UsersService from "@Services/super-admin/UsersService/UsersService"; import SubscriptionsService from "@Services/admin/SubscriptionsService/SubscriptionsService.ts"; import { ESubscriptionStatus } from "@prisma/client"; import SeatsService from "@Services/admin/SeatsService/SeatsService"; -import { BackendVariables } from "@Common/config/variables/Variables"; +import { EType } from "le-coffre-resources/dist/Admin/Subscription"; @Controller() @Service() export default class UserController extends ApiController { - constructor(private authService: AuthService, private idNotService: IdNotService, private whitelistService: WhitelistService, private userService: UsersService, private subscriptionsService: SubscriptionsService, private seatsService: SeatsService, private backendVariables: BackendVariables) { + constructor(private authService: AuthService, private idNotService: IdNotService, private whitelistService: WhitelistService, private userService: UsersService, private subscriptionsService: SubscriptionsService, private seatsService: SeatsService) { super(); } @@ -68,45 +68,41 @@ export default class UserController extends ApiController { let isSubscribed = false; const subscriptions = await this.subscriptionsService.get({ where: { office_uid: userHydrated.office_membership?.uid } }); - if(!subscriptions || subscriptions.length === 0) { + if(!subscriptions || subscriptions.length === 0 || subscriptions[0]?.status === ESubscriptionStatus.INACTIVE) { + this.httpUnauthorized(response, "User not subscribed"); isSubscribed = false; return; } - if(subscriptions[0]?.status === ESubscriptionStatus.INACTIVE) { - isSubscribed = false; - return; - } - - const hasSeat = await this.subscriptionsService.get({ where: {status: ESubscriptionStatus.ACTIVE, seats: {some : {user_uid : userHydrated.uid }} } }); - - if (hasSeat && hasSeat.length > 0) { + if(subscriptions[0]?.type === EType.Unlimited) { isSubscribed = true; } - else { - const nbMaxSeats = subscriptions[0]!.nb_seats; - - const nbCurrentSeats = await this.seatsService.get({ where: { subscription_uid: subscriptions[0]!.uid }}); - - //if nbMaxSeats < nbCurrentSeats, create a new seat for the user - if (nbMaxSeats > nbCurrentSeats.length) { - const seatAdded = await this.seatsService.create(user.uid, subscriptions[0]!.uid); - if (seatAdded) { - isSubscribed = true; - } + else{ + const hasSeat = await this.subscriptionsService.get({ where: {status: ESubscriptionStatus.ACTIVE, seats: {some : {user_uid : userHydrated.uid }} } }); + + if (hasSeat && hasSeat.length > 0) { + isSubscribed = true; } - else{ - isSubscribed = false; - return; + else { + const nbMaxSeats = subscriptions[0]!.nb_seats; + + const nbCurrentSeats = await this.seatsService.get({ where: { subscription_uid: subscriptions[0]!.uid }}); + + //if nbMaxSeats < nbCurrentSeats, create a new seat for the user + if (nbMaxSeats > nbCurrentSeats.length) { + const seatAdded = await this.seatsService.create(user.uid, subscriptions[0]!.uid); + if (seatAdded) { + isSubscribed = true; + } + } } } - if(this.backendVariables.ENV !== 'dev' && !isSubscribed) { + if(!isSubscribed) { this.httpUnauthorized(response, "User not subscribed"); return; } - //Check if user is whitelisted const isWhitelisted = await this.whitelistService.getByEmail(userHydrated.contact!.email); diff --git a/src/common/webhooks/stripeWebhooks.ts b/src/common/webhooks/stripeWebhooks.ts index 9463eadc..03ba1363 100644 --- a/src/common/webhooks/stripeWebhooks.ts +++ b/src/common/webhooks/stripeWebhooks.ts @@ -34,6 +34,8 @@ export default class StripeWebhooks extends ApiController { switch (event.type) { case "checkout.session.completed": if (event.data.object.status !== "complete") break; + console.log(event.data.object); + const subscription = JSON.parse(event.data.object.metadata.subscription); subscription.stripe_subscription_id = event.data.object.subscription;