Refactor/merge optin-list fetch queries

This commit is contained in:
Kailash Nadh 2020-02-09 16:25:19 +05:30
parent 6cb2cd7ea2
commit 6be3352f52
4 changed files with 23 additions and 22 deletions

View File

@ -597,9 +597,9 @@ func makeOptinCampaignMessage(o campaignReq, app *App) (campaignReq, error) {
var lists []models.List
err := app.Queries.GetListsByOptin.Select(&lists, models.ListOptinDouble, pq.Int64Array(o.ListIDs), 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,
"Error fetching optin lists.")
"Error fetching opt-in lists.")
}
// No opt-in lists.

View File

@ -136,20 +136,14 @@ func handleOptinPage(c echo.Context) error {
`One or more UUIDs in the request are invalid.`))
}
}
}
// Get lists by UUIDs.
if err := app.Queries.GetListsByOptin.Select(&out.Lists, models.ListOptinDouble, nil, pq.StringArray(out.ListUUIDs)); 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.`))
}
} 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.`))
}
// Get the list of subscription lists where the subscriber hasn't confirmed.
if err := app.Queries.GetSubscriberLists.Select(&out.Lists, 0, subUUID,
nil, pq.StringArray(out.ListUUIDs), models.SubscriptionStatusUnconfirmed, nil); err != nil {
app.Logger.Printf("error fetching lists for opt-in: %s", pqErrMsg(err))
return c.Render(http.StatusInternalServerError, "message",
makeMsgTpl("Error", "", `Error fetching lists. Please retry.`))
}
// There are no lists to confirm.

View File

@ -18,7 +18,13 @@ WITH sub AS (
SELECT * FROM lists
LEFT JOIN subscriber_lists ON (lists.id = subscriber_lists.list_id)
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
-- Get lists associations of subscribers given a list of subscriber IDs.

View File

@ -167,11 +167,10 @@ func handleCreateSubscriber(c echo.Context) error {
// Insert and read ID.
var (
newID int
email = strings.ToLower(strings.TrimSpace(req.Email))
)
req.UUID = uuid.NewV4().String()
err := app.Queries.InsertSubscriber.Get(&newID,
err := app.Queries.InsertSubscriber.Get(&req.ID,
req.UUID,
email,
strings.TrimSpace(req.Name),
@ -187,11 +186,12 @@ func handleCreateSubscriber(c echo.Context) error {
}
// 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)
// Hand over to the GET handler to return the last insertion.
c.SetParamNames("id")
c.SetParamValues(fmt.Sprintf("%d", newID))
c.SetParamValues(fmt.Sprintf("%d", req.ID))
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
// Fetch double opt-in lists from the given list IDs.
err := app.Queries.GetListsByOptin.Select(&lists, models.ListOptinDouble, pq.Int64Array(listIDs))
if err != nil {
app.Logger.Printf("error fetching lists for optin: %s", pqErrMsg(err))
// Get the list of subscription lists where the subscriber hasn't confirmed.
if err := app.Queries.GetSubscriberLists.Select(&lists, sub.ID, nil,
pq.Int64Array(listIDs), nil, models.SubscriptionStatusUnconfirmed, nil); err != nil {
app.Logger.Printf("error fetching lists for opt-in: %s", pqErrMsg(err))
return err
}