Skip to content

Commit

Permalink
Merge pull request #72 from datasektionen/live-applications
Browse files Browse the repository at this point in the history
Live applications
  • Loading branch information
adriansalamon authored Sep 15, 2023
2 parents 8ee8ad8 + 34131b6 commit b1845a5
Show file tree
Hide file tree
Showing 38 changed files with 1,005 additions and 430 deletions.
3 changes: 2 additions & 1 deletion config/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export IMGPROXY_SALT=VERY_SECRET_IMGPROXY_SALT
export AWS_ACCESS_KEY_ID=VERY_SECRET_AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=VERY_SECRET_AWS_SECRET_ACCESS_KEY
export API_LOGIN_SECRET=VERY_SECRET_AND_LONG_API_SECRET
export ZFINGER_URL=zfinger.datasektionen.se
export ZFINGER_URL=zfinger.datasektionen.se
export SPAM_API_KEY=VERY_SECRET_SPAM_API_KEY
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ config :haj, Haj.Repo,
config :haj,
login_api_key: System.get_env("LOGIN_API_KEY"),
login_host: System.get_env("LOGIN_HOST"),
spam_api_key: System.get_env("SPAM_API_KEY"),
hostname: "localhost.datasektionen.se",
port: 4001,
api_login_secret: System.get_env("API_LOGIN_SECRET"),
zfinger_url: System.get_env("ZFINGER_URL")


config :imgproxy,
key: System.get_env("IMGPROXY_KEY"),
salt: System.get_env("IMGPROXY_SALT")
Expand Down
4 changes: 3 additions & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ if config_env() == :prod || config_env() == :staging do
login_host = System.get_env("LOGIN_HOST") || raise "LOGIN_HOST is missing"
api_login_secret = System.get_env("API_LOGIN_SECRET") || raise "API_LOGIN_SECRET is missing"
zfinger_url = System.get_env("ZFINGER_URL") || "zfinger.datasektionen.se"
spam_api_key = System.get_env("SPAM_API_KEY") || raise "SPAM_API_KEY is missing"

config :haj,
login_api_key: login_api_key,
login_host: login_host,
api_login_secret: api_login_secret,
zfinger_url: zfinger_url
zfinger_url: zfinger_url,
spam_api_key: spam_api_key

# Variables for imgproxy
imgproxy_key = System.get_env("IMGPROXY_KEY") || raise "IMGPROXY_KEY is missing"
Expand Down
9 changes: 9 additions & 0 deletions lib/haj/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,13 @@ defmodule Haj.Accounts.User do
message: "Får ej vara tomt."
)
end

@doc false
def application_changeset(user, attrs \\ %{}) do
changeset(user, attrs)
|> validate_required([:class, :phone], message: "Får ej vara tomt.")
|> validate_change(:username, fn :username, _ ->
[username: "Du kan inte ändra KTH-id!"]
end)
end
end
143 changes: 120 additions & 23 deletions lib/haj/applications.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ defmodule Haj.Applications do
"""

import Ecto.Query, warn: false
alias Haj.Spex.ShowGroup
alias Haj.Spex
alias Haj.Repo

alias Haj.Applications.Application
alias Haj.Applications.Application, as: App
alias Haj.Applications.ApplicationShowGroup

@doc """
Expand All @@ -19,7 +21,7 @@ defmodule Haj.Applications do
"""
def list_applications do
Repo.all(Application)
Repo.all(App)
end

@doc """
Expand All @@ -36,33 +38,30 @@ defmodule Haj.Applications do
** (Ecto.NoResultsError)
"""
def get_application!(id), do: Repo.get!(Application, id)
def get_application!(id), do: Repo.get!(App, id)

@doc """
Creates an application. Takes a list of show, groups, user id and a show id.
If there is already an application for that show for that user, replaces and creates a new applicaiton.
"""
def create_application(
%{"show_groups" => show_groups, "user_id" => user_id, "show_id" => show_id} = attrs \\ %{}
) do
def create_application(user_id, show_id, show_groups) do
Repo.transaction(fn ->
previous =
Repo.one(from a in Application, where: a.show_id == ^show_id and a.user_id == ^user_id)
Repo.one(from a in App, where: a.show_id == ^show_id and a.user_id == ^user_id)

if previous != nil do
{:ok, _} = Repo.delete(previous)
Repo.delete(previous)
# Repo.rollback(:already_applied)
end

{:ok, application} =
%Application{}
|> Application.changeset(attrs)
%App{user_id: user_id, show_id: show_id}
|> Repo.insert()

Enum.each(show_groups, fn %{id: id, special_text: text} ->
Enum.each(show_groups, fn id ->
Repo.insert(%ApplicationShowGroup{
application_id: application.id,
show_group_id: id,
special_text: text
show_group_id: id
})
end)
end)
Expand All @@ -80,10 +79,18 @@ defmodule Haj.Applications do
{:error, %Ecto.Changeset{}}
"""
def update_application(%Application{} = application, attrs) do
application
|> Application.changeset(attrs)
|> Repo.update()
def update_application(%App{} = application, attrs, options \\ []) do
case Keyword.get(options, :with_show_groups, false) do
true ->
application
|> App.changeset_with_show_groups(attrs)
|> Repo.update()

false ->
application
|> App.changeset(attrs)
|> Repo.update()
end
end

@doc """
Expand All @@ -98,7 +105,7 @@ defmodule Haj.Applications do
{:error, %Ecto.Changeset{}}
"""
def delete_application(%Application{} = application) do
def delete_application(%App{} = application) do
Repo.delete(application)
end

Expand All @@ -111,16 +118,16 @@ defmodule Haj.Applications do
%Ecto.Changeset{data: %Application{}}
"""
def change_application(%Application{} = application, attrs \\ %{}) do
Application.changeset(application, attrs)
def change_application(%App{} = application, attrs \\ %{}) do
App.changeset(application, attrs)
end

@doc """
Returns a list of applications for a show group.
"""
def get_applications_for_show_group(show_group_id) do
query =
from a in Application,
from a in App,
join: asg in assoc(a, :application_show_groups),
where: asg.application_id == a.id,
where: asg.show_group_id == ^show_group_id,
Expand All @@ -134,22 +141,112 @@ defmodule Haj.Applications do
"""
def get_applications_for_user(user_id) do
query =
from a in Application,
from a in App,
where: a.user_id == ^user_id,
preload: [application_show_groups: []]

Repo.all(query)
end

@doc """
Returns an application for a user for the current show.
"""
def get_current_application_for_user(user_id) do
query =
from a in App,
where: a.user_id == ^user_id and a.show_id == ^Spex.current_spex().id,
preload: [application_show_groups: []]

Repo.one(query)
end

@doc """
Returns a list of show groups for an application.
"""
def get_show_groups_for_application(application_id) do
query =
from sg in ShowGroup,
join: asg in assoc(sg, :application_show_groups),
where: asg.application_id == ^application_id,
preload: :group

Repo.all(query)
end

@doc """
Returns all application for a show.
"""
def list_applications_for_show(show_id) do
query =
from a in Application,
from a in App,
where: a.show_id == ^show_id,
preload: [application_show_groups: [show_group: [group: []]], user: []]

Repo.all(query)
end

@doc """
Returns true if it is possible to apply for the current show.
"""
def open?() do
show = Spex.current_spex()
current_date = DateTime.now!("Etc/UTC")

case show.application_opens && DateTime.compare(show.application_opens, current_date) do
:lt ->
case DateTime.compare(show.application_closes, current_date) do
:gt -> true
_ -> false
end

_ ->
false
end
end

def application_email(user, application, show_groups) do
spex = Spex.current_spex()

show_group_names =
Enum.map(application.application_show_groups, fn sg ->
show_groups[sg.show_group_id].group.name
end)
|> Enum.join(", ")

"""
<h2>Tack för din ansökan!</h2>
<p>Din ansökan till METAspexet #{spex.year.year} har tagits emot. Nedan kommer en sammanfattning av din ansökan:</p>
<ul>
<li><b>Namn</b>: #{user.first_name} #{user.last_name}</li>
<li><b>Mail</b>: #{user.email}</li>
<li><b>Telefonnummer</b>: #{user.phone}</li>
<li><b>Sökta grupper</b>: #{show_group_names}</li>
</ul>
Du kommer inom kort kontaktas av chefer för de grupper du sökt. Om du har några frågor eller funderingar
är du välkommen att kontakta Direqtionen på <a href="mailto:[email protected]">[email protected]</a>.
<br/><br/>
Hälsningar,<br/><br/>
Chefsgruppen för METAspexet 2024
"""
end

@spam_url "https://spam.datasektionen.se/api/sendmail"

def send_email(to, message) do
spex = Spex.current_spex()
api_key = Application.get_env(:haj, :spam_api_key)

HTTPoison.post(
@spam_url,
{:form,
[
{"from", "[email protected]"},
{"to", to},
{"subject", "Din ansökan till METAspexet #{spex.year.year}"},
{"content", message},
{"template", "metaspexet"},
{"key", api_key}
]}
)
end
end
14 changes: 13 additions & 1 deletion lib/haj/applications/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ defmodule Haj.Applications.Application do
field :other, :string
field :ranking, :string

field :status, Ecto.Enum,
values: [:pending, :submitted],
default: :pending

belongs_to :user, Haj.Accounts.User
belongs_to :show, Haj.Spex.Show
has_many :application_show_groups, Haj.Applications.ApplicationShowGroup
Expand All @@ -16,7 +20,15 @@ defmodule Haj.Applications.Application do
@doc false
def changeset(application, attrs) do
application
|> cast(attrs, [:other, :user_id, :show_id, :ranking])
|> cast(attrs, [:other, :user_id, :show_id, :ranking, :status])
|> validate_required([:show_id, :user_id])
end

@doc false
def changeset_with_show_groups(application, attrs) do
changeset(application, attrs)
|> cast_assoc(:application_show_groups,
with: &Haj.Applications.ApplicationShowGroup.changeset/2
)
end
end
7 changes: 7 additions & 0 deletions lib/haj/policy/policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,11 @@ defmodule Haj.Policy do
allow role: :admin
end
end

object :applications do
action :read do
allow role: :admin
allow current_group_member: :chefsgruppen
end
end
end
14 changes: 13 additions & 1 deletion lib/haj/slack.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Haj.Slack do
"""
def send_message(webhook_url, message) do
if webhook_url do
Logger.info("Sent slack message to #{webhook_url}")
Logger.info("Sending slack message to #{webhook_url}")

{_, res} =
HTTPoison.post(webhook_url, "{\"text\":\"#{message}\"}", [
Expand All @@ -16,4 +16,16 @@ defmodule Haj.Slack do
Logger.debug(res)
end
end

def application_message(user, application, show_groups) do
show_group_names =
Enum.map(application.application_show_groups, fn sg ->
show_groups[sg.show_group_id].group.name
end)
|> Enum.join(", ")

"""
#{user.first_name} #{user.last_name} (#{user.email}) sökte just till följande grupper: #{show_group_names}.
"""
end
end
5 changes: 5 additions & 0 deletions lib/haj_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ defmodule HajWeb do
import HajWeb.LiveHelpers

import HajWeb.CoreComponents
import HajWeb.Components
import HajWeb.Gettext
alias HajWeb.Router.Helpers, as: Routes
alias Phoenix.LiveView.JS
Expand All @@ -165,4 +166,8 @@ defmodule HajWeb do
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end

defmacro __using__({which, opts}) when is_atom(which) and is_list(opts) do
apply(__MODULE__, which, [opts])
end
end
Loading

0 comments on commit b1845a5

Please sign in to comment.