Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api fix #19

Merged
merged 13 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static final class ValidationRegex {

public static final String FISCAL_CODE_STRUCTURE_REGEX = "(^([A-Za-z]{6}[0-9lmnpqrstuvLMNPQRSTUV]{2}[abcdehlmprstABCDEHLMPRST][0-9lmnpqrstuvLMNPQRSTUV]{2}[A-Za-z][0-9lmnpqrstuvLMNPQRSTUV]{3}[A-Za-z])$)|(^(\\d{11})$)";

public static final String TPP_STRUCTURE_REGEX = "(^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}-\\d{13}$)";
private ValidationRegex() {}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package it.gov.pagopa.onboarding.citizen.controller;

import it.gov.pagopa.onboarding.citizen.dto.CitizenConsentDTO;
import it.gov.pagopa.onboarding.citizen.dto.CitizenConsentStateUpdateDTO;
import jakarta.validation.Valid;


import jakarta.validation.constraints.Pattern;
import org.springframework.http.ResponseEntity;
Expand All @@ -12,16 +11,19 @@
import java.util.List;

import static it.gov.pagopa.onboarding.citizen.constants.CitizenConstants.ValidationRegex.FISCAL_CODE_STRUCTURE_REGEX;
import static it.gov.pagopa.onboarding.citizen.constants.CitizenConstants.ValidationRegex.TPP_STRUCTURE_REGEX;


@RequestMapping("/emd/citizen")
public interface CitizenController {

@PostMapping("")
Mono<ResponseEntity<CitizenConsentDTO>> saveCitizenConsent(@Valid @RequestBody CitizenConsentDTO citizenConsentDTO);
@PostMapping("/{fiscalCode}/{tppId}")
Mono<ResponseEntity<CitizenConsentDTO>> saveCitizenConsent(@PathVariable @Pattern(regexp = FISCAL_CODE_STRUCTURE_REGEX, message = "Invalid fiscal code format") String fiscalCode,
@PathVariable @Pattern(regexp = TPP_STRUCTURE_REGEX, message = "Invalid fiscal code format") String tppId);

@PutMapping("/stateUpdate")
Mono<ResponseEntity<CitizenConsentDTO>> stateUpdate(@Valid @RequestBody CitizenConsentStateUpdateDTO citizenConsentStateUpdateDTO);
@PutMapping("/{fiscalCode}/{tppId}")
Mono<ResponseEntity<CitizenConsentDTO>> stateSwitch(@PathVariable @Pattern(regexp = FISCAL_CODE_STRUCTURE_REGEX, message = "Invalid fiscal code format") String fiscalCode,
@PathVariable @Pattern(regexp = TPP_STRUCTURE_REGEX, message = "Invalid fiscal code format") String tppId);

@GetMapping("/filter/{fiscalCode}")
Mono<ResponseEntity<String>> bloomFilterSearch(@PathVariable @Pattern(regexp = FISCAL_CODE_STRUCTURE_REGEX, message = "Invalid fiscal code format") String fiscalCode);
Expand All @@ -37,8 +39,8 @@ public interface CitizenController {
* @return the citizen consent status
*/
@GetMapping("/{fiscalCode}/{tppId}")
Mono<ResponseEntity<CitizenConsentDTO>> getCitizenConsentStatus(@PathVariable @Pattern(regexp = FISCAL_CODE_STRUCTURE_REGEX, message = "Invalid fiscal code format") String fiscalCode, @PathVariable String tppId);

Mono<ResponseEntity<CitizenConsentDTO>> getCitizenConsentStatus(@PathVariable @Pattern(regexp = FISCAL_CODE_STRUCTURE_REGEX, message = "Invalid fiscal code format") String fiscalCode,
@PathVariable @Pattern(regexp = TPP_STRUCTURE_REGEX, message = "Invalid fiscal code format") String tppId);

/**
* Get consents for a specific citizen.
Expand All @@ -54,6 +56,6 @@ public interface CitizenController {
Mono<ResponseEntity<CitizenConsentDTO>> getCitizenConsentsListEnabled(@PathVariable @Pattern(regexp = FISCAL_CODE_STRUCTURE_REGEX, message = "Invalid fiscal code format") String fiscalCode);

@GetMapping("/{tppId}")
Mono<ResponseEntity<List<CitizenConsentDTO>>> getCitizenEnabled(@PathVariable String tppId);
Mono<ResponseEntity<List<CitizenConsentDTO>>> getCitizenEnabled(@PathVariable @Pattern(regexp = TPP_STRUCTURE_REGEX, message = "Invalid fiscal code format") String fiscalCode);

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package it.gov.pagopa.onboarding.citizen.controller;

import it.gov.pagopa.onboarding.citizen.dto.CitizenConsentDTO;
import it.gov.pagopa.onboarding.citizen.dto.CitizenConsentStateUpdateDTO;
import it.gov.pagopa.onboarding.citizen.service.BloomFilterServiceImpl;
import it.gov.pagopa.onboarding.citizen.service.CitizenServiceImpl;
import jakarta.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -25,17 +23,14 @@ public CitizenControllerImpl(BloomFilterServiceImpl bloomFilterService, CitizenS
}

@Override
public Mono<ResponseEntity<CitizenConsentDTO>> saveCitizenConsent(@Valid CitizenConsentDTO citizenConsentDTO) {
return citizenService.createCitizenConsent(citizenConsentDTO)
public Mono<ResponseEntity<CitizenConsentDTO>> saveCitizenConsent(String fiscalCode, String tppId) {
return citizenService.createCitizenConsent(fiscalCode, tppId)
.map(ResponseEntity::ok);
}

@Override
public Mono<ResponseEntity<CitizenConsentDTO>> stateUpdate(@Valid CitizenConsentStateUpdateDTO citizenConsentStateUpdateDTO) {
return citizenService.updateTppState(
citizenConsentStateUpdateDTO.getFiscalCode(),
citizenConsentStateUpdateDTO.getTppId(),
citizenConsentStateUpdateDTO.getTppState())
public Mono<ResponseEntity<CitizenConsentDTO>> stateSwitch(String fiscalCode, String tppId) {
return citizenService.switchState(fiscalCode, tppId)
.map(ResponseEntity::ok);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

public interface CitizenService {

Mono<CitizenConsentDTO> createCitizenConsent(CitizenConsentDTO citizenConsent);
Mono<CitizenConsentDTO> updateTppState(String fiscalCode, String tppId, boolean tppState);
Mono<CitizenConsentDTO> createCitizenConsent(String fiscalCode, String tppId);
Mono<CitizenConsentDTO> switchState(String fiscalCode, String tppId);
Mono<CitizenConsentDTO> getCitizenConsentStatus(String fiscalCode, String tppId);
Mono<List<String>> getTppEnabledList(String fiscalCode);
Mono<CitizenConsentDTO> getCitizenConsentsList(String fiscalCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
import it.gov.pagopa.onboarding.citizen.dto.mapper.CitizenConsentObjectToDTOMapper;
import it.gov.pagopa.onboarding.citizen.model.CitizenConsent;
import it.gov.pagopa.onboarding.citizen.model.ConsentDetails;
import it.gov.pagopa.onboarding.citizen.model.mapper.CitizenConsentDTOToObjectMapper;
import it.gov.pagopa.onboarding.citizen.repository.CitizenRepository;
import it.gov.pagopa.onboarding.citizen.validation.CitizenConsentValidationServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -29,67 +28,104 @@ public class CitizenServiceImpl implements CitizenService {

private final CitizenRepository citizenRepository;
private final CitizenConsentObjectToDTOMapper mapperToDTO;
private final CitizenConsentDTOToObjectMapper mapperToObject;
private final ExceptionMap exceptionMap;
private final TppConnectorImpl tppConnector;
private final CitizenConsentValidationServiceImpl validationService;
private final BloomFilterServiceImpl bloomFilterService;

public CitizenServiceImpl(CitizenRepository citizenRepository, CitizenConsentObjectToDTOMapper mapperToDTO, CitizenConsentDTOToObjectMapper mapperToObject, ExceptionMap exceptionMap, TppConnectorImpl tppConnector, CitizenConsentValidationServiceImpl validationService) {
public CitizenServiceImpl(CitizenRepository citizenRepository,
CitizenConsentObjectToDTOMapper mapperToDTO,
ExceptionMap exceptionMap,
TppConnectorImpl tppConnector,
BloomFilterServiceImpl bloomFilterService) {
this.citizenRepository = citizenRepository;
this.mapperToDTO = mapperToDTO;
this.mapperToObject = mapperToObject;
this.exceptionMap = exceptionMap;
this.tppConnector = tppConnector;
this.validationService = validationService;
this.bloomFilterService = bloomFilterService;
}

@Override
public Mono<CitizenConsentDTO> createCitizenConsent(CitizenConsentDTO citizenConsentDTO) {
public Mono<CitizenConsentDTO> createCitizenConsent(String fiscalCode, String tppId){
log.info("[EMD][CITIZEN][UPDATE-CHANNEL-STATE] Received hashedFiscalCode: {} and tppId: {}",
Utils.createSHA256(fiscalCode), inputSanify(tppId));

CitizenConsent citizenConsent = mapperToObject.map(citizenConsentDTO);
String fiscalCode = citizenConsent.getFiscalCode();

citizenConsent.getConsents().forEach((tppId, consentDetails) -> consentDetails.setTcDate(LocalDateTime.now()));

log.info("[EMD-CITIZEN][CREATE] Received consent: {}", inputSanify(citizenConsent.toString()));

String tppId = citizenConsent.getConsents().keySet().stream().findFirst().orElse(null);
if (tppId == null) {
return Mono.error(exceptionMap.throwException(ExceptionName.TPP_NOT_FOUND, ExceptionMessage.TPP_NOT_FOUND));
}

return citizenRepository.findByFiscalCode(fiscalCode)
.flatMap(existingConsent -> validationService.handleExistingConsent(existingConsent, tppId, citizenConsent))
.switchIfEmpty(validationService.validateTppAndSaveConsent(fiscalCode, tppId, citizenConsent));
return tppConnector.get(tppId)
.onErrorMap(error -> exceptionMap.throwException(ExceptionName.TPP_NOT_FOUND, ExceptionMessage.TPP_NOT_FOUND))
.flatMap(tppResponse -> citizenRepository.findByFiscalCode(fiscalCode)
.flatMap(citizenConsent -> {
if (!citizenConsent.getConsents().containsKey(tppId)) {
citizenConsent
.getConsents().put(tppId, ConsentDetails.builder()
.tppState(true)
.tcDate(LocalDateTime.now())
.build());
return citizenRepository.save(citizenConsent)
.flatMap(savedConsent ->{
Map<String, ConsentDetails> consents = new HashMap<>();
consents.put(tppId, citizenConsent.getConsents().get(tppId));
citizenConsent.setConsents(consents);
return Mono.just(mapperToDTO.map(citizenConsent));
});
}
Map<String, ConsentDetails> consents = new HashMap<>();
consents.put(tppId, citizenConsent.getConsents().get(tppId));
citizenConsent.setConsents(consents);
return Mono.just(mapperToDTO.map(citizenConsent));
})
.switchIfEmpty(Mono.defer(() -> {
Map<String, ConsentDetails> consents = new HashMap<>();
consents.put(tppId, ConsentDetails.builder()
.tppState(true)
.tcDate(LocalDateTime.now())
.build());
CitizenConsent citizenConsentToSave = CitizenConsent.builder()
.fiscalCode(fiscalCode)
.consents(consents)
.build();
return citizenRepository
.save(citizenConsentToSave)
.map(mapperToDTO::map);
}))
)
.doOnSuccess(savedConsent -> {
log.info("[EMD][CREATE-CITIZEN-CONSENT] Created new citizen consent for fiscal code: {}", Utils.createSHA256(fiscalCode));
bloomFilterService.add(fiscalCode);
});
}

@Override
public Mono<CitizenConsentDTO> updateTppState(String fiscalCode, String tppId, boolean tppState) {
log.info("[EMD][CITIZEN][UPDATE-CHANNEL-STATE] Received hashedFiscalCode: {} and tppId: {} with state: {}",
Utils.createSHA256(fiscalCode), inputSanify(tppId), tppState);
public Mono<CitizenConsentDTO> switchState(String fiscalCode, String tppId){
log.info("[EMD][CITIZEN][UPDATE-CHANNEL-STATE] Received hashedFiscalCode: {} and tppId: {}",
Utils.createSHA256(fiscalCode), inputSanify(tppId));

return tppConnector.get(tppId)
.onErrorMap(error ->exceptionMap.throwException(ExceptionName.TPP_NOT_FOUND, ExceptionMessage.TPP_NOT_FOUND))
.flatMap(tppResponse -> citizenRepository.findByFiscalCodeAndTppId(fiscalCode, tppId)
return citizenRepository.findByFiscalCode(fiscalCode)
.switchIfEmpty(Mono.error(exceptionMap.throwException
(ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded during update state process")))
(ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded during update state process"))
)
.flatMap(citizenConsent -> {
if(!citizenConsent.getConsents().containsKey(tppId))
return Mono.error(exceptionMap.throwException
(ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded during update state process"));
ConsentDetails consentDetails = citizenConsent.getConsents().get(tppId);
consentDetails.setTppState(tppState);
return citizenRepository.save(citizenConsent);
consentDetails.setTppState(!consentDetails.getTppState());
return citizenRepository.save(citizenConsent)
.flatMap(savedConsent -> {
Map<String, ConsentDetails> consents = new HashMap<>();
consents.put(tppId, citizenConsent.getConsents().get(tppId));
citizenConsent.setConsents(consents);
return Mono.just(mapperToDTO.map(citizenConsent));
});
})
.map(mapperToDTO::map)
.doOnSuccess(savedConsent -> log.info("[EMD][CITIZEN][UPDATE-CHANNEL-STATE] Updated state")));
.doOnSuccess(savedConsent -> log.info("[EMD][CITIZEN][UPDATE-CHANNEL-STATE] Updated state"));
}

@Override
public Mono<CitizenConsentDTO> getCitizenConsentStatus(String fiscalCode, String tppId) {
log.info("[EMD-CITIZEN][GET-CONSENT-STATUS] Received hashedFiscalCode: {} and tppId: {}", Utils.createSHA256(fiscalCode), inputSanify(tppId));
return citizenRepository.findByFiscalCodeAndTppId(fiscalCode, tppId)
.switchIfEmpty(Mono.error(exceptionMap.throwException
(ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded during get process ")))
(ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded")))
.map(mapperToDTO::map)
.doOnSuccess(consent -> log.info("[EMD-CITIZEN][GET-CONSENT-STATUS] Consent consent found:: {}", consent));
.doOnSuccess(consent -> log.info("[EMD-CITIZEN][GET-CONSENT-STATUS] Consent consent found: {}", consent));

}

Expand All @@ -104,7 +140,7 @@ public Mono<List<String>> getTppEnabledList(String fiscalCode) {
.map(Map.Entry::getKey)
.toList())
.doOnSuccess(tppIdList -> {
if (tppIdList != null) {
if (tppIdList != null){
log.info("EMD][CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] Consents found: {}", (tppIdList.size()));
} else {
log.info("EMD][CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] No consents found.");
Expand All @@ -116,6 +152,8 @@ public Mono<List<String>> getTppEnabledList(String fiscalCode) {
public Mono<CitizenConsentDTO> getCitizenConsentsList(String fiscalCode) {
log.info("[EMD-CITIZEN][FIND-ALL-CITIZEN-CONSENTS] Received hashedFiscalCode: {}", (Utils.createSHA256(fiscalCode)));
return citizenRepository.findByFiscalCode(fiscalCode)
.switchIfEmpty(Mono.error(exceptionMap.throwException
(ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded during get process ")))
.map(mapperToDTO::map)
.doOnSuccess(consentList -> log.info("[EMD-CITIZEN][FIND-ALL-CITIZEN-CONSENTS] Consents found: {}", consentList));
}
Expand All @@ -125,7 +163,8 @@ public Mono<CitizenConsentDTO> getCitizenConsentsListEnabled(String fiscalCode)
log.info("[EMD-CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] Received hashedFiscalCode: {}", Utils.createSHA256(fiscalCode));

return citizenRepository.findByFiscalCode(fiscalCode)
.switchIfEmpty(Mono.empty())
.switchIfEmpty(Mono.error(exceptionMap.throwException
(ExceptionName.CITIZEN_NOT_ONBOARDED, "Citizen consent not founded during get process ")))
.map(citizenConsent -> {
Map<String, ConsentDetails> filteredConsents = citizenConsent.getConsents().entrySet().stream()
.filter(tpp -> tpp.getValue().getTppState())
Expand All @@ -135,7 +174,7 @@ public Mono<CitizenConsentDTO> getCitizenConsentsListEnabled(String fiscalCode)
return mapperToDTO.map(citizenConsent);
})
.doOnSuccess(citizenConsent -> {
if (citizenConsent != null && citizenConsent.getConsents() != null) {
if (citizenConsent != null && !citizenConsent.getConsents().isEmpty()) {
log.info("EMD][CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] Consents found: {}", citizenConsent.getConsents().size());
} else {
log.info("EMD][CITIZEN][FIND-CITIZEN-CONSENTS-ENABLED] No consents found.");
Expand Down

This file was deleted.

Loading
Loading