From 1f71071a9e0289fa5c6b7264d3ea3cf20834d393 Mon Sep 17 00:00:00 2001 From: Vitolo-Andrea Date: Wed, 23 Oct 2024 11:10:18 +0200 Subject: [PATCH] Mapper fix --- .../citizen/service/CitizenServiceImpl.java | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java index 51962a8..35bd2b1 100644 --- a/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java +++ b/src/main/java/it/gov/pagopa/onboarding/citizen/service/CitizenServiceImpl.java @@ -32,7 +32,6 @@ public CitizenServiceImpl(CitizenRepository citizenRepository, CitizenConsentObj this.mapperToObject = mapperToObject; this.exceptionMap = exceptionMap; } - @Override public Mono createCitizenConsent(CitizenConsentDTO citizenConsentDTO) { CitizenConsent citizenConsent = mapperToObject.map(citizenConsentDTO); @@ -41,10 +40,16 @@ public Mono createCitizenConsent(CitizenConsentDTO citizenCon citizenConsent.setCreationDate(LocalDateTime.now()); citizenConsent.setLastUpdateDate(LocalDateTime.now()); log.info("[EMD-CITIZEN][CREATE] Received consent: {}",inputSanify(citizenConsent.toString())); - return citizenRepository.save(citizenConsent) - .map(mapperToDTO::map) - .doOnSuccess(savedConsent -> log.info("[EMD][CREATE-CITIZEN-CONSENT] Created")); - + return citizenRepository.findById(hashedFiscalCode) + .flatMap(existingConsent -> { + log.info("[EMD][CREATE-CITIZEN-CONSENT] Citizen consent already exists"); + return Mono.just(mapperToDTO.map(existingConsent)); + }) + .switchIfEmpty( + citizenRepository.save(citizenConsent) + .doOnSuccess(savedConsent -> log.info("[EMD][CREATE-CITIZEN-CONSENT] Created new citizen consent")) + .map(mapperToDTO::map) + ); } @Override @@ -54,7 +59,7 @@ public Mono updateChannelState(String fiscalCode, String tppI ,hashedFiscalCode, inputSanify(tppId), tppState); return citizenRepository.findByHashedFiscalCodeAndTppId(hashedFiscalCode, tppId) .switchIfEmpty(Mono.error(exceptionMap.throwException - (ExceptionName.CITIZEN_NOT_ONBOARDED,"Citizen not founded during update state process"))) + (ExceptionName.CITIZEN_NOT_ONBOARDED,"Citizen consent not founded during update state process"))) .flatMap(citizenConsent -> { citizenConsent.setTppState(tppState); citizenConsent.setLastUpdateDate(LocalDateTime.now()); @@ -70,9 +75,9 @@ public Mono getConsentStatus(String fiscalCode, String tppId) log.info("[EMD-CITIZEN][GET-CONSENT-STATUS] Received hashedFiscalCode: {} and tppId: {}",hashedFiscalCode,inputSanify(tppId)); return citizenRepository.findByHashedFiscalCodeAndTppId(hashedFiscalCode, tppId) .switchIfEmpty(Mono.error(exceptionMap.throwException - (ExceptionName.CITIZEN_NOT_ONBOARDED,"Citizen not founded during get process "))) + (ExceptionName.CITIZEN_NOT_ONBOARDED,"Citizen consent not founded during get process "))) .map(mapperToDTO::map) - .doOnSuccess(consent -> log.info("[EMD-CITIZEN][GET-CONSENT-STATUS] Consent found:: {}",consent)); + .doOnSuccess(consent -> log.info("[EMD-CITIZEN][GET-CONSENT-STATUS] Consent consent found:: {}",consent)); }