Switch UI subscriber addition to a new insert-only query
This commit is contained in:
parent
ec02921c25
commit
44442b2b62
|
@ -8,6 +8,7 @@ import (
|
||||||
|
|
||||||
// Queries contains all prepared SQL queries.
|
// Queries contains all prepared SQL queries.
|
||||||
type Queries struct {
|
type Queries struct {
|
||||||
|
InsertSubscriber *sqlx.Stmt `query:"insert-subscriber"`
|
||||||
UpsertSubscriber *sqlx.Stmt `query:"upsert-subscriber"`
|
UpsertSubscriber *sqlx.Stmt `query:"upsert-subscriber"`
|
||||||
BlacklistSubscriber *sqlx.Stmt `query:"blacklist-subscriber"`
|
BlacklistSubscriber *sqlx.Stmt `query:"blacklist-subscriber"`
|
||||||
GetSubscriber *sqlx.Stmt `query:"get-subscriber"`
|
GetSubscriber *sqlx.Stmt `query:"get-subscriber"`
|
||||||
|
|
28
queries.sql
28
queries.sql
|
@ -39,22 +39,39 @@ SELECT COUNT(subscribers.id) as num FROM subscribers INNER JOIN subscriber_lists
|
||||||
WHERE subscriber_lists.list_id = %d
|
WHERE subscriber_lists.list_id = %d
|
||||||
%s;
|
%s;
|
||||||
|
|
||||||
|
-- name: insert-subscriber
|
||||||
|
WITH sub AS (
|
||||||
|
INSERT INTO subscribers (uuid, email, name, attribs)
|
||||||
|
VALUES($1, $2, $3, $4)
|
||||||
|
returning id
|
||||||
|
),
|
||||||
|
subs AS (
|
||||||
|
INSERT INTO subscriber_lists (subscriber_id, list_id)
|
||||||
|
VALUES((SELECT id FROM sub), UNNEST($5::INT[]))
|
||||||
|
ON CONFLICT (subscriber_id, list_id) DO UPDATE
|
||||||
|
SET updated_at=NOW()
|
||||||
|
)
|
||||||
|
SELECT id from sub;
|
||||||
|
|
||||||
-- name: upsert-subscriber
|
-- name: upsert-subscriber
|
||||||
-- Upserts a subscriber where existing subscribers get their names and attributes overwritten.
|
-- Upserts a subscriber where existing subscribers get their names and attributes overwritten.
|
||||||
-- The status field is only updated when $6 = 'override_status'.
|
-- The status field is only updated when $6 = 'override_status'.
|
||||||
WITH s AS (
|
WITH sub AS (
|
||||||
INSERT INTO subscribers (uuid, email, name, attribs)
|
INSERT INTO subscribers (uuid, email, name, attribs)
|
||||||
VALUES($1, $2, $3, $4)
|
VALUES($1, $2, $3, $4)
|
||||||
ON CONFLICT (email) DO UPDATE
|
ON CONFLICT (email) DO UPDATE
|
||||||
SET name=$3,
|
SET name=$3,
|
||||||
attribs=$4,
|
attribs=$4,
|
||||||
updated_at=NOW()
|
updated_at=NOW()
|
||||||
RETURNING id
|
RETURNING uuid, id
|
||||||
) INSERT INTO subscriber_lists (subscriber_id, list_id)
|
),
|
||||||
VALUES((SELECT id FROM s), UNNEST($5::INT[]))
|
subs AS (
|
||||||
|
INSERT INTO subscriber_lists (subscriber_id, list_id)
|
||||||
|
VALUES((SELECT id FROM sub), UNNEST($5::INT[]))
|
||||||
ON CONFLICT (subscriber_id, list_id) DO UPDATE
|
ON CONFLICT (subscriber_id, list_id) DO UPDATE
|
||||||
SET updated_at=NOW()
|
SET updated_at=NOW()
|
||||||
RETURNING subscriber_id;
|
)
|
||||||
|
SELECT uuid, id from sub;
|
||||||
|
|
||||||
-- name: blacklist-subscriber
|
-- name: blacklist-subscriber
|
||||||
-- Upserts a subscriber where the update will only set the status to blacklisted
|
-- Upserts a subscriber where the update will only set the status to blacklisted
|
||||||
|
@ -247,6 +264,7 @@ subs AS (
|
||||||
WHERE subscriber_lists.list_id=ANY(
|
WHERE subscriber_lists.list_id=ANY(
|
||||||
SELECT list_id FROM campaign_lists where campaign_id=$1 AND list_id IS NOT NULL
|
SELECT list_id FROM campaign_lists where campaign_id=$1 AND list_id IS NOT NULL
|
||||||
)
|
)
|
||||||
|
AND subscribers.status != 'blacklisted'
|
||||||
AND id > (SELECT last_subscriber_id FROM camp)
|
AND id > (SELECT last_subscriber_id FROM camp)
|
||||||
AND id <= (SELECT max_subscriber_id FROM camp)
|
AND id <= (SELECT max_subscriber_id FROM camp)
|
||||||
ORDER BY id LIMIT $2
|
ORDER BY id LIMIT $2
|
||||||
|
|
|
@ -180,16 +180,14 @@ func handleCreateSubscriber(c echo.Context) error {
|
||||||
|
|
||||||
// Insert and read ID.
|
// Insert and read ID.
|
||||||
var newID int
|
var newID int
|
||||||
err := app.Queries.UpsertSubscriber.Get(&newID,
|
err := app.Queries.InsertSubscriber.Get(&newID,
|
||||||
uuid.NewV4(),
|
uuid.NewV4(),
|
||||||
strings.ToLower(strings.TrimSpace(req.Email)),
|
strings.ToLower(strings.TrimSpace(req.Email)),
|
||||||
strings.TrimSpace(req.Name),
|
strings.TrimSpace(req.Name),
|
||||||
req.Status,
|
|
||||||
req.Attribs,
|
req.Attribs,
|
||||||
true,
|
|
||||||
req.Lists)
|
req.Lists)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == sql.ErrNoRows {
|
if pqErr, ok := err.(*pq.Error); ok && pqErr.Constraint == "subscribers_email_key" {
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "The e-mail already exists.")
|
return echo.NewHTTPError(http.StatusBadRequest, "The e-mail already exists.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue