Skip to content

Commit

Permalink
P4ADEV-1303 modified UserInfo mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
macacia committed Oct 24, 2024
1 parent 02e50ce commit df9b907
Showing 1 changed file with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package it.gov.pagopa.payhub.auth.service.user;

import it.gov.pagopa.payhub.auth.dto.IamUserInfoDTO;
import it.gov.pagopa.payhub.auth.dto.IamUserOrganizationRolesDTO;
import it.gov.pagopa.payhub.auth.exception.custom.UserNotFoundException;
import it.gov.pagopa.payhub.auth.model.Operator;
import it.gov.pagopa.payhub.auth.model.User;
import it.gov.pagopa.payhub.auth.repository.OperatorsRepository;
import it.gov.pagopa.payhub.auth.repository.UsersRepository;
import it.gov.pagopa.payhub.auth.utils.Constants;
import it.gov.pagopa.payhub.model.generated.UserInfo;
import it.gov.pagopa.payhub.model.generated.UserOrganizationRoles;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

@Service
Expand All @@ -27,27 +31,36 @@ public IamUserInfoDTO2UserInfoMapper(UsersRepository usersRepository, OperatorsR

@Override
public UserInfo apply(IamUserInfoDTO iamUserInfoDTO) {
User user = usersRepository.findById(iamUserInfoDTO.getInnerUserId()).orElseThrow(() -> new UserNotFoundException("Cannot found user having inner id:" + iamUserInfoDTO.getInnerUserId()));
List<Operator> userRoles = operatorsRepository.findAllByUserId(iamUserInfoDTO.getInnerUserId());
UserInfo userInfo = UserInfo.builder()
.userId(user.getUserId())
.mappedExternalUserId(user.getMappedExternalUserId())
.fiscalCode(iamUserInfoDTO.getFiscalCode())
.familyName(iamUserInfoDTO.getFamilyName())
.name(iamUserInfoDTO.getName())
.issuer(iamUserInfoDTO.getIssuer())
.organizations(userRoles.stream()
.map(r -> UserOrganizationRoles.builder()
.operatorId(r.getOperatorId())
.organizationIpaCode(r.getOrganizationIpaCode())
.roles(new ArrayList<>(r.getRoles()))
.email(r.getEmail())
.build())
.toList())
.build();

if(iamUserInfoDTO.getOrganizationAccess()!=null){
userInfo.setOrganizationAccess(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode());
if (iamUserInfoDTO.isSystemUser()) {
userInfo.setUserId(iamUserInfoDTO.getUserId());
userInfo.setMappedExternalUserId(iamUserInfoDTO.getFiscalCode());
UserOrganizationRoles userOrgRoles = UserOrganizationRoles.builder()
.organizationIpaCode(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode())
.roles(Collections.singletonList(Constants.ROLE_ADMIN))
.build();
userInfo.setOrganizations(Collections.singletonList(userOrgRoles));
} else {
User user = usersRepository.findById(iamUserInfoDTO.getInnerUserId()).orElseThrow(() -> new UserNotFoundException("Cannot found user having inner id:" + iamUserInfoDTO.getInnerUserId()));
List<Operator> userRoles = operatorsRepository.findAllByUserId(iamUserInfoDTO.getInnerUserId());
userInfo.setUserId(user.getUserId());
userInfo.setMappedExternalUserId(user.getMappedExternalUserId());
userInfo.setOrganizations(userRoles.stream()
.map(r -> UserOrganizationRoles.builder()
.operatorId(r.getOperatorId())
.organizationIpaCode(r.getOrganizationIpaCode())
.roles(new ArrayList<>(r.getRoles()))
.email(r.getEmail())
.build())
.toList());
Optional.ofNullable(iamUserInfoDTO.getOrganizationAccess())
.map(IamUserOrganizationRolesDTO::getOrganizationIpaCode)
.ifPresent(userInfo::setOrganizationAccess);
}
return userInfo;
}
Expand Down

0 comments on commit df9b907

Please sign in to comment.