diff --git a/src/main/java/net/frozenorb/apiv3/route/punishments/DELETEUsersIdActivePunishment.java b/src/main/java/net/frozenorb/apiv3/route/punishments/DELETEUsersIdActivePunishment.java index 53a642e..274d461 100644 --- a/src/main/java/net/frozenorb/apiv3/route/punishments/DELETEUsersIdActivePunishment.java +++ b/src/main/java/net/frozenorb/apiv3/route/punishments/DELETEUsersIdActivePunishment.java @@ -8,11 +8,13 @@ import io.vertx.ext.web.RoutingContext; import net.frozenorb.apiv3.APIv3; import net.frozenorb.apiv3.auditLog.AuditLog; import net.frozenorb.apiv3.auditLog.AuditLogActionType; +import net.frozenorb.apiv3.model.AuditLogEntry; import net.frozenorb.apiv3.model.Punishment; import net.frozenorb.apiv3.model.User; import net.frozenorb.apiv3.util.ErrorUtils; import net.frozenorb.apiv3.util.SyncUtils; +import java.util.LinkedList; import java.util.List; public final class DELETEUsersIdActivePunishment implements Handler { @@ -37,33 +39,20 @@ public final class DELETEUsersIdActivePunishment implements Handler punishments = SyncUtils.runBlocking(v -> Punishment.findByUserAndType(target, ImmutableSet.of(type), v)); - Punishment activePunishment = null; + List removedPunishments = new LinkedList<>(); for (Punishment punishment : punishments) { - if (punishment.isActive()) { - activePunishment = punishment; - break; - } + if (!punishment.isActive()) continue; + + SyncUtils.runBlocking(v -> punishment.delete(removedBy, reason, v)); + SyncUtils.runBlocking(v -> AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", punishment.getId()), v)); + removedPunishments.add(punishment); } - if (activePunishment == null) { - ErrorUtils.respondOther(ctx, 409, "User provided has no active punishments.", "noActivePunishments", ImmutableMap.of()); - return; - } - - Punishment finalActivePunishment = activePunishment; - SyncUtils.runBlocking(v -> finalActivePunishment.delete(removedBy, reason, v)); - - if (removedBy != null) { - AuditLog.log(removedBy.getId(), requestBody.getString("removedByIp"), ctx, AuditLogActionType.PUNISHMENT_DELETE, ImmutableMap.of("punishmentId", activePunishment.getId()), (ignored, error) -> { - if (error != null) { - ErrorUtils.respondInternalError(ctx, error); - } else { - APIv3.respondJson(ctx, 200, finalActivePunishment); - } - }); + if (!removedPunishments.isEmpty()) { + APIv3.respondJson(ctx, 200, removedPunishments); } else { - APIv3.respondJson(ctx, 200, finalActivePunishment); + ErrorUtils.respondOther(ctx, 409, "User provided has no active punishments.", "noActivePunishments", ImmutableMap.of()); } }