diff --git a/cmd/subscribers.go b/cmd/subscribers.go index add2e62..7be8dc1 100644 --- a/cmd/subscribers.go +++ b/cmd/subscribers.go @@ -31,6 +31,7 @@ type subQueryReq struct { TargetListIDs pq.Int64Array `json:"target_list_ids"` SubscriberIDs pq.Int64Array `json:"ids"` Action string `json:"action"` + Status string `json:"status"` } type subsWrap struct { @@ -476,7 +477,7 @@ func handleManageSubscriberLists(c echo.Context) error { var err error switch req.Action { case "add": - _, err = app.queries.AddSubscribersToLists.Exec(IDs, req.TargetListIDs) + _, err = app.queries.AddSubscribersToLists.Exec(IDs, req.TargetListIDs, req.Status) case "remove": _, err = app.queries.DeleteSubscriptions.Exec(IDs, req.TargetListIDs) case "unsubscribe": diff --git a/queries.sql b/queries.sql index bc5f197..f5245d1 100644 --- a/queries.sql +++ b/queries.sql @@ -156,9 +156,9 @@ UPDATE subscriber_lists SET status='unsubscribed', updated_at=NOW() WHERE subscriber_id = ANY($1::INT[]); -- name: add-subscribers-to-lists -INSERT INTO subscriber_lists (subscriber_id, list_id) - (SELECT a, b FROM UNNEST($1::INT[]) a, UNNEST($2::INT[]) b) - ON CONFLICT (subscriber_id, list_id) DO NOTHING; +INSERT INTO subscriber_lists (subscriber_id, list_id, status) + (SELECT a, b, (CASE WHEN $3 != '' THEN $3::subscription_status ELSE 'unconfirmed' END) FROM UNNEST($1::INT[]) a, UNNEST($2::INT[]) b) + ON CONFLICT (subscriber_id, list_id) DO UPDATE SET status=(CASE WHEN $3 != '' THEN $3::subscription_status ELSE subscriber_lists.status END); -- name: delete-subscriptions DELETE FROM subscriber_lists