From c334d2e6e181a75578ec58ae862fcf47edb3836c Mon Sep 17 00:00:00 2001 From: Keshav Gupta Date: Fri, 2 Aug 2024 00:05:33 +0530 Subject: [PATCH] fix #1950:Export of unsubscribed users exports all users (#1965) Co-authored-by: keshav --- cmd/subscribers.go | 5 ++++- frontend/src/views/Subscribers.vue | 4 ++++ internal/core/subscribers.go | 4 ++-- queries.sql | 3 ++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/cmd/subscribers.go b/cmd/subscribers.go index d586d0a0..353387b7 100644 --- a/cmd/subscribers.go +++ b/cmd/subscribers.go @@ -136,8 +136,11 @@ func handleExportSubscribers(c echo.Context) error { return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID")) } + // Filter by subscription status + subStatus := c.QueryParam("subscription_status") + // Get the batched export iterator. - exp, err := app.core.ExportSubscribers(query, subIDs, listIDs, app.constants.DBBatchSize) + exp, err := app.core.ExportSubscribers(query, subIDs, listIDs, subStatus, app.constants.DBBatchSize) if err != nil { return err } diff --git a/frontend/src/views/Subscribers.vue b/frontend/src/views/Subscribers.vue index b4e19388..f05e0559 100644 --- a/frontend/src/views/Subscribers.vue +++ b/frontend/src/views/Subscribers.vue @@ -394,6 +394,10 @@ export default Vue.extend({ q.append('list_id', this.queryParams.listID); } + if (this.queryParams.subStatus) { + q.append('subscription_status', this.queryParams.subStatus); + } + // Export selected subscribers. if (!this.bulk.all && this.bulk.checked.length > 0) { this.bulk.checked.map((s) => q.append('id', s.id)); diff --git a/internal/core/subscribers.go b/internal/core/subscribers.go index c78acbfa..f2159170 100644 --- a/internal/core/subscribers.go +++ b/internal/core/subscribers.go @@ -175,7 +175,7 @@ func (c *Core) GetSubscriberProfileForExport(id int, uuid string) (models.Subscr // on the given criteria in an exportable form. The iterator function returned can be called // repeatedly until there are nil subscribers. It's an iterator because exports can be extremely // large and may have to be fetched in batches from the DB and streamed somewhere. -func (c *Core) ExportSubscribers(query string, subIDs, listIDs []int, batchSize int) (func() ([]models.SubscriberExport, error), error) { +func (c *Core) ExportSubscribers(query string, subIDs, listIDs []int, subStatus string, batchSize int) (func() ([]models.SubscriberExport, error), error) { // There's an arbitrary query condition. cond := "" if query != "" { @@ -219,7 +219,7 @@ func (c *Core) ExportSubscribers(query string, subIDs, listIDs []int, batchSize id := 0 return func() ([]models.SubscriberExport, error) { var out []models.SubscriberExport - if err := tx.Select(&out, pq.Array(listIDs), id, pq.Array(subIDs), batchSize); err != nil { + if err := tx.Select(&out, pq.Array(listIDs), id, pq.Array(subIDs), subStatus, batchSize); err != nil { c.log.Printf("error exporting subscribers by query: %v", err) return nil, echo.NewHTTPError(http.StatusInternalServerError, c.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.subscribers}", "error", pqErrMsg(err))) diff --git a/queries.sql b/queries.sql index 9bed63ec..57cab4d4 100644 --- a/queries.sql +++ b/queries.sql @@ -349,11 +349,12 @@ SELECT subscribers.id, -- Optional list filtering. (CASE WHEN CARDINALITY($1::INT[]) > 0 THEN true ELSE false END) AND subscriber_lists.subscriber_id = subscribers.id + AND ($4 = '' OR subscriber_lists.status = $4::subscription_status) ) WHERE subscriber_lists.list_id = ALL($1::INT[]) AND id > $2 AND (CASE WHEN CARDINALITY($3::INT[]) > 0 THEN id=ANY($3) ELSE true END) %query% - ORDER BY subscribers.id ASC LIMIT (CASE WHEN $4 < 1 THEN NULL ELSE $4 END); + ORDER BY subscribers.id ASC LIMIT (CASE WHEN $5 < 1 THEN NULL ELSE $5 END); -- name: query-subscribers-template -- raw: true