-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
antonio.torre
committed
Jul 2, 2024
1 parent
a1ca768
commit ce95b45
Showing
9 changed files
with
286 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/it/gov/pagopa/payhub/auth/service/user/retrieve/OperatorDTOMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package it.gov.pagopa.payhub.auth.service.user.retrieve; | ||
|
||
import it.gov.pagopa.payhub.auth.model.Operator; | ||
import it.gov.pagopa.payhub.auth.model.User; | ||
import it.gov.pagopa.payhub.model.generated.OperatorDTO; | ||
|
||
import java.util.ArrayList; | ||
import java.util.function.BiFunction; | ||
|
||
public class OperatorDTOMapper implements BiFunction<User, Operator, OperatorDTO> { | ||
@Override | ||
public OperatorDTO apply(User user, Operator operator) { | ||
return OperatorDTO.builder() | ||
.userId(user.getUserId()) | ||
.mappedExternalUserId(user.getMappedExternalUserId()) | ||
.userCode(user.getUserCode()) | ||
.operatorId(operator.getOperatorId()) | ||
.roles(new ArrayList<>(operator.getRoles())) | ||
.organizationIpaCode(operator.getOrganizationIpaCode()) | ||
.fiscalCode(user.getFiscalCode()) | ||
.firstName(user.getFirstName()) | ||
.lastName(user.getLastName()) | ||
.email(user.getEmail()) | ||
.build(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...it/gov/pagopa/payhub/auth/service/user/retrieve/OrganizationOperatorRetrieverService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package it.gov.pagopa.payhub.auth.service.user.retrieve; | ||
|
||
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.model.generated.OperatorDTO; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
@Service | ||
@Slf4j | ||
public class OrganizationOperatorRetrieverService { | ||
|
||
private final OperatorsRepository operatorsRepository; | ||
private final UsersRepository usersRepository; | ||
private final OperatorDTOMapper operatorDTOMapper; | ||
|
||
public OrganizationOperatorRetrieverService(OperatorsRepository operatorsRepository, UsersRepository usersRepository, OperatorDTOMapper operatorDTOMapper) { | ||
this.operatorsRepository = operatorsRepository; | ||
this.usersRepository = usersRepository; | ||
this.operatorDTOMapper = operatorDTOMapper; | ||
} | ||
|
||
public List<OperatorDTO> retrieveOrganizationOperators(String organizationIpaCode) { | ||
List<Operator> operators = operatorsRepository.findAllByOrganizationIpaCode(organizationIpaCode); | ||
return operators.stream() | ||
.map(op -> { | ||
Optional<User> user = usersRepository.findById(op.getUserId()); | ||
if(user.isEmpty()){ | ||
log.warn("Found an operator without a user: {}", op); | ||
return null; | ||
} else { | ||
return operatorDTOMapper.apply(user.get(), op); | ||
} | ||
}) | ||
.filter(Objects::nonNull) | ||
.toList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/test/java/it/gov/pagopa/payhub/auth/service/user/retrieve/OperatorDTOMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package it.gov.pagopa.payhub.auth.service.user.retrieve; | ||
|
||
import it.gov.pagopa.payhub.auth.model.Operator; | ||
import it.gov.pagopa.payhub.auth.model.User; | ||
import it.gov.pagopa.payhub.model.generated.OperatorDTO; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
class OperatorDTOMapperTest { | ||
|
||
private final OperatorDTOMapper mapper = new OperatorDTOMapper(); | ||
|
||
@Test | ||
void test(){ | ||
// Given | ||
User user = User.builder() | ||
.userId("USERID") | ||
.iamIssuer("IAMISSUER") | ||
.mappedExternalUserId("MAPPEDEXTERNALUSERID") | ||
.userCode("USERCODE") | ||
.fiscalCode("FISCALCODE") | ||
.firstName("FIRSTNAME") | ||
.lastName("LASTNAME") | ||
.email("EMAIL") | ||
.build(); | ||
|
||
Operator operator = Operator.builder() | ||
.operatorId("OPERATORID") | ||
.userId("USERID") | ||
.roles(Set.of("ROLES")) | ||
.organizationIpaCode("ORGANIZATIONIPACODE") | ||
.build(); | ||
|
||
// When | ||
OperatorDTO result = mapper.apply(user, operator); | ||
|
||
// Then | ||
Assertions.assertEquals( | ||
OperatorDTO.builder() | ||
.userId("USERID") | ||
.mappedExternalUserId("MAPPEDEXTERNALUSERID") | ||
.userCode("USERCODE") | ||
.operatorId("OPERATORID") | ||
.roles(List.of("ROLES")) | ||
.organizationIpaCode("ORGANIZATIONIPACODE") | ||
.fiscalCode("FISCALCODE") | ||
.firstName("FIRSTNAME") | ||
.lastName("LASTNAME") | ||
.email("EMAIL") | ||
.build(), | ||
result | ||
); | ||
} | ||
} |
Oops, something went wrong.