From 93f81b50f84f60b97c7605dce6e02c653fcdd16d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vila=C3=A7a?= Date: Sat, 16 Jan 2021 20:33:30 +0000 Subject: [PATCH 1/6] General updates --- config/config.exs | 2 +- config/prod.exs | 4 ++++ docker-compose.yml | 17 ++++++++++++++--- lib/infin/importer.ex | 4 +++- lib/infin/invoices.ex | 2 +- lib/infin_web/templates/invoice/index.html.eex | 2 +- mix.exs | 9 ++++++--- mix.lock | 2 ++ 8 files changed, 32 insertions(+), 10 deletions(-) diff --git a/config/config.exs b/config/config.exs index 4537ccc..4e9b8f7 100644 --- a/config/config.exs +++ b/config/config.exs @@ -18,7 +18,7 @@ config :infin, InfinWeb.Endpoint, pubsub_server: Infin.PubSub, live_view: [signing_salt: "ZtkrwQ6M"], pt_invoices_url: "localhost:3000", - statistics_url: "localhost:5600/graphql" + analytics_url: "localhost:5600/graphql" # Configures Elixir's Logger config :logger, :console, diff --git a/config/prod.exs b/config/prod.exs index 46c5f7d..d8207c9 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -17,6 +17,10 @@ config :infin, InfinWeb.Endpoint, "#{System.get_env("PT_INVOICES_HOST") || "localhost"}:#{ System.get_env("PT_INVOICES_PORT") || "3000" }", + analytics_url: + "#{System.get_env("ANALYTICS_HOST") || "localhost"}:#{ + System.get_env("ANALYTICS_PORT") || "5600" + }", pt_sibsapimarket: [ url: System.get_env("PT_SIBSAPIMARKET_HOST") || "https://site1.sibsapimarket.com:8445/sibs/apimarket-sb", diff --git a/docker-compose.yml b/docker-compose.yml index 4842819..9b951fe 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,7 @@ version: '3.1' services: analytics: - image: repo.treescale.com/infinions/analytics:v1.0.0 + image: repo.treescale.com/infinions/analytics:v1.0.2 environment: API_PORT: 5600 DB_NAME: infin_dev @@ -12,21 +12,32 @@ services: DB_HOST: db DB_ANALYTICS: 'mongodb://analytics_db:27017/' DB_ANALYTICS_HOST: analytics_db - DB_ANAKYTICS_PORT: 27017 + DB_ANALYTICS_PORT: 27017 ports: - 5600:5600 + restart: always analytics_db: image: mongo ports: - 27017:27017 + restart: always + + pt-invoices: + image: repo.treescale.com/infinions/pt-invoices:v1.0.0 + environment: + NODE_PORT: 3000 + NODE_ENV: production + ports: + - 3000:3000 + restart: always db: image: postgres - restart: always environment: POSTGRES_DB: infin_dev POSTGRES_PASSWORD: infin POSTGRES_USER: infin ports: - 5432:5432 + restart: always diff --git a/lib/infin/importer.ex b/lib/infin/importer.ex index 3e2e42a..4116529 100644 --- a/lib/infin/importer.ex +++ b/lib/infin/importer.ex @@ -25,11 +25,13 @@ defmodule Infin.Importer do headers ) do {:ok, response} -> + IO.inspect(response) case response.status_code do 200 -> %HTTPoison.Response{body: body} = response object = Jason.decode!(body) - Invoices.insert_fectched_invoices_pt(object["invoices"], company_id) + invoices = Map.get(object, "invoices", []) + Invoices.insert_fectched_invoices_pt(invoices, company_id) {:ok, "Invoices imported"} 400 -> diff --git a/lib/infin/invoices.ex b/lib/infin/invoices.ex index 3073d75..b08559d 100644 --- a/lib/infin/invoices.ex +++ b/lib/infin/invoices.ex @@ -122,7 +122,7 @@ defmodule Infin.Invoices do unless Companies.get_company_by_nif(to_string(invoice["nifEmitente"])) do Companies.create_company(%{ :nif => to_string(invoice["nifEmitente"]), - :name => invoice["nomeEmitente"] + :name => HtmlEntities.decode(invoice["nomeEmitente"]) }) end diff --git a/lib/infin_web/templates/invoice/index.html.eex b/lib/infin_web/templates/invoice/index.html.eex index 4322d52..c8eb42b 100644 --- a/lib/infin_web/templates/invoice/index.html.eex +++ b/lib/infin_web/templates/invoice/index.html.eex @@ -20,7 +20,7 @@ <%= invoice.doc_emission_date %> <%= get_total_value(invoice) %> <%= invoice.company_seller.nif %> - <%= invoice.company_seller.name %> + <%= invoice.company_seller.name %> <%= if invoice.category == nil do%> <% else %> diff --git a/mix.exs b/mix.exs index a34ed31..f9c8ec9 100644 --- a/mix.exs +++ b/mix.exs @@ -52,7 +52,8 @@ defmodule Infin.MixProject do {:ex_machina, "~> 2.4"}, {:httpoison, "~> 1.7"}, {:scrivener_ecto, "~> 2.0"}, - {:arc_ecto, "~> 0.11.3"} + {:arc_ecto, "~> 0.11.3"}, + {:html_entities, "~> 0.5.1"} ] end @@ -76,12 +77,14 @@ defmodule Infin.MixProject do test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"], "docker.up": [ "cmd docker login -u=machadovilaca -p=bf9ebaf2843c4b79bb60daeb1c961cbf repo.treescale.com", - "cmd docker-compose up -d" + "cmd docker-compose up -d", + "setup" ], "docker.down": ["cmd docker-compose down"], "podman.up": [ "cmd podman login -u=machadovilaca -p=bf9ebaf2843c4b79bb60daeb1c961cbf repo.treescale.com", - "cmd podman-compose up -d" + "cmd podman-compose up -d", + "setup" ], "podman.down": ["cmd podman-compose down"] ] diff --git a/mix.lock b/mix.lock index e0bcb37..fe58376 100644 --- a/mix.lock +++ b/mix.lock @@ -4,6 +4,7 @@ "bcrypt_elixir": {:hex, :bcrypt_elixir, "2.2.0", "3df902b81ce7fa8867a2ae30d20a1da6877a2c056bfb116fd0bc8a5f0190cea4", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "762be3fcb779f08207531bc6612cca480a338e4b4357abb49f5ce00240a77d1e"}, "certifi": {:hex, :certifi, "2.5.2", "b7cfeae9d2ed395695dd8201c57a2d019c0c43ecaf8b8bcb9320b40d6662f340", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "3b3b5f36493004ac3455966991eaf6e768ce9884693d9968055aeeeb1e575040"}, "comeonin": {:hex, :comeonin, "5.3.1", "7fe612b739c78c9c1a75186ef2d322ce4d25032d119823269d0aa1e2f1e20025", [:mix], [], "hexpm", "d6222483060c17f0977fad1b7401ef0c5863c985a64352755f366aee3799c245"}, + "con_cache": {:hex, :con_cache, "0.14.0", "863acb90fa08017be3129074993af944cf7a4b6c3ee7c06c5cd0ed6b94fbc223", [:mix], [], "hexpm", "50887a8949377d0b707a3c6653b7610de06074751b52d0f267f52135f391aece"}, "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"}, "cowboy": {:hex, :cowboy, "2.8.0", "f3dc62e35797ecd9ac1b50db74611193c29815401e53bac9a5c0577bd7bc667d", [:rebar3], [{:cowlib, "~> 2.9.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "4643e4fba74ac96d4d152c75803de6fad0b3fa5df354c71afdd6cbeeb15fac8a"}, "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.3.0", "69fdb5cf92df6373e15675eb4018cf629f5d8e35e74841bb637d6596cb797bbc", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "42868c229d9a2900a1501c5d0355bfd46e24c862c322b0b4f5a6f14fe0216753"}, @@ -46,4 +47,5 @@ "telemetry_metrics": {:hex, :telemetry_metrics, "0.6.0", "da9d49ee7e6bb1c259d36ce6539cd45ae14d81247a2b0c90edf55e2b50507f7b", [:mix], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "5cfe67ad464b243835512aa44321cee91faed6ea868d7fb761d7016e02915c3d"}, "telemetry_poller": {:hex, :telemetry_poller, "0.5.1", "21071cc2e536810bac5628b935521ff3e28f0303e770951158c73eaaa01e962a", [:rebar3], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4cab72069210bc6e7a080cec9afffad1b33370149ed5d379b81c7c5f0c663fd4"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.5.0", "8516502659002cec19e244ebd90d312183064be95025a319a6c7e89f4bccd65b", [:rebar3], [], "hexpm", "d48d002e15f5cc105a696cf2f1bbb3fc72b4b770a184d8420c8db20da2674b38"}, + "unidecode": {:hex, :unidecode, "0.0.2", "485fe257dd45081d5d8f376dfd7129994c3e383d52df96455c6367d3789e9171", [:mix], [{:con_cache, ">= 0.13.0", [hex: :con_cache, repo: "hexpm", optional: false]}], "hexpm", "ab7c0577a897e5651d57ed2fa9014eb60c60ac5f90847cb1ae8daf474494810d"}, } From b7e2b34be78e42833f67603ce8dd353e1c4df37e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vila=C3=A7a?= Date: Sun, 17 Jan 2021 20:36:59 +0000 Subject: [PATCH 2/6] Auto categorize imported invoices --- docker-compose.yml | 5 ++ lib/infin/importer.ex | 72 +++++++++++++++++-- lib/infin/invoices.ex | 3 +- .../live/dashboard/dashboard.html.leex | 2 +- lib/infin_web/router.ex | 2 +- mix.exs | 11 +-- mix.lock | 2 + priv/repo/banks.exs | 6 +- priv/repo/seeds.exs | 4 ++ 9 files changed, 91 insertions(+), 16 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 9b951fe..ab63fdd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -41,3 +41,8 @@ services: ports: - 5432:5432 restart: always + volumes: + - pgdata:/var/lib/postgresql/data + +volumes: + pgdata: diff --git a/lib/infin/importer.ex b/lib/infin/importer.ex index 4116529..2af5f1d 100644 --- a/lib/infin/importer.ex +++ b/lib/infin/importer.ex @@ -4,7 +4,10 @@ defmodule Infin.Importer do """ import Ecto.Query, warn: false + + alias Infin.Companies.Company alias Infin.Invoices + alias Infin.Repo require Logger @@ -20,19 +23,35 @@ defmodule Infin.Importer do headers = %{"Content-type" => "application/json"} case HTTPoison.post( - Application.get_env(:infin, InfinWeb.Endpoint)[:pt_invoices_url] <> "/invoices", + Application.get_env(:infin, InfinWeb.Endpoint)[:pt_invoices_url] <> + "/invoices", {:stream, enumerable}, headers ) do {:ok, response} -> - IO.inspect(response) case response.status_code do 200 -> %HTTPoison.Response{body: body} = response object = Jason.decode!(body) invoices = Map.get(object, "invoices", []) - Invoices.insert_fectched_invoices_pt(invoices, company_id) - {:ok, "Invoices imported"} + {:ok, result} = get_category_prediction(invoices, company_id) + + categories = + Jason.decode!(result.body["data"]["categorize_invoices"]) + + if categories != [] do + Invoices.insert_fectched_invoices_pt( + categorize_invoices(invoices, categories), + company_id + ) + + {:ok, "Invoices imported"} + else + Invoices.insert_fectched_invoices_pt(invoices, company_id) + + {:ok, + "Invoices imported, but insufficient data to automatic categorization"} + end 400 -> %HTTPoison.Response{body: body} = response @@ -40,13 +59,54 @@ defmodule Infin.Importer do {:error, object["message"]} _ -> - %{service: "pt_invoices", message: response} |> inspect() |> Logger.error + %{service: "pt_invoices", message: response} + |> inspect() + |> Logger.error() + {:error, "Service not available"} end {_, message} -> - message |> inspect() |> Logger.error + message |> inspect() |> Logger.error() {:error, "Service not available"} end end + + defp get_category_prediction(invoices, company_id) do + analytics_url = + Application.get_env(:infin, InfinWeb.Endpoint)[:analytics_url] + + parsed_invoices = + transform_invoices(invoices, company_id) |> String.slice(1..-2) + + Neuron.Config.set(url: analytics_url) + + Neuron.query( + "{ categorize_invoices(invoices: \"{\\\"list\\\": #{parsed_invoices}}\")}" + ) + end + + defp transform_invoices(invoices, company_id) do + company = Company |> Repo.get(company_id) + + invoices + |> Enum.map(fn i -> + %{ + nif: company.nif, + company_seller_name: i["nomeEmitente"], + total_value: i["valorTotal"], + doc_emission_date: i["dataEmissaoDocumento"] + } + end) + |> Jason.encode!() + |> Jason.encode!() + end + + defp categorize_invoices(invoices, categories) do + invoices + |> Enum.with_index(1) + |> Enum.map(fn {invoice, index} -> + Map.put(invoice, :category_id, categories[to_string(index)]) + end) + end end diff --git a/lib/infin/invoices.ex b/lib/infin/invoices.ex index b08559d..42dfbf2 100644 --- a/lib/infin/invoices.ex +++ b/lib/infin/invoices.ex @@ -131,7 +131,8 @@ defmodule Infin.Invoices do :total_value => invoice["valorTotal"], :doc_emission_date => invoice["dataEmissaoDocumento"], :company_id => company_id, - :company_seller_id => Companies.get_company_by_nif(to_string(invoice["nifEmitente"])).id + :company_seller_id => Companies.get_company_by_nif(to_string(invoice["nifEmitente"])).id, + :category_id => Map.get(invoice, :category_id) } create_invoice(document) diff --git a/lib/infin_web/live/dashboard/dashboard.html.leex b/lib/infin_web/live/dashboard/dashboard.html.leex index cd79822..ed51c37 100644 --- a/lib/infin_web/live/dashboard/dashboard.html.leex +++ b/lib/infin_web/live/dashboard/dashboard.html.leex @@ -9,7 +9,7 @@ <%= live_component(@socket, InfinWeb.DashboardLive.Pending, id: "#{@company_id}-pending", company_id: @company_id, transactions_page_number: 0, transactions_count: 0, - invoices_page_number: 0, invoices_count: 0, + invoices_page_number: 0, invoices_count: 0 ) %> diff --git a/lib/infin_web/router.ex b/lib/infin_web/router.ex index f05b030..8472aa8 100644 --- a/lib/infin_web/router.ex +++ b/lib/infin_web/router.ex @@ -93,6 +93,6 @@ defmodule InfinWeb.Router do resources "/categories", CategoryController, except: [:index, :edit] resources "/incomes", IncomeController resources "/costs", CostController - + end end diff --git a/mix.exs b/mix.exs index f9c8ec9..1b560d7 100644 --- a/mix.exs +++ b/mix.exs @@ -53,7 +53,8 @@ defmodule Infin.MixProject do {:httpoison, "~> 1.7"}, {:scrivener_ecto, "~> 2.0"}, {:arc_ecto, "~> 0.11.3"}, - {:html_entities, "~> 0.5.1"} + {:html_entities, "~> 0.5.1"}, + {:neuron, "~> 5.0.0"} ] end @@ -68,10 +69,10 @@ defmodule Infin.MixProject do setup: ["deps.get", "ecto.setup", "cmd npm install --prefix assets"], "ecto.bootstrap": ["ecto.migrate", "run priv/repo/banks.exs"], "ecto.setup": [ - "ecto.create", - "ecto.migrate", - "run priv/repo/seeds.exs", - "run priv/repo/banks.exs" + "ecto.create --quiet", + "ecto.migrate --quiet", + "run priv/repo/banks.exs", + "run priv/repo/seeds.exs" ], "ecto.reset": ["ecto.drop", "ecto.setup"], test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"], diff --git a/mix.lock b/mix.lock index fe58376..e83ad6a 100644 --- a/mix.lock +++ b/mix.lock @@ -26,6 +26,7 @@ "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, "mime": {:hex, :mime, "1.4.0", "5066f14944b470286146047d2f73518cf5cca82f8e4815cf35d196b58cf07c47", [:mix], [], "hexpm", "75fa42c4228ea9a23f70f123c74ba7cece6a03b1fd474fe13f6a7a85c6ea4ff6"}, "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"}, + "neuron": {:hex, :neuron, "5.0.0", "64c6b14138e4f6e61a55abb0bb95659aa193145ed9baf80b4e760d4c189f6c6f", [:mix], [{:httpoison, "~> 1.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "7597308e22e34f77acaadbae3da63974edc9e4041ef7f7154d45ce80c0e33d00"}, "parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"}, "phoenix": {:hex, :phoenix, "1.5.6", "8298cdb4e0f943242ba8410780a6a69cbbe972fef199b341a36898dd751bdd66", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.13", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.1.2 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "0dc4d39af1306b6aa5122729b0a95ca779e42c708c6fe7abbb3d336d5379e956"}, "phoenix_ecto": {:hex, :phoenix_ecto, "4.2.1", "13f124cf0a3ce0f1948cf24654c7b9f2347169ff75c1123f44674afee6af3b03", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 2.15", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "478a1bae899cac0a6e02be1deec7e2944b7754c04e7d4107fc5a517f877743c0"}, @@ -38,6 +39,7 @@ "plug": {:hex, :plug, "1.10.4", "41eba7d1a2d671faaf531fa867645bd5a3dce0957d8e2a3f398ccff7d2ef017f", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ad1e233fe73d2eec56616568d260777b67f53148a999dc2d048f4eb9778fe4a0"}, "plug_cowboy": {:hex, :plug_cowboy, "2.4.0", "e936ef151751f386804c51f87f7300f5aaae6893cdad726559c3930c6c032948", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "e25ddcfc06b1b76e55af79d078b03cbc86bbcb99ce4e5e0a5e4a8114ee039be6"}, "plug_crypto": {:hex, :plug_crypto, "1.2.0", "1cb20793aa63a6c619dd18bb33d7a3aa94818e5fd39ad357051a67f26dfa2df6", [:mix], [], "hexpm", "a48b538ae8bf381ffac344520755f3007cc10bd8e90b240af98ea29b69683fc2"}, + "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm", "fec8660eb7733ee4117b85f55799fd3833eb769a6df71ccf8903e8dc5447cfce"}, "postgrex": {:hex, :postgrex, "0.15.6", "a464c72010a56e3214fe2b99c1a76faab4c2bb0255cabdef30dea763a3569aa2", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "f99268325ac8f66ffd6c4964faab9e70fbf721234ab2ad238c00f9530b8cdd55"}, "ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"}, "scrivener": {:hex, :scrivener, "2.7.0", "fa94cdea21fad0649921d8066b1833d18d296217bfdf4a5389a2f45ee857b773", [:mix], [], "hexpm", "30da36a427f2519cf75993271fb7c5aad1759682a70f90d880a85c3d743d2c57"}, diff --git a/priv/repo/banks.exs b/priv/repo/banks.exs index 0b94d51..e24c4e4 100644 --- a/priv/repo/banks.exs +++ b/priv/repo/banks.exs @@ -45,5 +45,7 @@ defmodule PTBankSeed do end end -{:ok, response} = PTBankSeed.fetch_banks -response["aspsp-list"] |> Enum.map(fn x -> PTBankSeed.insert_bank(x) end) +if PTBank |> Repo.aggregate(:count, :id) == 0 do + {:ok, response} = PTBankSeed.fetch_banks + response["aspsp-list"] |> Enum.map(fn x -> PTBankSeed.insert_bank(x) end) +end diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 217701b..54416c1 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -15,6 +15,10 @@ alias Infin.Invoices.Tag alias Infin.Repo alias Infin.Revenue.Income +if User |> Repo.aggregate(:count, :id) > 0 do + exit(:shutdown) +end + Repo.insert!( %User{} |> User.registration_changeset(%{ From 53ed170b8370bb81f2e669d5c7914f7cccb931a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vila=C3=A7a?= Date: Wed, 20 Jan 2021 00:08:04 +0000 Subject: [PATCH 3/6] Signal auto categorized invoices --- lib/infin/importer.ex | 4 +++- lib/infin/invoices.ex | 8 +++++--- lib/infin/invoices/invoice.ex | 4 +++- lib/infin_web/templates/invoice/index.html.eex | 15 ++++++++++----- ...0119232239_invoices_add_automatic_category.exs | 9 +++++++++ 5 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 priv/repo/migrations/20210119232239_invoices_add_automatic_category.exs diff --git a/lib/infin/importer.ex b/lib/infin/importer.ex index 2af5f1d..e587792 100644 --- a/lib/infin/importer.ex +++ b/lib/infin/importer.ex @@ -106,7 +106,9 @@ defmodule Infin.Importer do invoices |> Enum.with_index(1) |> Enum.map(fn {invoice, index} -> - Map.put(invoice, :category_id, categories[to_string(index)]) + invoice + |> Map.put(:category_id, categories[to_string(index)]) + |> Map.put(:automatic_category, true) end) end end diff --git a/lib/infin/invoices.ex b/lib/infin/invoices.ex index 42dfbf2..4924d63 100644 --- a/lib/infin/invoices.ex +++ b/lib/infin/invoices.ex @@ -33,7 +33,8 @@ defmodule Infin.Invoices do query = from(i in Invoice, where: i.company_id == ^company_id, - preload: [:company_seller, :category, :company, company: :categories] + preload: [:company_seller, :category, :company, company: :categories], + order_by: [desc: :doc_emission_date] ) Repo.paginate(query, params) @@ -132,7 +133,8 @@ defmodule Infin.Invoices do :doc_emission_date => invoice["dataEmissaoDocumento"], :company_id => company_id, :company_seller_id => Companies.get_company_by_nif(to_string(invoice["nifEmitente"])).id, - :category_id => Map.get(invoice, :category_id) + :category_id => Map.get(invoice, :category_id), + :automatic_category => Map.get(invoice, :automatic_category) } create_invoice(document) @@ -153,7 +155,7 @@ defmodule Infin.Invoices do """ def update_invoice(%Invoice{} = invoice, attrs) do invoice - |> Invoice.changeset(attrs) + |> Invoice.changeset(attrs |> Map.put("automatic_category", false)) |> Repo.update() end diff --git a/lib/infin/invoices/invoice.ex b/lib/infin/invoices/invoice.ex index 7d1a957..598570f 100644 --- a/lib/infin/invoices/invoice.ex +++ b/lib/infin/invoices/invoice.ex @@ -30,6 +30,7 @@ defmodule Infin.Invoices.Invoice do field :merchant_comm, :boolean field :consumer_comm, :boolean field :is_foreign, :boolean + field :automatic_category, :boolean belongs_to :company_seller, Infin.Companies.Company, foreign_key: :company_seller_id belongs_to :company, Infin.Companies.Company, foreign_key: :company_id @@ -73,7 +74,8 @@ defmodule Infin.Invoices.Invoice do :company_id, :company_seller_id, :category_id, - :pdf_id + :pdf_id, + :automatic_category ]) |> validate_required([:id_document, :doc_emission_date, :total_value, :company_id]) |> unique_constraint(:id_document) diff --git a/lib/infin_web/templates/invoice/index.html.eex b/lib/infin_web/templates/invoice/index.html.eex index c8eb42b..00cc65f 100644 --- a/lib/infin_web/templates/invoice/index.html.eex +++ b/lib/infin_web/templates/invoice/index.html.eex @@ -22,11 +22,16 @@ <%= invoice.company_seller.nif %> <%= invoice.company_seller.name %> - <%= if invoice.category == nil do%> - <% else %> - - <%= invoice.category.name %> - + <%= if invoice.category != nil do%> + <%= if invoice.automatic_category do%> + + <%= invoice.category.name %> + + <% else %> + + <%= invoice.category.name %> + + <% end %> <% end %> diff --git a/priv/repo/migrations/20210119232239_invoices_add_automatic_category.exs b/priv/repo/migrations/20210119232239_invoices_add_automatic_category.exs new file mode 100644 index 0000000..868492b --- /dev/null +++ b/priv/repo/migrations/20210119232239_invoices_add_automatic_category.exs @@ -0,0 +1,9 @@ +defmodule Infin.Repo.Migrations.InvoicesAddAutomaticCategory do + use Ecto.Migration + + def change do + alter table(:invoices) do + add :automatic_category, :boolean, default: false + end + end +end From 6c91b0d0d65bba5b893efc2de0a64d957e353e53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vila=C3=A7a?= Date: Wed, 20 Jan 2021 14:14:02 +0000 Subject: [PATCH 4/6] Fix update_invoice support only for string map --- lib/infin/invoices.ex | 6 +++++- test/infin/invoices_test.exs | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/infin/invoices.ex b/lib/infin/invoices.ex index 4924d63..cf305c1 100644 --- a/lib/infin/invoices.ex +++ b/lib/infin/invoices.ex @@ -154,8 +154,12 @@ defmodule Infin.Invoices do """ def update_invoice(%Invoice{} = invoice, attrs) do + at = attrs + |> Map.new(fn {k, v} -> {to_string(k), v} end) + |> Map.put("automatic_category", false) + invoice - |> Invoice.changeset(attrs |> Map.put("automatic_category", false)) + |> Invoice.changeset(at) |> Repo.update() end diff --git a/test/infin/invoices_test.exs b/test/infin/invoices_test.exs index de1ff0f..9fbd385 100644 --- a/test/infin/invoices_test.exs +++ b/test/infin/invoices_test.exs @@ -14,7 +14,7 @@ defmodule Infin.InvoicesTest do def invoice_fixture() do company1 = insert(:company) company2 = insert(:company) - insert(:invoice, company_seller_id: company1.id, company_id: company2.id) + insert(:invoice, company_seller_id: company1.id, company_id: company2.id, automatic_category: false) end test "list_invoices/0 returns all invoices" do From 8b6d321e9a9f6d2916459851ac73557c2c6d0ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vila=C3=A7a?= Date: Wed, 20 Jan 2021 17:49:03 +0000 Subject: [PATCH 5/6] Fix build env issues --- Dockerfile | 2 ++ config/prod.exs | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8b360bf..d7b0da9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,10 +9,12 @@ ADD mix.exs mix.lock ./ RUN mix do deps.get, deps.compile ADD assets/package.json assets/ + RUN cd assets && \ npm install ADD . . +RUN echo "ANALYTICS_URL=https://infin.di.uminho.pt/graphql" >> assets/.env RUN cd assets/ && \ npm run deploy && \ diff --git a/config/prod.exs b/config/prod.exs index d8207c9..c2e1ea9 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -20,11 +20,13 @@ config :infin, InfinWeb.Endpoint, analytics_url: "#{System.get_env("ANALYTICS_HOST") || "localhost"}:#{ System.get_env("ANALYTICS_PORT") || "5600" - }", + }#{System.get_env("ANALYTICS_PATH") || "5600"}", pt_sibsapimarket: [ - url: System.get_env("PT_SIBSAPIMARKET_HOST") || + url: + System.get_env("PT_SIBSAPIMARKET_HOST") || "https://site1.sibsapimarket.com:8445/sibs/apimarket-sb", - apiKey: System.get_env("PT_SIBSAPIMARKET_APIKEY") || + apiKey: + System.get_env("PT_SIBSAPIMARKET_APIKEY") || "073286e4-055b-472b-96ad-7ddd484333ec" ] From cb895143fe65ca20be3ba5ea0bf7ce7f304ae1c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vila=C3=A7a?= Date: Wed, 20 Jan 2021 18:31:57 +0000 Subject: [PATCH 6/6] Fix js import errors causing issues in LiveView --- assets/js/app.js | 2 -- assets/webpack.config.js | 4 +++- lib/infin_web/live/dashboard/graph.ex | 2 +- lib/infin_web/live/dashboard/graph.html.leex | 2 ++ lib/infin_web/templates/invoice/form.html.eex | 2 ++ 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/assets/js/app.js b/assets/js/app.js index 143d336..988fbb4 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -8,8 +8,6 @@ import "./register" import "./calendar" import "./modal" -import "./charts" -import "./file" // webpack automatically bundles all modules in your // entry points. Those entry points can be configured diff --git a/assets/webpack.config.js b/assets/webpack.config.js index 789f507..d6bf504 100644 --- a/assets/webpack.config.js +++ b/assets/webpack.config.js @@ -19,7 +19,9 @@ module.exports = (env, options) => { ] }, entry: { - 'app': glob.sync('./vendor/**/*.js').concat(['./js/app.js']) + 'app': glob.sync('./vendor/**/*.js').concat(['./js/app.js']), + 'file': glob.sync('./vendor/**/*.js').concat(['./js/file.js']), + 'charts': glob.sync('./vendor/**/*.js').concat(['./js/charts.js']) }, output: { filename: '[name].js', diff --git a/lib/infin_web/live/dashboard/graph.ex b/lib/infin_web/live/dashboard/graph.ex index 80f84ae..15bcd85 100644 --- a/lib/infin_web/live/dashboard/graph.ex +++ b/lib/infin_web/live/dashboard/graph.ex @@ -1,5 +1,5 @@ defmodule InfinWeb.DashboardLive.Graph do - use Phoenix.LiveComponent + use InfinWeb, :live_component alias Infin.Companies diff --git a/lib/infin_web/live/dashboard/graph.html.leex b/lib/infin_web/live/dashboard/graph.html.leex index ab514d0..dfb8247 100644 --- a/lib/infin_web/live/dashboard/graph.html.leex +++ b/lib/infin_web/live/dashboard/graph.html.leex @@ -78,3 +78,5 @@ + + diff --git a/lib/infin_web/templates/invoice/form.html.eex b/lib/infin_web/templates/invoice/form.html.eex index a2a0dbc..fc9cca9 100644 --- a/lib/infin_web/templates/invoice/form.html.eex +++ b/lib/infin_web/templates/invoice/form.html.eex @@ -125,3 +125,5 @@ <% end %> + +