Skip to content

Commit

Permalink
fix #1950:Export of unsubscribed users exports all users (#1965)
Browse files Browse the repository at this point in the history
Co-authored-by: keshav <keshav.gupta@jarvis.consulting>
  • Loading branch information
keshavg2 and keshavjarvis authored Aug 1, 2024
1 parent fedc515 commit c334d2e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
5 changes: 4 additions & 1 deletion cmd/subscribers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/views/Subscribers.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions internal/core/subscribers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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)))
Expand Down
3 changes: 2 additions & 1 deletion queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c334d2e

Please sign in to comment.