Skip to content

Commit

Permalink
Backoffice : ajout bouton pour consolider BNZFE (#4236)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineAugusti authored Oct 2, 2024
1 parent 280a2e6 commit a23b5c3
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 7 deletions.
3 changes: 2 additions & 1 deletion apps/transport/lib/jobs/consolidate_lez_job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ defmodule Transport.Jobs.ConsolidateLEZsJob do
}

@impl Oban.Worker
def perform(%Oban.Job{}) do
def perform(%Oban.Job{id: job_id}) do
consolidate() |> update_files()
Oban.Notifier.notify(Oban, :gossip, %{complete: job_id})
:ok
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule TransportWeb.Live.SendConsolidateBNLCView do
defmodule TransportWeb.Live.SendConsolidateJobView do
# Very similar to `TransportWeb.Live.SendNowOnNAPNotificationView`
use Phoenix.LiveView
@button_disabled [:dispatched, :sent]
Expand All @@ -13,14 +13,20 @@ defmodule TransportWeb.Live.SendConsolidateBNLCView do

def mount(
_params,
%{"button_texts" => button_texts, "button_default_class" => button_default_class, "job_args" => job_args},
%{
"button_texts" => button_texts,
"button_default_class" => button_default_class,
"job_module" => job_module,
"job_args" => job_args
},
socket
) do
socket =
socket
|> assign(
button_texts: button_texts,
button_default_class: button_default_class,
job_module: job_module,
job_args: job_args
)
|> assign_step(:first)
Expand All @@ -33,9 +39,9 @@ defmodule TransportWeb.Live.SendConsolidateBNLCView do
{:noreply, socket}
end

def handle_info(:dispatch, %Phoenix.LiveView.Socket{assigns: %{job_args: job_args}} = socket) do
def handle_info(:dispatch, %Phoenix.LiveView.Socket{assigns: %{job_args: job_args, job_module: job_module}} = socket) do
new_socket =
case job_args |> Transport.Jobs.ConsolidateBNLCJob.new() |> Oban.insert() do
case job_args |> job_module.new() |> Oban.insert() do
{:ok, %Oban.Job{id: job_id}} ->
send(self(), {:wait_for_completion, job_id})
assign_step(socket, :dispatched)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,32 +91,50 @@

<h2>Consolidation BNLC</h2>
<div>
<%= live_render(@conn, TransportWeb.Live.SendConsolidateBNLCView,
<%= live_render(@conn, TransportWeb.Live.SendConsolidateJobView,
session: %{
"button_texts" => %{
sent: "Rapport disponible par e-mail",
dispatched: "Consolidation en cours",
default: "🧪 Consolider à blanc"
},
"button_default_class" => "button",
"job_module" => Transport.Jobs.ConsolidateBNLCJob,
"job_args" => %{}
}
) %>
</div>
<div>
<%= live_render(@conn, TransportWeb.Live.SendConsolidateBNLCView,
<%= live_render(@conn, TransportWeb.Live.SendConsolidateJobView,
session: %{
"button_texts" => %{
sent: "Fichier mis à jour et rapport envoyé",
dispatched: "Consolidation en cours",
default: "📥 Consolider et remplacer sur data.gouv.fr"
},
"button_default_class" => "button warning-light",
"job_module" => Transport.Jobs.ConsolidateBNLCJob,
"job_args" => %{"action" => "datagouv_update"}
}
) %>
</div>

<h2>Consolidation ZFE</h2>
<div>
<%= live_render(@conn, TransportWeb.Live.SendConsolidateJobView,
session: %{
"button_texts" => %{
sent: "Fichiers mis à jour",
dispatched: "Consolidation en cours",
default: "📥 Consolider et remplacer sur data.gouv.fr"
},
"button_default_class" => "button warning-light",
"job_module" => Transport.Jobs.ConsolidateLEZsJob,
"job_args" => %{}
}
) %>
</div>

<h2><%= dgettext("backoffice", "Validations") %></h2>
<div>
<%= form_for @conn, backoffice_dataset_path(@conn, :force_validate_gtfs_transport), [class: "no-margin"], fn _f -> %>
Expand Down
62 changes: 62 additions & 0 deletions apps/transport/test/transport/jobs/consolidate_lez_job_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Transport.Test.Transport.Jobs.ConsolidateLEZsJobTest do
use ExUnit.Case, async: true
use Oban.Testing, repo: DB.Repo
import DB.Factory
import Mox
alias Transport.Jobs.ConsolidateLEZsJob
Expand Down Expand Up @@ -275,6 +276,67 @@ defmodule Transport.Test.Transport.Jobs.ConsolidateLEZsJobTest do
refute File.exists?(ConsolidateLEZsJob.tmp_filepath("aires.geojson"))
end

test "perform" do
aom = insert(:aom, siren: "253800825")
dataset = insert(:dataset, type: "low-emission-zones", aom: aom)

zfe_aire =
insert(:resource,
dataset: dataset,
url: "https://example.com/aires.geojson",
schema_name: "etalab/schema-zfe"
)

zfe_voies =
insert(:resource,
dataset: dataset,
url: "https://example.com/voies.geojson",
schema_name: "etalab/schema-zfe"
)

resource_history_aire =
insert(:resource_history,
resource_id: zfe_aire.id,
payload: %{
"permanent_url" => permanent_url_aires = "https://example.com/permanent_url/aires"
}
)

resource_history_voies =
insert(:resource_history,
resource_id: zfe_voies.id,
payload: %{
"permanent_url" => permanent_url_voies = "https://example.com/permanent_url/voies"
}
)

insert(:multi_validation, resource_history_id: resource_history_aire.id, result: %{"has_errors" => false})
insert(:multi_validation, resource_history_id: resource_history_voies.id, result: %{"has_errors" => false})

setup_http_mocks(permanent_url_aires, permanent_url_voies)

Transport.HTTPoison.Mock
|> expect(:request, fn :post, _url, args, _headers, [follow_redirect: true] ->
{:multipart, [{:file, path, {"form-data", [name: "file", filename: "aires.geojson"]}, []}]} = args
assert String.ends_with?(path, "aires.geojson")
{:ok, %HTTPoison.Response{body: "", status_code: 200}}
end)

Transport.HTTPoison.Mock
|> expect(:request, fn :post, _url, args, _headers, [follow_redirect: true] ->
{:multipart, [{:file, path, {"form-data", [name: "file", filename: "voies.geojson"]}, []}]} = args
assert String.ends_with?(path, "voies.geojson")
{:ok, %HTTPoison.Response{body: "", status_code: 200}}
end)

assert :ok == perform_job(ConsolidateLEZsJob, %{})

# When Oban 2.19.0 will be released we should be able to make sure that
# a broadcast notification was sent on the `:gossip` channel.
# => We need to control the `job_id` when dispatching a job.
# https://github.com/oban-bg/oban/commit/fae376232ef44d8405940d3d287ab8fd93912d0a
end

defp setup_http_mocks(url_aires, url_voies) do
Transport.HTTPoison.Mock
|> expect(:get!, fn ^url_aires, [], follow_redirect: true ->
Expand Down

0 comments on commit a23b5c3

Please sign in to comment.