allow admin to connect
This commit is contained in:
parent
fc75a78fff
commit
832102d1fd
@ -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();
|
||||
}
|
||||
|
||||
@ -33,14 +39,14 @@ export default class UserController extends ApiController {
|
||||
|
||||
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);
|
||||
|
||||
if(!user) {
|
||||
if (!user) {
|
||||
this.httpUnauthorized(response, "User not found");
|
||||
return;
|
||||
}
|
||||
@ -49,7 +55,7 @@ export default class UserController extends ApiController {
|
||||
|
||||
//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");
|
||||
@ -59,45 +65,48 @@ export default class UserController extends ApiController {
|
||||
//Hydrate user to be able to use his contact
|
||||
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");
|
||||
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) {
|
||||
if (userHydrated.role?.name === "admin") {
|
||||
isSubscribed = true;
|
||||
}
|
||||
else{
|
||||
const hasSeat = await this.subscriptionsService.get({ where: {status: ESubscriptionStatus.ACTIVE, seats: {some : {user_uid : userHydrated.uid }} } });
|
||||
} else {
|
||||
const subscriptions = await this.subscriptionsService.get({ where: { office_uid: userHydrated.office_membership?.uid } });
|
||||
|
||||
if (hasSeat && hasSeat.length > 0) {
|
||||
isSubscribed = true;
|
||||
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 (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 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 {
|
||||
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;
|
||||
}
|
||||
@ -105,7 +114,6 @@ export default class UserController extends ApiController {
|
||||
//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);
|
||||
|
||||
@ -115,7 +123,6 @@ export default class UserController extends ApiController {
|
||||
// return;
|
||||
// }
|
||||
|
||||
|
||||
await this.idNotService.updateOffice(user.office_uid);
|
||||
|
||||
const payload = await this.authService.getUserJwtPayload(user.idNot);
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user