Manage monthly reports

This commit is contained in:
Uku Taht 2019-09-09 12:37:57 +01:00
parent 17e4dc96ef
commit ac267c5f69
4 changed files with 114 additions and 37 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -102,28 +102,48 @@
<div class="my-4 border-b border-grey-light"></div>
<div class="my-8 flex items-center">
<%= if @report_changeset do %>
<%= button(to: "/sites/#{@site.domain}/email-report/disable", method: :post, class: "border rounded-full border-grey flex items-center cursor-pointer w-8 bg-green justify-end") do %>
<%= if @weekly_report_changeset do %>
<%= button(to: "/sites/#{@site.domain}/weekly-report/disable", method: :post, class: "border rounded-full border-grey flex items-center cursor-pointer w-8 bg-green justify-end") do %>
<span class="rounded-full border w-4 h-4 border-grey shadow-inner bg-white shadow"></span>
<% end %>
<span class="ml-2">Receive a weekly email report every Monday</span>
<% else %>
<%= button(to: "/sites/#{@site.domain}/email-report/enable", method: :post, class: "border rounded-full border-grey flex items-center cursor-pointer w-8 justify-start") do %>
<%= button(to: "/sites/#{@site.domain}/weekly-report/enable", method: :post, class: "border rounded-full border-grey flex items-center cursor-pointer w-8 justify-start") do %>
<span class="rounded-full border w-4 h-4 border-grey shadow-inner bg-white shadow"></span>
<% end %>
<span class="ml-2">Receive a weekly email report every Monday</span>
<% end %>
<span class="ml-2">Receive a weekly email report every Monday</span>
</div>
<%= if @report_changeset do %>
<div class="my-4 border-b border-grey-light"></div>
<%= if @weekly_report_changeset do %>
<div class="text-sm text-grey-darker mt-6">
<%= form_for @report_changeset, "/sites/#{@site.domain}/email-report", [class: "max-w-xs"], fn f -> %>
<div class="my-4">
<%= label f, :email, "Email address", class: "block text-grey-darker text-sm font-bold mb-2" %>
<%= email_input f, :email, class: "transition bg-grey-lighter appearance-none border border-transparent rounded w-full p-2 text-grey-darker leading-normal appearance-none focus:outline-none focus:border-grey-light" %>
<%= error_tag f, :email %>
<%= form_for @weekly_report_changeset, "/sites/#{@site.domain}/weekly-report", [class: "max-w-xs"], fn f -> %>
<%= label f, :email, "Email address", class: "block text-grey-darker text-sm font-bold mb-2" %>
<div class="flex">
<%= email_input f, :email, class: "transition bg-grey-lighter appearance-none border border-transparent rounded w-full p-2 text-grey-darker leading-normal appearance-none focus:outline-none focus:border-grey-light", style: "flex-grow: 2" %>
<%= submit "Update", class: "button rounded-l-none" %>
</div>
<% end %>
</div>
<% end %>
<div class="my-4 border-b border-grey-light"></div>
<div class="my-8 flex items-center">
<%= if @monthly_report_changeset do %>
<%= button(to: "/sites/#{@site.domain}/monthly-report/disable", method: :post, class: "border rounded-full border-grey flex items-center cursor-pointer w-8 bg-green justify-end") do %>
<span class="rounded-full border w-4 h-4 border-grey shadow-inner bg-white shadow"></span>
<% end %>
<% else %>
<%= button(to: "/sites/#{@site.domain}/monthly-report/enable", method: :post, class: "border rounded-full border-grey flex items-center cursor-pointer w-8 justify-start") do %>
<span class="rounded-full border w-4 h-4 border-grey shadow-inner bg-white shadow"></span>
<% end %>
<% end %>
<span class="ml-2">Receive a monthly email report on 1st of the month</span>
</div>
<%= if @monthly_report_changeset do %>
<div class="text-sm text-grey-darker mt-6">
<%= form_for @monthly_report_changeset, "/sites/#{@site.domain}/monthly-report", [class: "max-w-xs"], fn f -> %>
<div class="flex">
<%= email_input f, :email, class: "transition bg-grey-lighter appearance-none border border-transparent rounded w-full p-2 text-grey-darker leading-normal appearance-none focus:outline-none focus:border-grey-light", style: "flex-grow: 2" %>
<%= submit "Update", class: "button rounded-l-none" %>
</div>
<%= submit "Set email address", class: "button mt-2" %>
<% end %>
</div>
<% end %>