Skip to content

Commit

Permalink
Add support for handling academic papers and for preview cards (#240 #…
Browse files Browse the repository at this point in the history
…459) (#460)

Co-authored-by: abdelghani <[email protected]>
  • Loading branch information
MOQA-Solutions and abdelghani authored Dec 16, 2023
1 parent 68fb755 commit 95e3704
Show file tree
Hide file tree
Showing 34 changed files with 906 additions and 121 deletions.
67 changes: 67 additions & 0 deletions lib/erlef/academic_papers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
defmodule Erlef.AcademicPapers do
@moduledoc """
Context responsible for managing Academic Papers
"""

alias Erlef.AcademicPapers.Query
alias Erlef.AcademicPapers.AcademicPaper

@spec all() :: [AcademicPaper.t()]
def all(), do: Query.published()

@spec get(Ecto.UUID.t()) :: AcademicPaper.t()
def get(id), do: Query.get(id)

@spec create_academic_paper(params :: map()) ::
{:ok, AcademicPaper.t()} | {:error, Ecto.Changeset.t()}
def create_academic_paper(params) do
new_params = normalize_params(params)

%AcademicPaper{}
|> change_academic_paper(new_params)
|> Query.create()
end

@spec update_academic_paper(academic_paper :: AcademicPaper.t(), params :: map()) ::
{:ok, AcademicPaper.t()} | {:error, Ecto.Changeset.t()}
def update_academic_paper(%AcademicPaper{} = academic_paper, params) do
new_params = normalize_params(params)

academic_paper
|> change_academic_paper(new_params)
|> Query.update()
end

@spec delete_academic_paper(academic_paper :: AcademicPaper.t()) ::
{:ok, AcademicPaper.t()} | {:error, Ecto.Changeset.t()}
def delete_academic_paper(%AcademicPaper{} = academic_paper) do
academic_paper
|> change_academic_paper(%{deleted_at: DateTime.utc_now()})
|> Query.update()
end

@spec change_academic_paper(academic_paper :: Ecto.Schema.t(), params :: map()) ::
Ecto.Changeset.t()
def change_academic_paper(%AcademicPaper{} = academic_paper, attrs \\ %{}),
do: AcademicPaper.changeset(academic_paper, attrs)

@spec unapproved_all() :: [AcademicPaper.t()]
def unapproved_all(), do: Query.unpublished()

@spec approve_academic_paper(academic_paper :: AcademicPaper.t(), params :: map()) ::
{:ok, AcademicPaper.t()} | {:error, Ecto.Changeset.t()}
def approve_academic_paper(%AcademicPaper{} = unapproved_academic_paper, params) do
params0 = normalize_params(params)
new_params = Map.put(params0, "published_at", DateTime.utc_now())

unapproved_academic_paper
|> change_academic_paper(new_params)
|> Query.update()
end

# Private Internal Functions

defp normalize_params(%{"technologies" => _val} = params), do: params

defp normalize_params(params), do: Map.put(params, "technologies", "")
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Erlef.Publications.AcademicPaper do
defmodule Erlef.AcademicPapers.AcademicPaper do
@moduledoc """
Schema module for academic papers
"""
Expand All @@ -12,8 +12,10 @@ defmodule Erlef.Publications.AcademicPaper do
url: String.t(),
pay_wall?: :boolean | nil,
description: String.t() | nil,
technologies: {:array, String.t()} | nil,
technologies: {:array, String.t()},
original_publish_date: Date.t() | nil,
submitted_by: Ecto.UUID.t() | nil,
approved_by: Ecto.UUID.t() | nil,
published_at: :utc_datetime | nil,
deleted_at: :utc_datetime | nil
}
Expand All @@ -23,13 +25,15 @@ defmodule Erlef.Publications.AcademicPaper do
author
language
url
technologies
|a

@optional_fields ~w|
pay_wall?
description
technologies
original_publish_date
submitted_by
approved_by
published_at
deleted_at
|a
Expand All @@ -45,6 +49,8 @@ defmodule Erlef.Publications.AcademicPaper do
field(:description, :string)
field(:technologies, {:array, :string})
field(:original_publish_date, :date)
field(:submitted_by, :binary_id)
field(:approved_by, :binary_id)

field(:published_at, :utc_datetime)
field(:deleted_at, :utc_datetime)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
defmodule Erlef.Publications.Query do
defmodule Erlef.AcademicPapers.Query do
@moduledoc """
Module for the academic paper queries
"""

import Ecto.Query, only: [from: 2]

alias Erlef.Publications.AcademicPaper
alias Erlef.AcademicPapers.AcademicPaper
alias Erlef.Repo

@doc """
Expand All @@ -21,15 +21,15 @@ defmodule Erlef.Publications.Query do
end

@doc """
Returns a academic paper by id. Used for admin functionality
Returns an academic paper by id. Used for admin functionality
"""
@spec get(id :: binary(), repo :: Ecto.Repo.t()) :: AcademicPaper.t() | nil
def get(id, repo \\ Repo) do
from(p in AcademicPaper,
where: is_nil(p.deleted_at),
where: p.id == ^id
)
|> repo.one()
|> repo.one
end

@doc """
Expand Down Expand Up @@ -61,62 +61,16 @@ defmodule Erlef.Publications.Query do
@doc """
Creates a new academic paper. Used for admin functionality
"""
@spec create(params :: map(), repo :: Ecto.Repo.t()) ::
@spec create(changeset :: Ecto.Changeset.t(), repo :: Ecto.Repo.t()) ::
{:ok, AcademicPaper.t()} | {:error, Ecto.Changeset.t()}
def create(params, repo \\ Repo) do
%AcademicPaper{}
|> AcademicPaper.changeset(params)
|> case do
%Ecto.Changeset{valid?: true} = changeset ->
repo.insert(changeset)

changeset ->
{:error, changeset}
end
end
def create(%Ecto.Changeset{} = changeset, repo \\ Repo), do: repo.insert(changeset)

@doc """
Updates an existing academic paper, Used for admin functionality. To make sure that data is not changed with a delete
use the delete/0 function for deletes.
"""
@spec update(academic_paper :: AcademicPaper.t(), params :: map(), repo :: Ecto.Repo.t()) ::
@spec update(changeset :: Ecto.Changeset.t(), repo :: Ecto.Repo.t()) ::
{:ok, AcademicPaper.t()} | {:error, Ecto.Changeset.t()}
def update(academic_paper, params, repo \\ Repo)

def update(_, %{deleted_at: _}, _),
do: {:error, {:invalid_operation, "use delete/1 for the delete operation"}}

def update(_, %{"deleted_at" => _}, _),
do: {:error, {:invalid_operation, "use delete/1 for the delete operation"}}

def update(%AcademicPaper{} = original, params, repo) when is_map(params) do
original
|> AcademicPaper.changeset(params)
|> case do
%Ecto.Changeset{valid?: true} = changeset ->
repo.update(changeset)

changeset ->
{:error, changeset}
end
end

@doc """
Deletes an academic paper. This is a logical delete
"""
@spec delete(academic_paper :: AcademicPaper.t(), repo :: Ecto.Repo.t()) ::
{:ok, AcademicPaper.t()} | {:error, :no_record_found}
def delete(%AcademicPaper{id: id}, repo \\ Repo) do
id
|> get(repo)
|> case do
%AcademicPaper{} = academic_paper ->
academic_paper
|> AcademicPaper.changeset(%{deleted_at: DateTime.utc_now()})
|> repo.update()

nil ->
{:error, :no_record_found}
end
end
def update(%Ecto.Changeset{} = changeset, repo \\ Repo), do: repo.update(changeset)
end
14 changes: 11 additions & 3 deletions lib/erlef/admins.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ defmodule Erlef.Admins do
(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 apps)
(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 null)
"""

%{
Expand All @@ -45,7 +49,9 @@ defmodule Erlef.Admins do
sponsors_count,
unapproved_events_count,
outstanding_email_requests_count,
apps_count
apps_count,
academic_papers_count,
unapproved_academic_papers_count
]
]
} = Ecto.Adapters.SQL.query!(Erlef.Repo, q)
Expand All @@ -56,7 +62,9 @@ defmodule Erlef.Admins do
sponsors_count: sponsors_count,
unapproved_events_count: unapproved_events_count,
outstanding_email_requests_count: outstanding_email_requests_count,
apps_count: apps_count
apps_count: apps_count,
academic_papers_count: academic_papers_count,
unapproved_academic_papers_count: unapproved_academic_papers_count
}
end
end
18 changes: 17 additions & 1 deletion lib/erlef/admins/notifications.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ defmodule Erlef.Admins.Notifications do

import Swoosh.Email

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

@type params() :: map()

Expand Down Expand Up @@ -31,4 +35,16 @@ defmodule Erlef.Admins.Notifications do
|> subject("A new event was submitted")
|> text_body(msg)
end

def new(:new_academic_paper_submitted, _) do
msg = """
A new academic paper was submitted, Visit https://erlef.org/admin/unapproved_academic_papers/ to view unapproved academic papers
"""

new()
|> to({"Website Admins", "[email protected]"})
|> from({"Erlef Notifications", "[email protected]"})
|> subject("A new academic paper was submitted")
|> text_body(msg)
end
end
44 changes: 43 additions & 1 deletion lib/erlef/members/notifications.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ defmodule Erlef.Members.Notifications do

import Swoosh.Email

@type notification_type() :: :new_event_submitted | :new_event_approved
@type notification_type() ::
:new_event_submitted
| :new_academic_paper_submitted
| :new_event_approved
| :new_academic_paper_approved

@type params() :: map()

Expand All @@ -26,6 +30,25 @@ defmodule Erlef.Members.Notifications do
|> text_body(msg)
end

def new(:new_academic_paper_submitted, %{member: member}) do
msg = """
Thanks for submitting a new academic paper, we appreciate your involvement in our community.
An admin will approve the paper for display shortly. Once approved you will get an email stating so.
If you haven't seen an approval for this academic paper in 48 hours please send an email to [email protected].
Thanks
The Erlang Ecosystem Foundation
"""

new()
|> to({member.name, member.email})
|> from({"Erlef Notifications", "[email protected]"})
|> subject("You submitted a new academic paper")
|> text_body(msg)
end

def new(:new_event_approved, %{member: member}) do
msg = """
An event you submitted has been approved and is now published on the site.
Expand All @@ -44,4 +67,23 @@ defmodule Erlef.Members.Notifications do
|> subject("Your submitted event has been approved!")
|> text_body(msg)
end

def new(:new_academic_paper_approved, %{member: member}) do
msg = """
An academic paper you submitted has been approved and is now published on the site.
Once again, we appreciate your involvement with our community and hope to see more academic papers
submissions from you in the future!
Thanks
The Erlang Ecosystem Foundation
"""

new()
|> to({member.name, member.email})
|> from({"Erlef Notifications", "[email protected]"})
|> subject("Your submitted academic paper has been approved!")
|> text_body(msg)
end
end
9 changes: 0 additions & 9 deletions lib/erlef/publications.ex

This file was deleted.

32 changes: 30 additions & 2 deletions lib/erlef_web/controllers/academic_paper_controller.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
defmodule ErlefWeb.AcademicPaperController do
use ErlefWeb, :controller

alias Erlef.Publications
alias Erlef.AcademicPapers
alias Erlef.AcademicPapers.AcademicPaper
alias Erlef.{Accounts, Admins, Members}

action_fallback ErlefWeb.FallbackController

def index(conn, _params) do
render(conn, :index, academic_papers: Publications.all())
render(conn, :index, academic_papers: AcademicPapers.all())
end

def new(conn, _params) do
changeset = AcademicPapers.change_academic_paper(%AcademicPaper{})
render(conn, :new, changeset: changeset)
end

def create(conn, %{"academic_paper" => params}) do
submitter_id = conn.assigns.current_user.id
new_params = Map.put(params, "submitted_by", submitter_id)

case AcademicPapers.create_academic_paper(new_params) do
{:ok, _academic_paper} ->
type = :new_academic_paper_submitted
submitter = Accounts.get_member(submitter_id)
opts = %{member: submitter}
{:ok, _notify} = Admins.notify(type, opts)
{:ok, _notify} = Members.notify(type, opts)

conn
|> put_flash(:info, "Paper Submitted and has to be Approved")
|> redirect(to: "/")

{:error, changeset} ->
render(conn, :new, changeset: changeset)
end
end
end
Loading

0 comments on commit 95e3704

Please sign in to comment.