Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
fix: create method checkIfExistsAndSaveDelegation to avoid code dupli…
Browse files Browse the repository at this point in the history
…cation
  • Loading branch information
giulia-tremolada committed Apr 15, 2024
1 parent c1f427c commit cd2f9c7
Showing 1 changed file with 11 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cd2f9c7

Please sign in to comment.