Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions lib/erlef/admins.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ defmodule Erlef.Admins do

def resource_counts() do
q = """
select
(select count(id) from volunteers),
(select count(id) from working_groups),
select
(select count(id) from volunteers),
(select count(id) from working_groups),
(select count(id) from sponsors),
(select count(id) from events where events.approved = false),
(select count(id) from member_email_requests where member_email_requests.status != 'complete'),
(select count(id) from events where events.approved = false),
(select count(id) from apps),
(select count(id) from academic_papers where
academic_papers.deleted_at is null and academic_papers.published_at is not null),
(select count(id) from academic_papers where
academic_papers.deleted_at is null and academic_papers.published_at is not null),
(select count(id) from academic_papers where
academic_papers.deleted_at is null and academic_papers.published_at is null)
"""

Expand All @@ -48,7 +47,6 @@ defmodule Erlef.Admins do
working_group_count,
sponsors_count,
unapproved_events_count,
outstanding_email_requests_count,
apps_count,
academic_papers_count,
unapproved_academic_papers_count
Expand All @@ -61,7 +59,6 @@ defmodule Erlef.Admins do
working_groups_count: working_group_count,
sponsors_count: sponsors_count,
unapproved_events_count: unapproved_events_count,
outstanding_email_requests_count: outstanding_email_requests_count,
apps_count: apps_count,
academic_papers_count: academic_papers_count,
unapproved_academic_papers_count: unapproved_academic_papers_count
Expand Down
15 changes: 1 addition & 14 deletions lib/erlef/admins/notifications.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,13 @@ defmodule Erlef.Admins.Notifications do
import Swoosh.Email

@type notification_type() ::
:new_email_request
| :new_event_submitted
:new_event_submitted
| :new_academic_paper_submitted
| :new_slack_invite

@type params() :: map()

@spec new(notification_type(), params()) :: Swoosh.Email.t()
def new(:new_email_request, _) do
msg = """
A new email request was created. Visit https://erlef.org/admin/ to view outstanding requests.
"""

new()
|> to({"Infrastructure Requests", "infra.requests@erlef.org"})
|> from({"Erlef Notifications", "notifications@erlef.org"})
|> subject("A new email request was created")
|> text_body(msg)
end

def new(:new_event_submitted, _) do
msg = """
A new event was submitted. Visit https://erlef.org/admin/ to view unapproved events.
Expand Down
130 changes: 1 addition & 129 deletions lib/erlef/members.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,140 +3,12 @@ defmodule Erlef.Members do
Members context
"""

alias Erlef.Repo
alias Erlef.Members.{EmailRequest, EmailRequestNotification, Notifications}
alias Erlef.Members.Notifications
alias Erlef.Mailer
alias Erlef.Admins
alias Erlef.Accounts.Member

import Ecto.Query

def new_email_request(params \\ %{}) do
EmailRequest.changeset(%EmailRequest{}, params)
end

def notify(type, params) do
type
|> Notifications.new(params)
|> Mailer.deliver()
end

def create_email_request(params) do
result =
params
|> new_email_request()
|> Repo.insert()

case result do
{:ok, _} ->
Admins.notify(:new_email_request)
result

err ->
err
end
end

def get_email_request(id), do: Repo.one(EmailRequest.get(id))

def has_email_request?(member) do
case Repo.get_by(EmailRequest, submitted_by_id: member.id) do
nil -> false
_ -> true
end
end

def get_email_request_by_member(member) do
Repo.get_by(EmailRequest, submitted_by_id: member.id)
end

def update_email_request(%EmailRequest{} = req, params) do
req
|> EmailRequest.changeset(params)
|> Repo.update()
end

def update_email_request(id, params) do
case get_email_request(id) do
%EmailRequest{} = req ->
update_email_request(req, params)

nil ->
{:error, :not_found}
end
end

def complete_email_request(%{id: id, password: password}) do
with %EmailRequest{} = req <- get_email_request(id),
{:ok, member} <- update_member_fields(req),
{:ok, _req} <- update_email_request(req.id, %{status: :completed}) do
do_notify(req, %{member: member, password: password})
end
end

def complete_email_request(%{id: id}) do
with %EmailRequest{} = req <- get_email_request(id),
{:ok, member} <- update_member_fields(req),
{:ok, req} <- update_email_request(req.id, %{status: :completed}) do
do_notify(req, %{member: member})
end
end

def outstanding_email_requests() do
q =
from(p in EmailRequest,
where: p.status not in [:completed]
)

Repo.all(q)
end

def outstanding_email_requests_count() do
q =
from(p in EmailRequest,
where: p.status not in [:completed],
select: count(p.id)
)

Repo.one(q)
end

defp update_member_fields(req) do
email = "#{req.username}@erlef.org"

update =
case req.type do
:email_box ->
%{erlef_email_address: email, has_email_box?: true, has_email_alias?: false}

:email_alias ->
%{erlef_email_address: email, has_email_box?: false, has_email_alias?: true}
end

case Erlef.Accounts.get_member!(req.submitted_by_id) do
%Member{} = member ->
Erlef.Accounts.update_member(member, update, update_external: true)

err ->
err
end
end

def do_notify(%EmailRequest{type: :email_alias}, params) do
{:ok, _} =
params.member
|> EmailRequestNotification.email_alias_created()
|> Mailer.deliver()

:ok
end

def do_notify(%EmailRequest{type: :email_box}, params) do
{:ok, _} =
params.member
|> EmailRequestNotification.email_box_created(params.password)
|> Erlef.Mailer.deliver()

:ok
end
end
88 changes: 0 additions & 88 deletions lib/erlef/members/email_request.ex

This file was deleted.

53 changes: 0 additions & 53 deletions lib/erlef/members/email_request_notification.ex

This file was deleted.

Loading
Loading