Merge branch 'staging' into preprod
This commit is contained in:
commit
3da5b78b53
@ -2,7 +2,7 @@ import { Response, Request } from "express";
|
||||
import { Controller, Post } from "@ControllerPattern/index";
|
||||
import ApiController from "@Common/system/controller-pattern/ApiController";
|
||||
import { Service } from "typedi";
|
||||
import AuthService, { IUserJwtPayload } from "@Services/common/AuthService/AuthService";
|
||||
import AuthService, { IUserJwtPayload, PROVIDER_OPENID } from "@Services/common/AuthService/AuthService";
|
||||
|
||||
import IdNotService from "@Services/common/IdNotService/IdNotService";
|
||||
import User, { RulesGroup } from "le-coffre-resources/dist/Admin";
|
||||
@ -22,7 +22,7 @@ export default class UserController extends ApiController {
|
||||
private userService: UsersService,
|
||||
private subscriptionsService: SubscriptionsService,
|
||||
private seatsService: SeatsService,
|
||||
private rulesGroupsService: RulesGroupsService
|
||||
private rulesGroupsService: RulesGroupsService,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
@ -47,8 +47,6 @@ export default class UserController extends ApiController {
|
||||
}
|
||||
|
||||
const user = await this.idNotService.getOrCreateUser(idNotToken);
|
||||
console.log(user);
|
||||
|
||||
|
||||
if (!user) {
|
||||
this.httpUnauthorized(response, "User not found");
|
||||
@ -59,19 +57,14 @@ export default class UserController extends ApiController {
|
||||
|
||||
//Whitelist feature
|
||||
//Get user with contact
|
||||
const prismaUser = await this.userService.getByUid(user.uid, { contact: true, role: true });
|
||||
console.log(prismaUser);
|
||||
|
||||
const prismaUser = await this.userService.getByUid(user.uid, { contact: true, role: true, office_membership: true});
|
||||
|
||||
if (!prismaUser) {
|
||||
this.httpNotFoundRequest(response, "user not found");
|
||||
return;
|
||||
}
|
||||
|
||||
//Hydrate user to be able to use his contact
|
||||
const userHydrated = User.hydrate<User>(prismaUser, { strategy: "excludeAll" });
|
||||
console.log(userHydrated);
|
||||
|
||||
|
||||
if (!userHydrated.contact?.email || userHydrated.contact?.email === "") {
|
||||
this.httpUnauthorized(response, "Email not found");
|
||||
@ -80,20 +73,13 @@ export default class UserController extends ApiController {
|
||||
let isSubscribed = false;
|
||||
|
||||
const subscriptions = await this.subscriptionsService.get({ where: { office_uid: userHydrated.office_membership?.uid } });
|
||||
console.log(subscriptions);
|
||||
|
||||
if (!subscriptions || subscriptions.length === 0 || subscriptions[0]?.status === ESubscriptionStatus.INACTIVE) {
|
||||
console.log("no subscription");
|
||||
|
||||
isSubscribed = false;
|
||||
}
|
||||
else if (subscriptions[0]?.type === EType.Unlimited) {
|
||||
console.log("unlimited subscription");
|
||||
|
||||
isSubscribed = true;
|
||||
} else {
|
||||
console.log("Seats");
|
||||
|
||||
const hasSeat = await this.subscriptionsService.get({
|
||||
where: { status: ESubscriptionStatus.ACTIVE, seats: { some: { user_uid: userHydrated.uid } } },
|
||||
});
|
||||
@ -132,22 +118,17 @@ export default class UserController extends ApiController {
|
||||
const payload = await this.authService.getUserJwtPayload(user.idNot);
|
||||
if(!payload) return;
|
||||
|
||||
console.log(isSubscribed, userHydrated.role?.name);
|
||||
|
||||
if(!isSubscribed && userHydrated.role?.name === "admin" || userHydrated.role?.name === "super-admin"){
|
||||
const manageSubscriptionRulesEntity = await this.rulesGroupsService.get({ where: { uid: "94343601-04c8-44ef-afb9-3047597528a9" }, include: { rules: true } });
|
||||
console.log(manageSubscriptionRulesEntity);
|
||||
|
||||
const manageSubscriptionRules = RulesGroup.hydrateArray<RulesGroup>(manageSubscriptionRulesEntity, { strategy: "excludeAll" });
|
||||
if(!manageSubscriptionRules[0]) return;
|
||||
|
||||
payload.rules = manageSubscriptionRules[0].rules!.map((rule) => rule.name) || [];
|
||||
console.log(payload);
|
||||
|
||||
isSubscribed = true;
|
||||
}
|
||||
|
||||
|
||||
if (!isSubscribed) {
|
||||
this.httpUnauthorized(response, "User not subscribed");
|
||||
return;
|
||||
@ -176,21 +157,24 @@ export default class UserController extends ApiController {
|
||||
}
|
||||
|
||||
let accessToken;
|
||||
this.authService.verifyRefreshToken(token, (err, userPayload) => {
|
||||
this.authService.verifyRefreshToken(token, async (err, userPayload) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
this.httpUnauthorized(response);
|
||||
return;
|
||||
}
|
||||
|
||||
const user = userPayload as IUserJwtPayload;
|
||||
const openId = (userPayload as IUserJwtPayload).openId.userId;
|
||||
if (!openId) return;
|
||||
const newUserPayload = await this.authService.getUserJwtPayload(openId.toString(), PROVIDER_OPENID.idNot);
|
||||
const user = newUserPayload as IUserJwtPayload;
|
||||
delete user.iat;
|
||||
delete user.exp;
|
||||
accessToken = this.authService.generateAccessToken(user);
|
||||
this.httpSuccess(response, { accessToken });
|
||||
});
|
||||
|
||||
//success
|
||||
this.httpSuccess(response, { accessToken });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
this.httpInternalError(response);
|
||||
|
@ -8,7 +8,7 @@ import { ECustomerStatus } from "@prisma/client";
|
||||
import { Customer } from "le-coffre-resources/dist/Notary";
|
||||
import bcrypt from "bcrypt";
|
||||
|
||||
enum PROVIDER_OPENID {
|
||||
export enum PROVIDER_OPENID {
|
||||
idNot = "idNot",
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user