allow admin to connect

This commit is contained in:
Vins 2024-04-04 11:41:12 +02:00
parent fc75a78fff
commit 832102d1fd
2 changed files with 60 additions and 49 deletions

View File

@ -15,7 +15,13 @@ 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 userService: UsersService, private subscriptionsService: SubscriptionsService, private seatsService: SeatsService) { constructor(
private authService: AuthService,
private idNotService: IdNotService,
private userService: UsersService,
private subscriptionsService: SubscriptionsService,
private seatsService: SeatsService,
) {
super(); super();
} }
@ -28,100 +34,101 @@ export default class UserController extends ApiController {
protected async getUserInfosFromIdnot(req: Request, response: Response) { protected async getUserInfosFromIdnot(req: Request, response: Response) {
try { try {
const code = req.params["code"]; const code = req.params["code"];
if (!code) throw new Error("code is required"); if (!code) throw new Error("code is required");
const idNotToken = await this.idNotService.getIdNotToken(code); const idNotToken = await this.idNotService.getIdNotToken(code);
if(!idNotToken) { if (!idNotToken) {
this.httpValidationError(response, "IdNot token undefined"); this.httpValidationError(response, "IdNot token undefined");
return; return;
} }
const user = await this.idNotService.getOrCreateUser(idNotToken); const user = await this.idNotService.getOrCreateUser(idNotToken);
if(!user) { if (!user) {
this.httpUnauthorized(response, "User not found"); this.httpUnauthorized(response, "User not found");
return; return;
} }
await this.idNotService.updateUser(user.uid); await this.idNotService.updateUser(user.uid);
//Whitelist feature //Whitelist feature
//Get user with contact //Get user with contact
const prismaUser = await this.userService.getByUid(user.uid, {contact: true }); const prismaUser = await this.userService.getByUid(user.uid, { contact: true, role: true });
if (!prismaUser) { if (!prismaUser) {
this.httpNotFoundRequest(response, "user not found"); this.httpNotFoundRequest(response, "user not found");
return; return;
} }
//Hydrate user to be able to use his contact //Hydrate user to be able to use his contact
const userHydrated = User.hydrate<User>(prismaUser, { strategy: "excludeAll" }); const userHydrated = User.hydrate<User>(prismaUser, { strategy: "excludeAll" });
if(!userHydrated.contact?.email || userHydrated.contact?.email === "") { if (!userHydrated.contact?.email || userHydrated.contact?.email === "") {
this.httpUnauthorized(response, "Email not found"); this.httpUnauthorized(response, "Email not found");
return; return;
} }
let isSubscribed = false;
let isSubscribed = false; if (userHydrated.role?.name === "admin") {
const subscriptions = await this.subscriptionsService.get({ where: { office_uid: userHydrated.office_membership?.uid } });
if(!subscriptions || subscriptions.length === 0 || subscriptions[0]?.status === ESubscriptionStatus.INACTIVE) {
this.httpUnauthorized(response, "User not subscribed");
isSubscribed = false;
return;
}
if(subscriptions[0]?.type === EType.Unlimited) {
isSubscribed = true; isSubscribed = true;
} } else {
else{ const subscriptions = await this.subscriptionsService.get({ where: { office_uid: userHydrated.office_membership?.uid } });
const hasSeat = await this.subscriptionsService.get({ where: {status: ESubscriptionStatus.ACTIVE, seats: {some : {user_uid : userHydrated.uid }} } });
if (!subscriptions || subscriptions.length === 0 || subscriptions[0]?.status === ESubscriptionStatus.INACTIVE) {
if (hasSeat && hasSeat.length > 0) { this.httpUnauthorized(response, "User not subscribed");
isSubscribed = true; isSubscribed = false;
return;
} }
else {
const nbMaxSeats = subscriptions[0]!.nb_seats; if (subscriptions[0]?.type === EType.Unlimited) {
isSubscribed = true;
const nbCurrentSeats = await this.seatsService.get({ where: { subscription_uid: subscriptions[0]!.uid }}); } else {
const hasSeat = await this.subscriptionsService.get({
//if nbMaxSeats < nbCurrentSeats, create a new seat for the user where: { status: ESubscriptionStatus.ACTIVE, seats: { some: { user_uid: userHydrated.uid } } },
if (nbMaxSeats > nbCurrentSeats.length) { });
const seatAdded = await this.seatsService.create(user.uid, subscriptions[0]!.uid);
if (seatAdded) { if (hasSeat && hasSeat.length > 0) {
isSubscribed = true; 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;
}
} }
} }
} }
} }
if(!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);
//When we'll switch to idNotId whitelisting //When we'll switch to idNotId whitelisting
// const isWhitelisted = await this.userWhitelistService.getByIdNotId(user.idNot); // const isWhitelisted = await this.userWhitelistService.getByIdNotId(user.idNot);
//If not whitelisted, return 409 Not whitelisted //If not whitelisted, return 409 Not whitelisted
// if (!isWhitelisted || isWhitelisted.length === 0) { // if (!isWhitelisted || isWhitelisted.length === 0) {
// this.httpNotWhitelisted(response); // this.httpNotWhitelisted(response);
// return; // return;
// } // }
await this.idNotService.updateOffice(user.office_uid); await this.idNotService.updateOffice(user.office_uid);
const payload = await this.authService.getUserJwtPayload(user.idNot); const payload = await this.authService.getUserJwtPayload(user.idNot);
const accessToken = this.authService.generateAccessToken(payload); const accessToken = this.authService.generateAccessToken(payload);
const refreshToken = this.authService.generateRefreshToken(payload); const refreshToken = this.authService.generateRefreshToken(payload);
this.httpSuccess(response, { accessToken, refreshToken }); this.httpSuccess(response, { accessToken, refreshToken });
} catch (error) { } catch (error) {
console.log(error); console.log(error);

View File

@ -147,7 +147,7 @@ export default class IdNotService extends BaseService {
case EIdnotRole.SUPPLEANT: case EIdnotRole.SUPPLEANT:
return (await this.rolesService.get({ where: { name: "notary" } }))[0]!; return (await this.rolesService.get({ where: { name: "notary" } }))[0]!;
case EIdnotRole.ADMINISTRATEUR: case EIdnotRole.ADMINISTRATEUR:
return (await this.rolesService.get({ where: { name: "notary" } }))[0]!; return (await this.rolesService.get({ where: { name: "admin" } }))[0]!;
case EIdnotRole.CURATEUR: case EIdnotRole.CURATEUR:
return (await this.rolesService.get({ where: { name: "notary" } }))[0]!; return (await this.rolesService.get({ where: { name: "notary" } }))[0]!;
default: default:
@ -346,6 +346,10 @@ export default class IdNotService extends BaseService {
// } // }
const role = await this.getRole(userData.typeLien.name); const role = await this.getRole(userData.typeLien.name);
console.log(role);
console.log(userData.typeLien);
const userToAdd = { const userToAdd = {
idNot: decodedToken.sub, idNot: decodedToken.sub,