class SubscriptionService {

    public void updateSubscription(Subscription subscription, String alarmId) {
        Alarm alarm = seyrenRepository.getAlarm(alarmId);
        Optional<Subscription> actualSub = alarm.getSubscriptions().stream().filter(s -> s.getId().equals(subscription.getId())).findAny();
        if (!actualSub.isPresent()) {
            throw new CerebroException(ErrorCode.SUBSCRIPTION_UNKNOWN,
                "The subscription passed as parameter does not exist in the Alarm " + alarmId);
        }

        validateUpdateSubscription(actualSub.get(), subscription);

        if (alarm.isEnabled()) {
            if (isLastSubscriptionActiveToDisable(actualSub.get(), subscription, alarm)) {
                alarm.setEnabled(false);
                seyrenRepository.updateAlarm(alarm);
            }
        } else if (!actualSub.get().isEnabled() && subscription.isEnabled()) {
            alarm.setEnabled(true);
            seyrenRepository.updateAlarm(alarm);
        }

        seyrenRepository.updateSubscription(subscription, alarmId);
    }

}