Skip to content

Commit

Permalink
Fix Upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitolo-Andrea committed Nov 13, 2024
1 parent 11cada1 commit b74c2e9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public ExceptionMap() {
message
)
);

exceptions.put(TppConstants.ExceptionName.TPP_ALREADY_ONBOARDED, message ->
new ClientExceptionWithBody(
HttpStatus.FORBIDDEN,
TppConstants.ExceptionCode.TPP_ALREADY_ONBOARDED,
message
)
);
}

public RuntimeException throwException(String exceptionKey, String message) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/it/gov/pagopa/tpp/constants/TppConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@ public class TppConstants {
public static final class ExceptionCode {

public static final String TPP_NOT_ONBOARDED = "TPP_NOT_ONBOARDED";

public static final String TPP_ALREADY_ONBOARDED = "TPP_ALREADY_ONBOARDED";
public static final String GENERIC_ERROR = "GENERIC_ERROR";
private ExceptionCode() {}
}

public static final class ExceptionMessage {

public static final String TPP_NOT_ONBOARDED = "TPP_NOT_ONBOARDED";

public static final String TPP_ALREADY_ONBOARDED = "TPP_ALREADY_ONBOARDED";
public static final String GENERIC_ERROR = "GENERIC_ERROR";
private ExceptionMessage() {}
}

public static final class ExceptionName {

public static final String TPP_NOT_ONBOARDED = "TPP_NOT_ONBOARDED";

public static final String TPP_ALREADY_ONBOARDED = "TPP_ALREADY_ONBOARDED";
public static final String GENERIC_ERROR = "GENERIC_ERROR";
private ExceptionName() {}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/it/gov/pagopa/tpp/repository/TppRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface TppRepository extends ReactiveMongoRepository<Tpp,String> {

Mono<Tpp> findByTppId(String tppId);

Mono<Tpp> findByEntityId(String entityId);

}
30 changes: 17 additions & 13 deletions src/main/java/it/gov/pagopa/tpp/service/TppServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package it.gov.pagopa.tpp.service;

import it.gov.pagopa.tpp.configuration.ExceptionMap;
import it.gov.pagopa.tpp.constants.TppConstants.ExceptionName;
import it.gov.pagopa.tpp.dto.TppDTO;
import it.gov.pagopa.tpp.dto.mapper.TppObjectToDTOMapper;
import it.gov.pagopa.tpp.configuration.ExceptionMap;
import it.gov.pagopa.tpp.model.Tpp;
import it.gov.pagopa.tpp.model.mapper.TppDTOToObjectMapper;
import it.gov.pagopa.tpp.repository.TppRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;


import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -53,23 +52,28 @@ public Mono<List<TppDTO>> getEnabledList(List<String> tppIdList) {
public Mono<TppDTO> upsert(TppDTO tppDTO) {
log.info("[TPP-SERVICE][UPSERT] Received tppDTO: {}", inputSanify(tppDTO.toString()));

if(tppDTO.getTppId() == null){
Tpp tppToSave = mapperToObject.map(tppDTO);
tppToSave.setTppId(generateTppId());
log.info("[TPP-SERVICE][UPSERT] Creating new entry ...");
return saveTpp(tppToSave, "[TPP-SERVICE][UPSERT] Created new TPP with tppId: ");
}
else {
if (tppDTO.getTppId() == null) {
return tppRepository.findByEntityId(tppDTO.getEntityId())
.flatMap(existingTpp -> Mono.error(
exceptionMap.throwException(ExceptionName.TPP_ALREADY_ONBOARDED, "TPP already onboarded with entity ID.")
))
.flatMap(entityNotFound -> {
Tpp tppToSave = mapperToObject.map(tppDTO);
tppToSave.setId(generateTppId());
return saveTpp(tppToSave, "[TPP-SERVICE][UPSERT] Created new TPP with tppId");
});
} else {
return tppRepository.findByTppId(tppDTO.getTppId())
.flatMap(existingTpp -> {
log.info("[TPP-SERVICE][UPSERT] TPP with tppId [{}] already exists. Updating...", tppDTO.getTppId());
log.info("[TPP-SERVICE][UPSERT] TPP with tppId [{}] found. Updating...", tppDTO.getTppId());
Tpp tppToUpdate = mapperToObject.map(tppDTO);
tppToUpdate.setId(existingTpp.getId());
tppToUpdate.setLastUpdateDate(LocalDateTime.now());
return saveTpp(tppToUpdate, "[TPP-SERVICE][UPSERT] Updated existing TPP with tppId: ");
return saveTpp(tppToUpdate, "[TPP-SERVICE][UPSERT] Updated existing TPP with tppId");
})
.switchIfEmpty(Mono.error(exceptionMap.throwException(ExceptionName.TPP_NOT_ONBOARDED,
"Tpp not found during update process")));
.switchIfEmpty(
Mono.error(exceptionMap.throwException(ExceptionName.TPP_NOT_ONBOARDED, "TPP not found for update."))
);
}
}

Expand Down

0 comments on commit b74c2e9

Please sign in to comment.