Search in sources :

Example 1 with ApplicationSettingsValue

use of com.redhat.cloud.notifications.routers.models.SettingsValues.ApplicationSettingsValue in project notifications-backend by RedHatInsights.

the class UserConfigResource method saveSettings.

@POST
@Path("/notification-preference")
@Consumes(APPLICATION_JSON)
@Produces(TEXT_PLAIN)
@Operation(hidden = true)
@Transactional
public Response saveSettings(@Context SecurityContext sec, @Valid SettingsValues values) {
    final RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    final String account = principal.getAccount();
    final String name = principal.getName();
    final List<Boolean> subscriptionRequests = new ArrayList<>();
    values.bundles.forEach((bundleName, bundleSettingsValue) -> bundleSettingsValue.applications.forEach((applicationName, applicationSettingsValue) -> {
        Application app = applicationRepository.getApplication(bundleName, applicationName);
        applicationSettingsValue.notifications.forEach((emailSubscriptionType, subscribed) -> {
            if (subscribed) {
                if (app != null) {
                    subscriptionRequests.add(emailSubscriptionRepository.subscribe(account, name, bundleName, applicationName, emailSubscriptionType));
                }
            } else {
                subscriptionRequests.add(emailSubscriptionRepository.unsubscribe(account, name, bundleName, applicationName, emailSubscriptionType));
            }
        });
    }));
    boolean allisSuccess = subscriptionRequests.stream().allMatch(Boolean.TRUE::equals);
    Response.ResponseBuilder builder;
    if (allisSuccess) {
        builder = Response.ok();
    } else {
        // Prevent from saving
        builder = Response.serverError().entity("Storing of settings Failed.");
        builder.type("text/plain");
    }
    return builder.build();
}
Also used : PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Constants(com.redhat.cloud.notifications.Constants) Path(javax.ws.rs.Path) SecurityContext(javax.ws.rs.core.SecurityContext) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Valid(javax.validation.Valid) EmailSubscriptionRepository(com.redhat.cloud.notifications.db.repositories.EmailSubscriptionRepository) BundleSettingsValue(com.redhat.cloud.notifications.routers.models.SettingsValues.BundleSettingsValue) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) BadRequestException(javax.ws.rs.BadRequestException) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) EmailSubscriptionType(com.redhat.cloud.notifications.models.EmailSubscriptionType) UserConfigPreferences(com.redhat.cloud.notifications.routers.models.UserConfigPreferences) BundleRepository(com.redhat.cloud.notifications.db.repositories.BundleRepository) Application(com.redhat.cloud.notifications.models.Application) ApplicationRepository(com.redhat.cloud.notifications.db.repositories.ApplicationRepository) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) RestClient(org.eclipse.microprofile.rest.client.inject.RestClient) SettingsValues(com.redhat.cloud.notifications.routers.models.SettingsValues) Transactional(javax.transaction.Transactional) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Bundle(com.redhat.cloud.notifications.models.Bundle) EmailSubscription(com.redhat.cloud.notifications.models.EmailSubscription) SettingsValueJsonForm(com.redhat.cloud.notifications.routers.models.SettingsValueJsonForm) Operation(org.eclipse.microprofile.openapi.annotations.Operation) EntityTag(javax.ws.rs.core.EntityTag) ApplicationSettingsValue(com.redhat.cloud.notifications.routers.models.SettingsValues.ApplicationSettingsValue) List(java.util.List) Response(javax.ws.rs.core.Response) RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) TemplateEngineClient(com.redhat.cloud.notifications.templates.TemplateEngineClient) TEXT_PLAIN(javax.ws.rs.core.MediaType.TEXT_PLAIN) Response(javax.ws.rs.core.Response) RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) ArrayList(java.util.ArrayList) Application(com.redhat.cloud.notifications.models.Application) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Operation(org.eclipse.microprofile.openapi.annotations.Operation) Transactional(javax.transaction.Transactional)

Example 2 with ApplicationSettingsValue

use of com.redhat.cloud.notifications.routers.models.SettingsValues.ApplicationSettingsValue 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)

Example 3 with ApplicationSettingsValue

use of com.redhat.cloud.notifications.routers.models.SettingsValues.ApplicationSettingsValue in project notifications-backend by RedHatInsights.

the class UserConfigResourceTest method createSettingsValue.

private SettingsValues createSettingsValue(String bundle, String application, Boolean daily, Boolean instant) {
    ApplicationSettingsValue applicationSettingsValue = new ApplicationSettingsValue();
    applicationSettingsValue.notifications.put(DAILY, daily);
    applicationSettingsValue.notifications.put(INSTANT, instant);
    BundleSettingsValue bundleSettingsValue = new BundleSettingsValue();
    bundleSettingsValue.applications.put(application, applicationSettingsValue);
    SettingsValues settingsValues = new SettingsValues();
    settingsValues.bundles.put(bundle, bundleSettingsValue);
    return settingsValues;
}
Also used : ApplicationSettingsValue(com.redhat.cloud.notifications.routers.models.SettingsValues.ApplicationSettingsValue) BundleSettingsValue(com.redhat.cloud.notifications.routers.models.SettingsValues.BundleSettingsValue) SettingsValues(com.redhat.cloud.notifications.routers.models.SettingsValues)

Aggregations

SettingsValues (com.redhat.cloud.notifications.routers.models.SettingsValues)3 ApplicationSettingsValue (com.redhat.cloud.notifications.routers.models.SettingsValues.ApplicationSettingsValue)3 BundleSettingsValue (com.redhat.cloud.notifications.routers.models.SettingsValues.BundleSettingsValue)3 Application (com.redhat.cloud.notifications.models.Application)2 Bundle (com.redhat.cloud.notifications.models.Bundle)2 EmailSubscription (com.redhat.cloud.notifications.models.EmailSubscription)2 EmailSubscriptionType (com.redhat.cloud.notifications.models.EmailSubscriptionType)2 BadRequestException (javax.ws.rs.BadRequestException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Constants (com.redhat.cloud.notifications.Constants)1 RhIdPrincipal (com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal)1 ApplicationRepository (com.redhat.cloud.notifications.db.repositories.ApplicationRepository)1 BundleRepository (com.redhat.cloud.notifications.db.repositories.BundleRepository)1 EmailSubscriptionRepository (com.redhat.cloud.notifications.db.repositories.EmailSubscriptionRepository)1 SettingsValueJsonForm (com.redhat.cloud.notifications.routers.models.SettingsValueJsonForm)1 UserConfigPreferences (com.redhat.cloud.notifications.routers.models.UserConfigPreferences)1 TemplateEngineClient (com.redhat.cloud.notifications.templates.TemplateEngineClient)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1