Refactor/merge optin-list fetch queries
This commit is contained in:
parent
6cb2cd7ea2
commit
6be3352f52
4 changed files with 23 additions and 22 deletions
|
@ -597,9 +597,9 @@ func makeOptinCampaignMessage(o campaignReq, app *App) (campaignReq, error) {
|
||||||
var lists []models.List
|
var lists []models.List
|
||||||
err := app.Queries.GetListsByOptin.Select(&lists, models.ListOptinDouble, pq.Int64Array(o.ListIDs), nil)
|
err := app.Queries.GetListsByOptin.Select(&lists, models.ListOptinDouble, pq.Int64Array(o.ListIDs), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Printf("error fetching lists for optin: %s", pqErrMsg(err))
|
app.Logger.Printf("error fetching lists for opt-in: %s", pqErrMsg(err))
|
||||||
return o, echo.NewHTTPError(http.StatusInternalServerError,
|
return o, echo.NewHTTPError(http.StatusInternalServerError,
|
||||||
"Error fetching optin lists.")
|
"Error fetching opt-in lists.")
|
||||||
}
|
}
|
||||||
|
|
||||||
// No opt-in lists.
|
// No opt-in lists.
|
||||||
|
|
20
public.go
20
public.go
|
@ -136,20 +136,14 @@ func handleOptinPage(c echo.Context) error {
|
||||||
`One or more UUIDs in the request are invalid.`))
|
`One or more UUIDs in the request are invalid.`))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Get lists by UUIDs.
|
// Get the list of subscription lists where the subscriber hasn't confirmed.
|
||||||
if err := app.Queries.GetListsByOptin.Select(&out.Lists, models.ListOptinDouble, nil, pq.StringArray(out.ListUUIDs)); err != nil {
|
if err := app.Queries.GetSubscriberLists.Select(&out.Lists, 0, subUUID,
|
||||||
app.Logger.Printf("error fetching lists for optin: %s", pqErrMsg(err))
|
nil, pq.StringArray(out.ListUUIDs), models.SubscriptionStatusUnconfirmed, nil); err != nil {
|
||||||
return c.Render(http.StatusInternalServerError, "message",
|
app.Logger.Printf("error fetching lists for opt-in: %s", pqErrMsg(err))
|
||||||
makeMsgTpl("Error", "", `Error fetching lists. Please retry.`))
|
return c.Render(http.StatusInternalServerError, "message",
|
||||||
}
|
makeMsgTpl("Error", "", `Error fetching lists. Please retry.`))
|
||||||
} else {
|
|
||||||
// Otherwise, get the list of all unconfirmed lists for the subscriber.
|
|
||||||
if err := app.Queries.GetSubscriberLists.Select(&out.Lists, 0, subUUID, models.SubscriptionStatusUnconfirmed); err != nil {
|
|
||||||
app.Logger.Printf("error fetching lists for optin: %s", pqErrMsg(err))
|
|
||||||
return c.Render(http.StatusInternalServerError, "message",
|
|
||||||
makeMsgTpl("Error", "", `Error fetching lists. Please retry.`))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// There are no lists to confirm.
|
// There are no lists to confirm.
|
||||||
|
|
|
@ -18,7 +18,13 @@ WITH sub AS (
|
||||||
SELECT * FROM lists
|
SELECT * FROM lists
|
||||||
LEFT JOIN subscriber_lists ON (lists.id = subscriber_lists.list_id)
|
LEFT JOIN subscriber_lists ON (lists.id = subscriber_lists.list_id)
|
||||||
WHERE subscriber_id = (SELECT id FROM sub)
|
WHERE subscriber_id = (SELECT id FROM sub)
|
||||||
AND (CASE WHEN $3 != '' THEN subscriber_lists.status = $3::subscription_status END);
|
-- Optional list IDs or UUIDs to filter.
|
||||||
|
AND (CASE WHEN $3::INT[] IS NOT NULL THEN id = ANY($3::INT[])
|
||||||
|
WHEN $4::UUID[] IS NOT NULL THEN uuid = ANY($4::UUID[])
|
||||||
|
ELSE TRUE
|
||||||
|
END)
|
||||||
|
AND (CASE WHEN $5 != '' THEN subscriber_lists.status = $5::subscription_status END)
|
||||||
|
AND (CASE WHEN $6 != '' THEN lists.optin = $6::list_optin ELSE TRUE END);
|
||||||
|
|
||||||
-- name: get-subscriber-lists-lazy
|
-- name: get-subscriber-lists-lazy
|
||||||
-- Get lists associations of subscribers given a list of subscriber IDs.
|
-- Get lists associations of subscribers given a list of subscriber IDs.
|
||||||
|
|
|
@ -167,11 +167,10 @@ func handleCreateSubscriber(c echo.Context) error {
|
||||||
|
|
||||||
// Insert and read ID.
|
// Insert and read ID.
|
||||||
var (
|
var (
|
||||||
newID int
|
|
||||||
email = strings.ToLower(strings.TrimSpace(req.Email))
|
email = strings.ToLower(strings.TrimSpace(req.Email))
|
||||||
)
|
)
|
||||||
req.UUID = uuid.NewV4().String()
|
req.UUID = uuid.NewV4().String()
|
||||||
err := app.Queries.InsertSubscriber.Get(&newID,
|
err := app.Queries.InsertSubscriber.Get(&req.ID,
|
||||||
req.UUID,
|
req.UUID,
|
||||||
email,
|
email,
|
||||||
strings.TrimSpace(req.Name),
|
strings.TrimSpace(req.Name),
|
||||||
|
@ -187,11 +186,12 @@ func handleCreateSubscriber(c echo.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the lists are double-optins, send confirmation e-mails.
|
// If the lists are double-optins, send confirmation e-mails.
|
||||||
|
// Todo: This arbitrary goroutine should be moved to a centralised pool.
|
||||||
go sendOptinConfirmation(req.Subscriber, []int64(req.Lists), app)
|
go sendOptinConfirmation(req.Subscriber, []int64(req.Lists), app)
|
||||||
|
|
||||||
// Hand over to the GET handler to return the last insertion.
|
// Hand over to the GET handler to return the last insertion.
|
||||||
c.SetParamNames("id")
|
c.SetParamNames("id")
|
||||||
c.SetParamValues(fmt.Sprintf("%d", newID))
|
c.SetParamValues(fmt.Sprintf("%d", req.ID))
|
||||||
return c.JSON(http.StatusOK, handleGetSubscriber(c))
|
return c.JSON(http.StatusOK, handleGetSubscriber(c))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -524,9 +524,10 @@ func sendOptinConfirmation(sub models.Subscriber, listIDs []int64, app *App) err
|
||||||
var lists []models.List
|
var lists []models.List
|
||||||
|
|
||||||
// Fetch double opt-in lists from the given list IDs.
|
// Fetch double opt-in lists from the given list IDs.
|
||||||
err := app.Queries.GetListsByOptin.Select(&lists, models.ListOptinDouble, pq.Int64Array(listIDs))
|
// Get the list of subscription lists where the subscriber hasn't confirmed.
|
||||||
if err != nil {
|
if err := app.Queries.GetSubscriberLists.Select(&lists, sub.ID, nil,
|
||||||
app.Logger.Printf("error fetching lists for optin: %s", pqErrMsg(err))
|
pq.Int64Array(listIDs), nil, models.SubscriptionStatusUnconfirmed, nil); err != nil {
|
||||||
|
app.Logger.Printf("error fetching lists for opt-in: %s", pqErrMsg(err))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue