diff --git a/lib/plausible/site/monthly_report.ex b/lib/plausible/site/monthly_report.ex new file mode 100644 index 000000000..e98fd91be --- /dev/null +++ b/lib/plausible/site/monthly_report.ex @@ -0,0 +1,20 @@ +defmodule Plausible.Site.MonthlyReport do + use Ecto.Schema + import Ecto.Changeset + @mail_regex ~r/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/ + + schema "monthly_reports" do + field :email, :string + belongs_to :site, Plausible.Site + + timestamps() + end + + def changeset(settings, attrs \\ %{}) do + settings + |> cast(attrs, [:site_id, :email]) + |> validate_required([:site_id, :email]) + |> validate_format(:email, @mail_regex) + |> unique_constraint(:site) + end +end diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex index 1636525b8..04eb6feae 100644 --- a/lib/plausible_web/controllers/site_controller.ex +++ b/lib/plausible_web/controllers/site_controller.ex @@ -42,33 +42,22 @@ defmodule PlausibleWeb.SiteController do !google_site["error"] end - report = Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id) - report_changeset = report && Plausible.Site.WeeklyReport.changeset(report, %{}) - - changeset = Plausible.Site.changeset(site, %{}) + weekly_report = Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id) + weekly_report_changeset = weekly_report && Plausible.Site.WeeklyReport.changeset(weekly_report, %{}) + monthly_report = Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id) + monthly_report_changeset = monthly_report && Plausible.Site.WeeklyReport.changeset(monthly_report, %{}) conn |> assign(:skip_plausible_tracking, true) |> render("settings.html", site: site, - report_changeset: report_changeset, + weekly_report_changeset: weekly_report_changeset, + monthly_report_changeset: monthly_report_changeset, google_search_console_verified: google_search_console_verified, - changeset: changeset + changeset: Plausible.Site.changeset(site, %{}) ) end - - def update_email_settings(conn, %{"website" => website, "weekly_report" => weekly_report}) do - site = Sites.get_for_user!(conn.assigns[:current_user].id, website) - Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id) - |> Plausible.Site.WeeklyReport.changeset(weekly_report) - |> Repo.update! - - conn - |> put_flash(:success, "Email address saved succesfully") - |> redirect(to: "/#{site.domain}/settings") - end - def update_settings(conn, %{"website" => website, "site" => site_params}) do site = Sites.get_for_user!(conn.assigns[:current_user].id, website) changeset = site |> Plausible.Site.changeset(site_params) @@ -122,7 +111,7 @@ defmodule PlausibleWeb.SiteController do |> redirect(to: "/" <> site.domain <> "/settings") end - def enable_email_report(conn, %{"website" => website}) do + def enable_weekly_report(conn, %{"website" => website}) do site = Sites.get_for_user!(conn.assigns[:current_user].id, website) Plausible.Site.WeeklyReport.changeset(%Plausible.Site.WeeklyReport{}, %{ @@ -136,15 +125,60 @@ defmodule PlausibleWeb.SiteController do |> redirect(to: "/" <> site.domain <> "/settings") end - def disable_email_report(conn, %{"website" => website}) do + def disable_weekly_report(conn, %{"website" => website}) do site = Sites.get_for_user!(conn.assigns[:current_user].id, website) - Repo.delete_all(from es in Plausible.Site.EmailSettings, where: es.site_id == ^site.id) + Repo.delete_all(from wr in Plausible.Site.WeeklyReport, where: wr.site_id == ^site.id) conn |> put_flash(:success, "Success! You will not receive weekly email reports going forward") |> redirect(to: "/" <> site.domain <> "/settings") end + def update_weekly_settings(conn, %{"website" => website, "weekly_report" => weekly_report}) do + site = Sites.get_for_user!(conn.assigns[:current_user].id, website) + Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id) + |> Plausible.Site.WeeklyReport.changeset(weekly_report) + |> Repo.update! + + conn + |> put_flash(:success, "Email address saved succesfully") + |> redirect(to: "/#{site.domain}/settings") + end + + def enable_monthly_report(conn, %{"website" => website}) do + site = Sites.get_for_user!(conn.assigns[:current_user].id, website) + + Plausible.Site.MonthlyReport.changeset(%Plausible.Site.MonthlyReport{}, %{ + site_id: site.id, + email: conn.assigns[:current_user].email + }) + |> Repo.insert! + + conn + |> put_flash(:success, "Success! You will receive an email report every month going forward") + |> redirect(to: "/" <> site.domain <> "/settings") + end + + def disable_monthly_report(conn, %{"website" => website}) do + site = Sites.get_for_user!(conn.assigns[:current_user].id, website) + Repo.delete_all(from mr in Plausible.Site.MonthlyReport, where: mr.site_id == ^site.id) + + conn + |> put_flash(:success, "Success! You will not receive monthly email reports going forward") + |> redirect(to: "/" <> site.domain <> "/settings") + end + + def update_monthly_settings(conn, %{"website" => website, "monthly_report" => monthly_report}) do + site = Sites.get_for_user!(conn.assigns[:current_user].id, website) + Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id) + |> Plausible.Site.WeeklyReport.changeset(monthly_report) + |> Repo.update! + + conn + |> put_flash(:success, "Email address saved succesfully") + |> redirect(to: "/#{site.domain}/settings") + end + defp insert_site(user_id, params) do site_changeset = Plausible.Site.changeset(%Plausible.Site{}, params) diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex index bf03f0cca..9195f029a 100644 --- a/lib/plausible_web/router.ex +++ b/lib/plausible_web/router.ex @@ -85,9 +85,12 @@ defmodule PlausibleWeb.Router do post "/sites", SiteController, :create_site post "/sites/:website/make-public", SiteController, :make_public post "/sites/:website/make-private", SiteController, :make_private - post "/sites/:website/email-report/enable", SiteController, :enable_email_report - post "/sites/:website/email-report/disable", SiteController, :disable_email_report - put "/sites/:website/email-report", SiteController, :update_email_settings + post "/sites/:website/weekly-report/enable", SiteController, :enable_weekly_report + post "/sites/:website/weekly-report/disable", SiteController, :disable_weekly_report + put "/sites/:website/weekly-report", SiteController, :update_weekly_settings + post "/sites/:website/monthly-report/enable", SiteController, :enable_monthly_report + post "/sites/:website/monthly-report/disable", SiteController, :disable_monthly_report + put "/sites/:website/monthly-report", SiteController, :update_monthly_settings get "/:website/snippet", SiteController, :add_snippet get "/:website/settings", SiteController, :settings put "/:website/settings", SiteController, :update_settings diff --git a/lib/plausible_web/templates/site/settings.html.eex b/lib/plausible_web/templates/site/settings.html.eex index 40ea88adf..43a1108ac 100644 --- a/lib/plausible_web/templates/site/settings.html.eex +++ b/lib/plausible_web/templates/site/settings.html.eex @@ -102,28 +102,48 @@