diff --git a/core/src/main/java/it/pagopa/selfcare/mscore/core/DelegationServiceImpl.java b/core/src/main/java/it/pagopa/selfcare/mscore/core/DelegationServiceImpl.java index f758e6fb2..e568c4fe9 100644 --- a/core/src/main/java/it/pagopa/selfcare/mscore/core/DelegationServiceImpl.java +++ b/core/src/main/java/it/pagopa/selfcare/mscore/core/DelegationServiceImpl.java @@ -48,6 +48,16 @@ public Delegation createDelegation(Delegation delegation) { setPartnerByInstitutionTaxCode(delegation); } + Delegation savedDelegation = checkIfExistsAndSaveDelegation(delegation); + try { + notificationService.sendMailForDelegation(delegation.getInstitutionFromName(), delegation.getProductId(), delegation.getTo()); + } catch (Exception e) { + log.error(SEND_MAIL_FOR_DELEGATION_ERROR.getMessage() + ":", e.getMessage(), e); + } + return savedDelegation; + } + + private Delegation checkIfExistsAndSaveDelegation(Delegation delegation) { if(checkIfExistsWithStatus(delegation, DelegationState.ACTIVE)) { throw new ResourceConflictException(String.format(CustomError.CREATE_DELEGATION_CONFLICT.getMessage()), CustomError.CREATE_DELEGATION_CONFLICT.getCode()); @@ -67,11 +77,6 @@ public Delegation createDelegation(Delegation delegation) { } catch (Exception e) { throw new MsCoreException(CREATE_DELEGATION_ERROR.getMessage(), CREATE_DELEGATION_ERROR.getCode()); } - try { - notificationService.sendMailForDelegation(delegation.getInstitutionFromName(), delegation.getProductId(), delegation.getTo()); - } catch (Exception e) { - log.error(SEND_MAIL_FOR_DELEGATION_ERROR.getMessage() + ":", e.getMessage(), e); - } return savedDelegation; } @@ -123,26 +128,7 @@ public Delegation createDelegationFromInstitutionsTaxCode(Delegation delegation) INSTITUTION_TAX_CODE_NOT_FOUND.getCode())); delegation.setFrom(from); - if(checkIfExistsWithStatus(delegation, DelegationState.ACTIVE)) { - throw new ResourceConflictException(String.format(CustomError.CREATE_DELEGATION_CONFLICT.getMessage()), - CustomError.CREATE_DELEGATION_CONFLICT.getCode()); - } - - Delegation savedDelegation; - try { - if(checkIfExistsWithStatus(delegation, DelegationState.DELETED)){ - savedDelegation = delegationConnector.findAndActivate(delegation.getFrom(),delegation.getTo(),delegation.getProductId()); - } else { - delegation.setCreatedAt(OffsetDateTime.now()); - delegation.setUpdatedAt(OffsetDateTime.now()); - delegation.setStatus(DelegationState.ACTIVE); - savedDelegation = delegationConnector.save(delegation); - } - institutionService.updateInstitutionDelegation(delegation.getTo(), true); - return savedDelegation; - } catch (Exception e) { - throw new MsCoreException(CREATE_DELEGATION_ERROR.getMessage(), CREATE_DELEGATION_ERROR.getCode()); - } + return checkIfExistsAndSaveDelegation(delegation); } @Override