Skip to content

Commit

Permalink
Merge pull request #1510 from GSA/1508-submissions-count
Browse files Browse the repository at this point in the history
[1508] add counter_cache implementation to phoenix
  • Loading branch information
stepchud authored Jan 16, 2025
2 parents 1742281 + 028ff7e commit 08c7cdf
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/challenge_gov/challenges/phase.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ defmodule ChallengeGov.Challenges.Phase do
field(:how_to_enter_length, :integer, virtual: true)

field(:delete_phase, :boolean, virtual: true)
field(:submissions_count, :integer, default: 0)

timestamps(type: :utc_datetime_usec)
end
Expand Down
26 changes: 26 additions & 0 deletions lib/challenge_gov/submissions/submission.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule ChallengeGov.Submissions.Submission do
use Ecto.Schema

import Ecto.Changeset
import Ecto.Query
import Waffle.Ecto.Schema

alias ChallengeGov.Accounts.User
Expand Down Expand Up @@ -124,6 +125,7 @@ defmodule ChallengeGov.Submissions.Submission do
|> validate_required([:title, :brief_description, :description])
# Validate file upload on review.
|> validate_file_upload(challenge, params)
|> prepare_changes(&increment_submissions_count/1)
end

def update_draft_changeset(struct, params, _challenge) do
Expand Down Expand Up @@ -151,6 +153,14 @@ defmodule ChallengeGov.Submissions.Submission do
|> validate_required([:title, :brief_description, :description])
# Validate file upload on update review.
|> validate_file_upload(challenge, params)
|> prepare_changes(fn changeset ->
# only increment when the value changed to submitted
if "submitted" == get_change(changeset, :status) do
increment_submissions_count(changeset)
else
changeset
end
end)
end

defp validate_file_upload(changeset, challenge, params) do
Expand All @@ -171,6 +181,15 @@ defmodule ChallengeGov.Submissions.Submission do
end
end

defp increment_submissions_count(changeset, increment \\ 1) do
phase_id = get_field(changeset, :phase_id)

from(p in Phase, where: p.id == ^phase_id)
|> changeset.repo.update_all(inc: [submissions_count: increment])

changeset
end

def submit_changeset(struct) do
struct
|> change()
Expand All @@ -193,6 +212,13 @@ defmodule ChallengeGov.Submissions.Submission do
struct
|> change()
|> put_change(:deleted_at, now)
|> prepare_changes(fn changeset ->
if "submitted" == get_field(changeset, :status) do
increment_submissions_count(changeset, -1)
else
changeset
end
end)
end

defp validate_required_fields(struct) do
Expand Down
1 change: 1 addition & 0 deletions lib/web/controllers/submission_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ defmodule Web.SubmissionController do
# |> render("edit.html")
# end

# NOTE: This route is not currently used? can also remove Web.SubmissionView.submit_button/4?
def submit(conn, %{"id" => id}) do
%{current_user: user, current_submission: submission} = conn.assigns

Expand Down
15 changes: 1 addition & 14 deletions lib/web/views/submission_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ defmodule Web.SubmissionView do
end
end

# TODO: Remove unused button?
def submit_button(conn, submission, user, opts \\ []) do
case Submissions.is_editable?(user, submission) && submission.status !== "submitted" do
true ->
Expand All @@ -219,20 +220,6 @@ defmodule Web.SubmissionView do
end
end

def submit_button2(conn, submission, user, opts \\ []) do
case Submissions.is_editable?(user, submission) do
true ->
button(opts[:label] || "Submit",
to: Routes.submission_path(conn, :submit, submission.id),
method: :put,
class: "usa-button float-right submit-form display-none"
)

false ->
nil
end
end

def cancel_button(conn, action, challenge, phase, user, _opts \\ []) do
route =
cond do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule ChallengeGov.Repo.Migrations.AddPhasesSubmissionsCount do
use Ecto.Migration

def change do
alter table("phases") do
add_if_not_exists :submissions_count, :integer, null: false, default: 0
end
end
end
27 changes: 27 additions & 0 deletions test/challenge_gov/submissions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule ChallengeGov.SubmissionsTest do
user = AccountHelpers.create_user()
challenge = ChallengeHelpers.create_single_phase_challenge(user, %{user_id: user.id})
phase = Enum.at(challenge.phases, 0)
assert phase.submissions_count == 0

{:ok, submission} =
Submissions.create_draft(
Expand All @@ -30,6 +31,9 @@ defmodule ChallengeGov.SubmissionsTest do
assert submission.challenge_id === challenge.id
assert is_nil(submission.title)
assert submission.status === "draft"
# no change to submissions_count for drafts
{:ok, phase} = ChallengeGov.Phases.get(phase.id)
assert phase.submissions_count == 0
end

test "saving as draft with data" do
Expand Down Expand Up @@ -84,6 +88,7 @@ defmodule ChallengeGov.SubmissionsTest do
user = AccountHelpers.create_user()
challenge = ChallengeHelpers.create_single_phase_challenge(user, %{user_id: user.id})
phase = Enum.at(challenge.phases, 0)
assert phase.submissions_count == 0

{:ok, submission} =
Submissions.create_review(
Expand All @@ -109,6 +114,9 @@ defmodule ChallengeGov.SubmissionsTest do
assert submission.description === "Test Description"
assert submission.external_url === "www.example.com"
assert submission.status === "submitted"
# increment submissions_count for submitted status
{:ok, phase} = ChallengeGov.Phases.get(phase.id)
assert phase.submissions_count == 1
end
end

Expand Down Expand Up @@ -176,6 +184,10 @@ defmodule ChallengeGov.SubmissionsTest do
{:error, changeset} = Submissions.update_review(submission, %{"title" => nil}, challenge)

assert changeset.errors[:title]
# does not increment submissions_count for errors
phase = Enum.at(challenge.phases, 0)
{:ok, phase} = ChallengeGov.Phases.get(phase.id)
assert phase.submissions_count == 0
end

test "update submitted" do
Expand All @@ -191,6 +203,10 @@ defmodule ChallengeGov.SubmissionsTest do
challenge = Repo.preload(challenge, [:challenge_manager_users])

submission = SubmissionHelpers.create_submitted_submission(%{}, user, challenge)
# increment submissions_count for submitted status
phase = Enum.at(challenge.phases, 0)
{:ok, phase} = ChallengeGov.Phases.get(phase.id)
assert phase.submissions_count == 1

{:ok, updated_submission} =
Submissions.update_review(
Expand All @@ -199,6 +215,10 @@ defmodule ChallengeGov.SubmissionsTest do
challenge
)

# does not increment submissions_count when submitted status doesn't change
{:ok, phase} = ChallengeGov.Phases.get(phase.id)
assert phase.submissions_count == 1

{:ok, updated_submission} = Submissions.submit(updated_submission)

assert updated_submission.submitter_id === user.id
Expand Down Expand Up @@ -234,8 +254,15 @@ defmodule ChallengeGov.SubmissionsTest do
challenge = ChallengeHelpers.create_single_phase_challenge(user, %{user_id: user.id})

submission = SubmissionHelpers.create_submitted_submission(%{}, user, challenge)
# increment submissions_count for submitted status
phase = Enum.at(challenge.phases, 0)
{:ok, phase} = ChallengeGov.Phases.get(phase.id)
assert phase.submissions_count == 1

{:ok, submission} = Submissions.delete(submission)
# decrement submissions_count for submitted submission that is deleted
{:ok, phase} = ChallengeGov.Phases.get(phase.id)
assert phase.submissions_count == 0

assert !is_nil(submission.deleted_at)
end
Expand Down

0 comments on commit 08c7cdf

Please sign in to comment.