fix #1950:Export of unsubscribed users exports all users (#1965)

Co-authored-by: keshav <keshav.gupta@jarvis.consulting>
This commit is contained in:
Keshav Gupta 2024-08-02 00:05:33 +05:30 committed by GitHub
parent fedc51514d
commit c334d2e6e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 12 additions and 4 deletions

View file

@ -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
}

View file

@ -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));

View file

@ -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)))

View file

@ -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