-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from datasektionen/live-applications
Live applications
- Loading branch information
Showing
38 changed files
with
1,005 additions
and
430 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 """ | ||
|
@@ -19,7 +21,7 @@ defmodule Haj.Applications do | |
""" | ||
def list_applications do | ||
Repo.all(Application) | ||
Repo.all(App) | ||
end | ||
|
||
@doc """ | ||
|
@@ -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) | ||
|
@@ -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 """ | ||
|
@@ -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 | ||
|
||
|
@@ -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, | ||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.