Merge branch 'dev' into staging

This commit is contained in:
Vins 2024-04-04 11:41:29 +02:00
commit 568257f6b9
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()
@Service()
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();
}
@ -28,100 +34,101 @@ export default class UserController extends ApiController {
protected async getUserInfosFromIdnot(req: Request, response: Response) {
try {
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);
if(!idNotToken) {
if (!idNotToken) {
this.httpValidationError(response, "IdNot token undefined");
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");
return;
}
await this.idNotService.updateUser(user.uid);
await this.idNotService.updateUser(user.uid);
//Whitelist feature
//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) {
this.httpNotFoundRequest(response, "user not found");
return;
}
}
//Hydrate user to be able to use his contact
const userHydrated = User.hydrate<User>(prismaUser, { strategy: "excludeAll" });
if(!userHydrated.contact?.email || userHydrated.contact?.email === "") {
const userHydrated = User.hydrate<User>(prismaUser, { strategy: "excludeAll" });
if (!userHydrated.contact?.email || userHydrated.contact?.email === "") {
this.httpUnauthorized(response, "Email not found");
return;
}
let isSubscribed = false;
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) {
let isSubscribed = false;
if (userHydrated.role?.name === "admin") {
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 {
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;
}
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 (subscriptions[0]?.type === EType.Unlimited) {
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 {
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");
return;
}
//Check if user is whitelisted
// const isWhitelisted = await this.whitelistService.getByEmail(userHydrated.contact!.email);
//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 (!isWhitelisted || isWhitelisted.length === 0) {
// if (!isWhitelisted || isWhitelisted.length === 0) {
// this.httpNotWhitelisted(response);
// return;
// }
await this.idNotService.updateOffice(user.office_uid);
const payload = await this.authService.getUserJwtPayload(user.idNot);
const accessToken = this.authService.generateAccessToken(payload);
const refreshToken = this.authService.generateRefreshToken(payload);
this.httpSuccess(response, { accessToken, refreshToken });
} catch (error) {
console.log(error);

View File

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