Skip to content

Commit

Permalink
don't overwite submitted application until sumbit
Browse files Browse the repository at this point in the history
  • Loading branch information
adriansalamon committed Sep 18, 2023
1 parent 37e18ab commit 5387e7a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 38 deletions.
57 changes: 29 additions & 28 deletions lib/haj/applications.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ defmodule Haj.Applications do

@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.
If there is already a pending application for that show for that user, it updates that application instead.
"""
def create_application(user_id, show_id, show_groups) do
Repo.transaction(fn ->
previous =
Repo.one(
from a in App,
where: a.show_id == ^show_id and a.user_id == ^user_id,
where: a.show_id == ^show_id and a.user_id == ^user_id and a.status == ^:pending,
preload: :application_show_groups
)

Expand All @@ -69,10 +69,6 @@ defmodule Haj.Applications do
show_group_id: id
})
end)

previous
|> App.changeset(%{status: :pending})
|> Repo.update!()
else
{:ok, application} =
%App{user_id: user_id, show_id: show_id}
Expand All @@ -89,29 +85,19 @@ defmodule Haj.Applications do
end

@doc """
Updates a application.
## Examples
iex> update_application(application, %{field: new_value})
{:ok, %Application{}}
Completes a pending application. Deletes all other applications for the user for the same show.
"""
def complete_application(%App{} = application, attrs) do
Repo.transaction(fn ->
app = application |> App.changeset_with_show_groups(attrs) |> Repo.update!()

iex> update_application(application, %{field: bad_value})
{:error, %Ecto.Changeset{}}
Repo.delete_all(
from a in App,
where: a.id != ^app.id and a.show_id == ^app.show_id and a.user_id == ^app.user_id
)

"""
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
app
end)
end

@doc """
Expand Down Expand Up @@ -175,7 +161,22 @@ defmodule Haj.Applications do
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,
where:
a.user_id == ^user_id and a.show_id == ^Spex.current_spex().id and
a.status == ^:submitted,
preload: [application_show_groups: []]

Repo.one(query)
end

@doc """
Returns an application for a user for the current show.
"""
def get_pending_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 and a.status == ^:pending,
preload: [application_show_groups: []]

Repo.one(query)
Expand Down
8 changes: 2 additions & 6 deletions lib/haj_web/live/apply_live/complete.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule HajWeb.ApplyLive.Complete do
@impl true
def mount(_params, _session, socket) do
user = socket.assigns[:current_user]
application = Applications.get_current_application_for_user(user.id)
application = Applications.get_pending_application_for_user(user.id)

if application == nil do
{:ok,
Expand Down Expand Up @@ -45,11 +45,7 @@ defmodule HajWeb.ApplyLive.Complete do
if Applications.open?() do
application_params = Map.put(application_params, "status", :submitted)

case Applications.update_application(
socket.assigns.application,
application_params,
with_show_groups: true
) do
case Applications.complete_application(socket.assigns.application, application_params) do
{:ok, application} ->
slack_message =
Haj.Slack.application_message(
Expand Down
7 changes: 3 additions & 4 deletions lib/haj_web/live/apply_live/groups.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ defmodule HajWeb.ApplyLive.Groups do
current_spex = Spex.current_spex()

{application, pre_filled?} =
case Haj.Applications.get_applications_for_user(user.id)
|> Enum.filter(fn %{show_id: id} -> id == current_spex.id end) do
[app | _] -> {app, true}
_ -> {%Haj.Applications.Application{}, false}
case Haj.Applications.get_current_application_for_user(user.id) do
nil -> {%Haj.Applications.Application{}, false}
app -> {app, true}
end

groups =
Expand Down

0 comments on commit 5387e7a

Please sign in to comment.