class AuthorisationClosingService {

    @Transactional
    public void closePreviousAuthorisationsByAuthorisation(AuthorisationEntity authorisation, PsuIdData psuIdData) {
        if (psuIdData == null || psuIdData.isEmpty()) {
            log.info("Closing previous authorisations by PSU is skipped, because no PSU data has been provided in the request");
            return;
        }

        String parentId = authorisation.getParentExternalId();
        List<AuthorisationEntity> parentAuthorisations = authServiceResolver.getAuthService(authorisation.getType()).getAuthorisationsByParentId(parentId);
        List<AuthorisationEntity> previousAuthorisations = parentAuthorisations.stream()
                                                               .filter(a -> !a.getExternalId().equals(authorisation.getExternalId()))
                                                               .collect(Collectors.toList());

        closePreviousAuthorisationsByPsu(authorisation.getType(), previousAuthorisations, psuIdData);
    }

}