diff --git a/src/hub-users/services/hub-users.service.ts b/src/hub-users/services/hub-users.service.ts index 577a47d..1c9d92f 100644 --- a/src/hub-users/services/hub-users.service.ts +++ b/src/hub-users/services/hub-users.service.ts @@ -154,6 +154,23 @@ export class HubUsersService { } } + /** + * Vérifie si un utilisateur est Hub Admin ou Support + */ + async isUserMerchantPartner(userId: string): Promise { + try { + const userRoles = await this.keycloakApi.getUserClientRoles(userId); + const hubMerchantPartnerRoles = [UserRole.DCB_PARTNER]; + + return userRoles.some(role => + hubMerchantPartnerRoles.includes(role.name as UserRole) + ); + } catch (error) { + this.logger.error(`Error checking Merchant Partner status for user ${userId}:`, error); + return false; + } + } + /** * Récupère les utilisateurs marchands selon les permissions de l'utilisateur * - Hub Admin/Support: tous les utilisateurs marchands de tous les merchants @@ -170,7 +187,7 @@ export class HubUsersService { } // Pour les autres utilisateurs (DCB_PARTNER, DCB_PARTNER_ADMIN, etc.) - return await this.getMerchantUsersForRegularUser(userId); + return await this.getUsersForMerchants(userId); } catch (error) { this.logger.error(`Error in getMyMerchantUsers for user ${userId}:`, error); @@ -221,21 +238,29 @@ export class HubUsersService { /** * Récupère les utilisateurs marchands pour les utilisateurs réguliers (non Hub Admin/Support) */ - private async getMerchantUsersForRegularUser(userId: string): Promise { + private async getUsersForMerchants(userId: string): Promise { // Récupérer le merchantPartnerId de l'utilisateur - const userMerchantId = await this.getUserMerchantPartnerId(userId); - + let userMerchantId = await this.getUserMerchantPartnerId(userId); + + // Vérifier si l'utilisateur est un admin ou support Hub + const isUserMerchantPartner = await this.isUserMerchantPartner(userId); + + if(isUserMerchantPartner){ + userMerchantId = userId; + } + if (!userMerchantId) { throw new BadRequestException('Current user is not associated with a merchant partner'); } this.logger.log(`User ${userId} accessing merchant users for partner ${userMerchantId}`); - + // Utiliser la méthode existante pour récupérer les utilisateurs du merchant spécifique const users = await this.getMerchantUsersByPartner(userMerchantId, userId); this.logger.log(`User ${userId} retrieved ${users.length} merchant users for partner ${userMerchantId}`); return users; + } async getMerchantUsersByPartner(merchantPartnerId: string, requesterId: string): Promise {