class ProfileServiceImpl extends BaseTransactionalService implements ProfileService {

    @Override
    public ServiceResult<ProfileAgreementResource> getProfileAgreement(long userId) {
        return find(userRepository.findById(userId), notFoundError(User.class, userId))
                .andOnSuccess(user ->
                        getCurrentAgreement().andOnSuccess(currentAgreement -> {
                            Profile profile = profileRepository.findById(user.getProfileId()).orElse(null);
                            boolean hasAgreement = profile != null && profile.getAgreement() != null;
                            boolean hasCurrentAgreement = hasAgreement && currentAgreement.getId().equals(profile.getAgreement().getId());
                            ProfileAgreementResource profileAgreementResource = new ProfileAgreementResource();
                            profileAgreementResource.setUser(user.getId());
                            profileAgreementResource.setAgreement(agreementMapper.mapToResource(currentAgreement));
                            profileAgreementResource.setCurrentAgreement(hasCurrentAgreement);
                            if (hasCurrentAgreement) {
                                profileAgreementResource.setAgreementSignedDate(profile.getAgreementSignedDate());
                            }
                            return serviceSuccess(profileAgreementResource);
                        })
                );
    }

}