Subscription
This commit is contained in:
parent
86cccab8da
commit
a99224c319
@ -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,11 +18,16 @@ 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 {
|
||||
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<Subscription>(req.body);
|
||||
const subscriptionEntity = Subscription.hydrate<Subscription>(req.body, { strategy: "excludeAll" });
|
||||
|
||||
await validateOrReject(subscriptionEntity, { groups: ["createSubscription"], forbidUnknownValues: false });
|
||||
|
||||
|
@ -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;
|
||||
else{
|
||||
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 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 (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);
|
||||
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user