Skip to content

Commit

Permalink
Repository fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitolo-Andrea committed Nov 7, 2024
1 parent 1aa4cf0 commit 5d9ce5a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import it.gov.pagopa.onboarding.citizen.model.CitizenConsent;
import reactor.core.publisher.Mono;

import java.util.List;

public interface CitizenSpecificRepository {
Mono<List<String>> findByFiscalCodeAndTppStateTrue(String fiscalCode);

Mono<CitizenConsent> findByFiscalCodeAndTppId(String fiscalCode, String tppId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@
import lombok.Data;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
public class CitizenSpecificRepositoryImpl implements CitizenSpecificRepository {

Expand All @@ -25,20 +17,7 @@ public CitizenSpecificRepositoryImpl(ReactiveMongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}

public Mono<List<String>> findByFiscalCodeAndTppStateTrue(String fiscalCode) {
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(Criteria.where("fiscalCode").is(fiscalCode)),
Aggregation.project("consents").asArray("consentsArray"),
Aggregation.match(Criteria.where("consentsArray.v.tppState").is(true)),
Aggregation.project("consentsArray.k")
);

return mongoTemplate.aggregate(aggregation, "citizen_consents", ConsentKeyWrapper.class)
.collectList()
.map(results -> results.stream()
.map(ConsentKeyWrapper::getK)
.collect(Collectors.toList()));
}
public Mono<CitizenConsent> findByFiscalCodeAndTppId(String fiscalCode, String tppId) {
if (tppId == null) {
return Mono.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static it.gov.pagopa.common.utils.Utils.inputSanify;

Expand Down Expand Up @@ -107,8 +107,12 @@ public Mono<CitizenConsentDTO> getConsentStatus(String fiscalCode, String tppId)
public Mono<List<String>> getTppEnabledList(String fiscalCode) {
log.info("[EMD-CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] Received hashedFiscalCode: {}", Utils.createSHA256(fiscalCode));

return citizenRepository.findByFiscalCodeAndTppStateTrue(fiscalCode)
.switchIfEmpty(Mono.just(Collections.emptyList()))
return citizenRepository.findByFiscalCode(fiscalCode)
.switchIfEmpty(Mono.empty())
.map(citizenConsent -> citizenConsent.getConsents().entrySet().stream()
.filter(tpp -> tpp.getValue().getTppState())
.map(Map.Entry::getKey)
.toList())
.doOnSuccess(tppIdList -> log.info("EMD][CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] Consents found: {}", (tppIdList.size())));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
Expand All @@ -32,23 +29,6 @@ public void setUp() {
repository = new CitizenSpecificRepositoryImpl(mongoTemplate);
}

@Test
void testFindByFiscalCodeAndTppStateTrue() {
CitizenSpecificRepositoryImpl.ConsentKeyWrapper key = new CitizenSpecificRepositoryImpl.ConsentKeyWrapper();
key.setK("tppId");
String hashedFiscalCode = "hashedCode";

when(mongoTemplate.aggregate(Mockito.any(), anyString(), Mockito.eq(CitizenSpecificRepositoryImpl.ConsentKeyWrapper.class)))
.thenReturn(Flux.just(key));

List<String> result = repository.findByFiscalCodeAndTppStateTrue(hashedFiscalCode).block();

Assertions.assertNotNull(result);
Assertions.assertEquals(1,result.size());
Assertions.assertEquals("tppId", result.get(0));

}

@Test
void testFindByFiscalCodeAndTppId() {
String hashedFiscalCode = "hashedCode";
Expand Down

0 comments on commit 5d9ce5a

Please sign in to comment.