Subscription
This commit is contained in:
parent
3820c24549
commit
6ca4f04bf2
@ -1,4 +1,4 @@
|
|||||||
// 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, Post } from "@ControllerPattern/index";
|
import { Controller, Post } from "@ControllerPattern/index";
|
||||||
@ -18,15 +18,20 @@ export default class StripeController extends ApiController {
|
|||||||
/**
|
/**
|
||||||
* @description Create a new checkout session
|
* @description Create a new checkout session
|
||||||
*/
|
*/
|
||||||
@Post("/api/v1/admin/stripe")
|
@Post("/api/v1/admin/stripe", [authHandler])
|
||||||
protected async post(req: Request, response: Response) {
|
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
|
//init Subscription resource with request body values
|
||||||
const subscriptionEntity = Subscription.hydrate<Subscription>(req.body);
|
const subscriptionEntity = Subscription.hydrate<Subscription>(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);
|
this.httpCreated(response, stripeSession);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -6,17 +6,17 @@ import AuthService, { IUserJwtPayload } from "@Services/common/AuthService/AuthS
|
|||||||
|
|
||||||
import IdNotService from "@Services/common/IdNotService/IdNotService";
|
import IdNotService from "@Services/common/IdNotService/IdNotService";
|
||||||
import WhitelistService from "@Services/common/WhitelistService/WhitelistService";
|
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 UsersService from "@Services/super-admin/UsersService/UsersService";
|
||||||
import SubscriptionsService from "@Services/admin/SubscriptionsService/SubscriptionsService.ts";
|
import SubscriptionsService from "@Services/admin/SubscriptionsService/SubscriptionsService.ts";
|
||||||
import { ESubscriptionStatus } from "@prisma/client";
|
import { ESubscriptionStatus } from "@prisma/client";
|
||||||
import SeatsService from "@Services/admin/SeatsService/SeatsService";
|
import SeatsService from "@Services/admin/SeatsService/SeatsService";
|
||||||
import { BackendVariables } from "@Common/config/variables/Variables";
|
import { EType } from "le-coffre-resources/dist/Admin/Subscription";
|
||||||
|
|
||||||
@Controller()
|
@Controller()
|
||||||
@Service()
|
@Service()
|
||||||
export default class UserController extends ApiController {
|
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();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,45 +68,41 @@ export default class UserController extends ApiController {
|
|||||||
let isSubscribed = false;
|
let isSubscribed = false;
|
||||||
const subscriptions = await this.subscriptionsService.get({ where: { office_uid: userHydrated.office_membership?.uid } });
|
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;
|
isSubscribed = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(subscriptions[0]?.status === ESubscriptionStatus.INACTIVE) {
|
if(subscriptions[0]?.type === EType.Unlimited) {
|
||||||
isSubscribed = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const hasSeat = await this.subscriptionsService.get({ where: {status: ESubscriptionStatus.ACTIVE, seats: {some : {user_uid : userHydrated.uid }} } });
|
|
||||||
|
|
||||||
if (hasSeat && hasSeat.length > 0) {
|
|
||||||
isSubscribed = true;
|
isSubscribed = true;
|
||||||
}
|
}
|
||||||
else {
|
else{
|
||||||
const nbMaxSeats = subscriptions[0]!.nb_seats;
|
const hasSeat = await this.subscriptionsService.get({ where: {status: ESubscriptionStatus.ACTIVE, seats: {some : {user_uid : userHydrated.uid }} } });
|
||||||
|
|
||||||
const nbCurrentSeats = await this.seatsService.get({ where: { subscription_uid: subscriptions[0]!.uid }});
|
if (hasSeat && hasSeat.length > 0) {
|
||||||
|
isSubscribed = true;
|
||||||
//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{
|
else {
|
||||||
isSubscribed = false;
|
const nbMaxSeats = subscriptions[0]!.nb_seats;
|
||||||
return;
|
|
||||||
|
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");
|
this.httpUnauthorized(response, "User not subscribed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Check if user is whitelisted
|
//Check if user is whitelisted
|
||||||
const isWhitelisted = await this.whitelistService.getByEmail(userHydrated.contact!.email);
|
const isWhitelisted = await this.whitelistService.getByEmail(userHydrated.contact!.email);
|
||||||
|
|
||||||
|
@ -34,6 +34,8 @@ export default class StripeWebhooks extends ApiController {
|
|||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case "checkout.session.completed":
|
case "checkout.session.completed":
|
||||||
if (event.data.object.status !== "complete") break;
|
if (event.data.object.status !== "complete") break;
|
||||||
|
console.log(event.data.object);
|
||||||
|
|
||||||
|
|
||||||
const subscription = JSON.parse(event.data.object.metadata.subscription);
|
const subscription = JSON.parse(event.data.object.metadata.subscription);
|
||||||
subscription.stripe_subscription_id = event.data.object.subscription;
|
subscription.stripe_subscription_id = event.data.object.subscription;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user