Add presence check when unsubscribing from email reports

pull/994/head
Uku Taht 6 years ago
parent cc2a34758c
commit d30f6def0f

@ -5,10 +5,13 @@ defmodule PlausibleWeb.UnsubscribeController do
def weekly_report(conn, %{"website" => website, "email" => email}) do
site = Repo.get_by(Plausible.Site, domain: website)
weekly_report = site && Repo.get_by(WeeklyReport, site_id: site.id)
Repo.get_by(WeeklyReport, site_id: site.id)
|> WeeklyReport.remove_recipient(email)
|> Repo.update!()
if weekly_report do
weekly_report
|> WeeklyReport.remove_recipient(email)
|> Repo.update!()
end
conn
|> assign(:skip_plausible_tracking, true)
@ -21,10 +24,13 @@ defmodule PlausibleWeb.UnsubscribeController do
def monthly_report(conn, %{"website" => website, "email" => email}) do
site = Repo.get_by(Plausible.Site, domain: website)
monthly_report = site && Repo.get_by(MonthlyReport, site_id: site.id)
Repo.get_by(MonthlyReport, site_id: site.id)
|> MonthlyReport.remove_recipient(email)
|> Repo.update!()
if monthly_report do
monthly_report
|> MonthlyReport.remove_recipient(email)
|> Repo.update!()
end
conn
|> assign(:skip_plausible_tracking, true)

@ -15,6 +15,13 @@ defmodule PlausibleWeb.UnsubscribeControllerTest do
report = Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id)
assert report.recipients == []
end
test "renders success if site or weekly report does not exist in the database", %{conn: conn} do
conn =
get(conn, "/sites/nonexistent.com/weekly-report/unsubscribe?email=recipient@email.com")
assert html_response(conn, 200) =~ "Unsubscribe successful"
end
end
describe "GET /sites/:website/monthly-report/unsubscribe" do
@ -30,5 +37,12 @@ defmodule PlausibleWeb.UnsubscribeControllerTest do
report = Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id)
assert report.recipients == []
end
test "renders success if site or weekly report does not exist in the database", %{conn: conn} do
conn =
get(conn, "/sites/nonexistent.com/monthly-report/unsubscribe?email=recipient@email.com")
assert html_response(conn, 200) =~ "Unsubscribe successful"
end
end
end

Loading…
Cancel
Save