Skip to content

Commit

Permalink
better error handling when the notification type does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
ErykKul committed May 24, 2022
1 parent ca87b28 commit aa38724
Showing 1 changed file with 44 additions and 40 deletions.
84 changes: 44 additions & 40 deletions src/main/java/edu/harvard/iq/dataverse/api/Notifications.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,18 @@ public Response muteEmailsForUser(@PathParam("typeName") String typeName) {
return error(Response.Status.BAD_REQUEST, "Only an AuthenticatedUser can have notifications.");
}

UserNotification.Type notificationType = UserNotification.Type.valueOf(typeName);
if (notificationType != null) {
AuthenticatedUser authenticatedUser = (AuthenticatedUser) user;
Set<UserNotification.Type> mutedEmails = authenticatedUser.getMutedEmails();
mutedEmails.add(notificationType);
authenticatedUser.setMutedEmails(mutedEmails);
authSvc.update(authenticatedUser);
return ok("Notification emails of type " + typeName + " muted.");
UserNotification.Type notificationType;
try {
notificationType = UserNotification.Type.valueOf(typeName);
} catch (Exception ignore) {
return notFound("Notification type " + typeName + " not found.");
}

return notFound("Notification type " + typeName + " not found.");
AuthenticatedUser authenticatedUser = (AuthenticatedUser) user;
Set<UserNotification.Type> mutedEmails = authenticatedUser.getMutedEmails();
mutedEmails.add(notificationType);
authenticatedUser.setMutedEmails(mutedEmails);
authSvc.update(authenticatedUser);
return ok("Notification emails of type " + typeName + " muted.");
}

@DELETE
Expand All @@ -189,17 +190,18 @@ public Response unmuteEmailsForUser(@PathParam("typeName") String typeName) {
return error(Response.Status.BAD_REQUEST, "Only an AuthenticatedUser can have notifications.");
}

UserNotification.Type notificationType = UserNotification.Type.valueOf(typeName);
if (notificationType != null) {
AuthenticatedUser authenticatedUser = (AuthenticatedUser) user;
Set<UserNotification.Type> mutedEmails = authenticatedUser.getMutedEmails();
mutedEmails.remove(notificationType);
authenticatedUser.setMutedEmails(mutedEmails);
authSvc.update(authenticatedUser);
return ok("Notification emails of type " + typeName + " unmuted.");
UserNotification.Type notificationType;
try {
notificationType = UserNotification.Type.valueOf(typeName);
} catch (Exception ignore) {
return notFound("Notification type " + typeName + " not found.");
}

return notFound("Notification type " + typeName + " not found.");
AuthenticatedUser authenticatedUser = (AuthenticatedUser) user;
Set<UserNotification.Type> mutedEmails = authenticatedUser.getMutedEmails();
mutedEmails.remove(notificationType);
authenticatedUser.setMutedEmails(mutedEmails);
authSvc.update(authenticatedUser);
return ok("Notification emails of type " + typeName + " unmuted.");
}

@GET
Expand Down Expand Up @@ -245,17 +247,18 @@ public Response muteNotificationsForUser(@PathParam("typeName") String typeName)
return error(Response.Status.BAD_REQUEST, "Only an AuthenticatedUser can have notifications.");
}

UserNotification.Type notificationType = UserNotification.Type.valueOf(typeName);
if (notificationType != null) {
AuthenticatedUser authenticatedUser = (AuthenticatedUser) user;
Set<UserNotification.Type> mutedNotifications = authenticatedUser.getMutedNotifications();
mutedNotifications.add(notificationType);
authenticatedUser.setMutedNotifications(mutedNotifications);
authSvc.update(authenticatedUser);
return ok("Notification of type " + typeName + " muted.");
UserNotification.Type notificationType;
try {
notificationType = UserNotification.Type.valueOf(typeName);
} catch (Exception ignore) {
return notFound("Notification type " + typeName + " not found.");
}

return notFound("Notification type " + typeName + " not found.");
AuthenticatedUser authenticatedUser = (AuthenticatedUser) user;
Set<UserNotification.Type> mutedNotifications = authenticatedUser.getMutedNotifications();
mutedNotifications.add(notificationType);
authenticatedUser.setMutedNotifications(mutedNotifications);
authSvc.update(authenticatedUser);
return ok("Notification of type " + typeName + " muted.");
}

@DELETE
Expand All @@ -275,16 +278,17 @@ public Response unmuteNotificationsForUser(@PathParam("typeName") String typeNam
return error(Response.Status.BAD_REQUEST, "Only an AuthenticatedUser can have notifications.");
}

UserNotification.Type notificationType = UserNotification.Type.valueOf(typeName);
if (notificationType != null) {
AuthenticatedUser authenticatedUser = (AuthenticatedUser) user;
Set<UserNotification.Type> mutedNotifications = authenticatedUser.getMutedNotifications();
mutedNotifications.remove(notificationType);
authenticatedUser.setMutedNotifications(mutedNotifications);
authSvc.update(authenticatedUser);
return ok("Notification of type " + typeName + " unmuted.");
UserNotification.Type notificationType;
try {
notificationType = UserNotification.Type.valueOf(typeName);
} catch (Exception ignore) {
return notFound("Notification type " + typeName + " not found.");
}

return notFound("Notification type " + typeName + " not found.");
AuthenticatedUser authenticatedUser = (AuthenticatedUser) user;
Set<UserNotification.Type> mutedNotifications = authenticatedUser.getMutedNotifications();
mutedNotifications.remove(notificationType);
authenticatedUser.setMutedNotifications(mutedNotifications);
authSvc.update(authenticatedUser);
return ok("Notification of type " + typeName + " unmuted.");
}
}

0 comments on commit aa38724

Please sign in to comment.