Search in sources :

Example 1 with EmailSubscription

use of com.redhat.cloud.notifications.models.EmailSubscription in project notifications-backend by RedHatInsights.

the class UserConfigResource method getPreferences.

@GET
@Path("/notification-preference/{bundleName}/{applicationName}")
@Produces(APPLICATION_JSON)
@Operation(hidden = true)
public UserConfigPreferences getPreferences(@Context SecurityContext sec, @PathParam("bundleName") String bundleName, @PathParam("applicationName") String applicationName) {
    final RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    final String account = principal.getAccount();
    final String name = principal.getName();
    final UserConfigPreferences preferences = new UserConfigPreferences();
    // TODO Get the DAILY and INSTANT subscriptions with a single SQL query and return UserConfigPreferences directly from Hibernate.
    EmailSubscription daily = emailSubscriptionRepository.getEmailSubscription(account, name, bundleName, applicationName, EmailSubscriptionType.DAILY);
    preferences.setDailyEmail(daily != null);
    EmailSubscription instant = emailSubscriptionRepository.getEmailSubscription(account, name, bundleName, applicationName, EmailSubscriptionType.INSTANT);
    preferences.setInstantEmail(instant != null);
    return preferences;
}
Also used : RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) UserConfigPreferences(com.redhat.cloud.notifications.routers.models.UserConfigPreferences) EmailSubscription(com.redhat.cloud.notifications.models.EmailSubscription) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Operation(org.eclipse.microprofile.openapi.annotations.Operation)

Example 2 with EmailSubscription

use of com.redhat.cloud.notifications.models.EmailSubscription in project notifications-backend by RedHatInsights.

the class LifecycleITest method subscribeUserPreferences.

@Transactional
void subscribeUserPreferences(String accountId, String userId, UUID appId) {
    Application application = entityManager.find(Application.class, appId);
    EmailSubscription subscription = new EmailSubscription();
    subscription.setId(new EmailSubscriptionId());
    subscription.setAccountId(accountId);
    subscription.setUserId(userId);
    subscription.setApplication(application);
    subscription.setType(INSTANT);
    entityManager.persist(subscription);
}
Also used : EmailSubscriptionId(com.redhat.cloud.notifications.models.EmailSubscriptionId) Application(com.redhat.cloud.notifications.models.Application) EmailSubscription(com.redhat.cloud.notifications.models.EmailSubscription) Transactional(javax.transaction.Transactional)

Example 3 with EmailSubscription

use of com.redhat.cloud.notifications.models.EmailSubscription in project notifications-backend by RedHatInsights.

the class UserConfigResource method getSettingsValueForUser.

/**
 * Pulls the user settings values of an user across all the know applications of a bundle
 */
private SettingsValues getSettingsValueForUser(String account, String username, String bundleName) {
    if (bundleName == null || bundleName.equals("")) {
        throw new BadRequestException("bundleName must have a value");
    }
    SettingsValues settingsValues = new SettingsValues();
    Bundle bundle = bundleRepository.getBundle(bundleName);
    if (bundle == null) {
        throw new BadRequestException("Unknown bundleName: " + bundleName);
    } else {
        BundleSettingsValue bundleSettingsValue = new BundleSettingsValue();
        bundleSettingsValue.displayName = bundle.getDisplayName();
        settingsValues.bundles.put(bundle.getName(), bundleSettingsValue);
        for (Application application : applicationRepository.getApplications(bundle.getName())) {
            ApplicationSettingsValue applicationSettingsValue = new ApplicationSettingsValue();
            applicationSettingsValue.displayName = application.getDisplayName();
            settingsValues.bundles.get(bundle.getName()).applications.put(application.getName(), applicationSettingsValue);
            for (EmailSubscriptionType emailSubscriptionType : EmailSubscriptionType.values()) {
                // TODO NOTIF-450 How do we deal with a failure here? What kind of response should be sent to the UI when the engine is down?
                boolean supported = templateEngineClient.isSubscriptionTypeSupported(bundle.getName(), application.getName(), emailSubscriptionType);
                if (supported) {
                    settingsValues.bundles.get(bundle.getName()).applications.get(application.getName()).notifications.put(emailSubscriptionType, false);
                }
            }
        }
        List<EmailSubscription> emailSubscriptions = emailSubscriptionRepository.getEmailSubscriptionsForUser(account, username);
        for (EmailSubscription emailSubscription : emailSubscriptions) {
            if (settingsValues.bundles.containsKey(emailSubscription.getApplication().getBundle().getName())) {
                BundleSettingsValue bundleSettings = settingsValues.bundles.get(emailSubscription.getApplication().getBundle().getName());
                if (bundleSettings.applications.containsKey(emailSubscription.getApplication().getName())) {
                    ApplicationSettingsValue applicationSettingsValue = bundleSettings.applications.get(emailSubscription.getApplication().getName());
                    if (applicationSettingsValue.notifications.containsKey(emailSubscription.getType())) {
                        bundleSettings.applications.get(emailSubscription.getApplication().getName()).notifications.put(emailSubscription.getType(), true);
                    }
                }
            }
        }
        return settingsValues;
    }
}
Also used : ApplicationSettingsValue(com.redhat.cloud.notifications.routers.models.SettingsValues.ApplicationSettingsValue) SettingsValues(com.redhat.cloud.notifications.routers.models.SettingsValues) BundleSettingsValue(com.redhat.cloud.notifications.routers.models.SettingsValues.BundleSettingsValue) EmailSubscriptionType(com.redhat.cloud.notifications.models.EmailSubscriptionType) Bundle(com.redhat.cloud.notifications.models.Bundle) BadRequestException(javax.ws.rs.BadRequestException) Application(com.redhat.cloud.notifications.models.Application) EmailSubscription(com.redhat.cloud.notifications.models.EmailSubscription)

Aggregations

EmailSubscription (com.redhat.cloud.notifications.models.EmailSubscription)3 Application (com.redhat.cloud.notifications.models.Application)2 RhIdPrincipal (com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal)1 Bundle (com.redhat.cloud.notifications.models.Bundle)1 EmailSubscriptionId (com.redhat.cloud.notifications.models.EmailSubscriptionId)1 EmailSubscriptionType (com.redhat.cloud.notifications.models.EmailSubscriptionType)1 SettingsValues (com.redhat.cloud.notifications.routers.models.SettingsValues)1 ApplicationSettingsValue (com.redhat.cloud.notifications.routers.models.SettingsValues.ApplicationSettingsValue)1 BundleSettingsValue (com.redhat.cloud.notifications.routers.models.SettingsValues.BundleSettingsValue)1 UserConfigPreferences (com.redhat.cloud.notifications.routers.models.UserConfigPreferences)1 Transactional (javax.transaction.Transactional)1 BadRequestException (javax.ws.rs.BadRequestException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Operation (org.eclipse.microprofile.openapi.annotations.Operation)1