From 7bfbd6a856123ebd2bb067f9195c3e29660dc764 Mon Sep 17 00:00:00 2001
From: Kailash Nadh <kailash@nadh.in>
Date: Wed, 4 Dec 2024 22:07:54 +0530
Subject: [PATCH] Add `all` to subscriber deletion by query which broke with
 `query` validation. Ref: #2122.

---
 cmd/subscribers.go                    | 5 ++++-
 docs/docs/content/apis/subscribers.md | 9 +++++----
 frontend/src/views/Subscribers.vue    | 3 +++
 3 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/cmd/subscribers.go b/cmd/subscribers.go
index 5497cbb41..1f65b38e7 100644
--- a/cmd/subscribers.go
+++ b/cmd/subscribers.go
@@ -30,6 +30,7 @@ type subQueryReq struct {
 	Action             string `json:"action"`
 	Status             string `json:"status"`
 	SubscriptionStatus string `json:"subscription_status"`
+	All                bool   `json:"all"`
 }
 
 // subProfileData represents a subscriber's collated data in JSON
@@ -439,7 +440,9 @@ func handleDeleteSubscribersByQuery(c echo.Context) error {
 		return err
 	}
 
-	if req.Query == "" {
+	if req.All {
+		req.Query = ""
+	} else if req.Query == "" {
 		return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.invalidFields", "name", "query"))
 	}
 
diff --git a/docs/docs/content/apis/subscribers.md b/docs/docs/content/apis/subscribers.md
index 2ac6529ff..c22c61d9e 100644
--- a/docs/docs/content/apis/subscribers.md
+++ b/docs/docs/content/apis/subscribers.md
@@ -604,10 +604,11 @@ Delete subscribers based on SQL expression.
 
 ##### Parameters
 
-| Name     | Type     | Required | Description                                 |
-|:---------|:---------|:---------|:--------------------------------------------|
-| query    | string   | Yes      | SQL expression to filter subscribers with.  |
-| list_ids | []number | No       | Optional list IDs to limit the filtering to.|
+| Name     | Type     | Required | Description                                                        |
+|:---------|:---------|:---------|:-------------------------------------------------------------------|
+| query    | string   | No       | SQL expression to filter subscribers with.                         |
+| list_ids | []number | No       | Optional list IDs to limit the filtering to.                       |
+| all      | bool     | No       | When set to `true`, ignores any query and deletes all subscribers. |
 
 
 ##### Example Request
diff --git a/frontend/src/views/Subscribers.vue b/frontend/src/views/Subscribers.vue
index 1c8d1c38b..5b25cde86 100644
--- a/frontend/src/views/Subscribers.vue
+++ b/frontend/src/views/Subscribers.vue
@@ -423,6 +423,9 @@ export default Vue.extend({
         // 'All' is selected, delete by query.
         fn = () => {
           this.$api.deleteSubscribersByQuery({
+            // If the query expression is empty, explicitly pass `all=true`
+            // so that the backend deletes all records in the DB with an empty query string.
+            all: this.queryParams.queryExp.trim() === '',
             query: this.queryParams.queryExp,
             list_ids: this.queryParams.listID ? [this.queryParams.listID] : null,
             subscription_status: this.queryParams.subStatus,