Okay, so I worked on this code and fixed a few things. Here is the second iteration.
# --------------------------------------------------------
# Modified create fn that creates new users and modifies org
def create(conn, %{"user" => user_params}) do
with {:ok, %User{} = user} <- Users.create_user(user_params),
{:ok, user_params} <- UserController.update_user_role(user_params),
{:ok, user_params} <- UserController.update_user_org(user_params) do
authorized_user(conn, user.email, user_params["passphrase"])
end
end
# --------------------------------------------------------
# Queries roleID from role table and inserts into user table
def update_user_role(user_params) do
uname = user_params["username"]
query_uid = Repo.one(from u in User,
where: u.username == ^uname,
select: u.id)
query_rid = Repo.one(from r in Role,
where: r.role_name == "SuperAdmin",
select: r.id)
changeset = User.changeset(%User{id: query_uid}, %{role_id: query_rid})
case Repo.update(changeset, on_conflict: :nothing) do
{:ok, _updated_user} ->
Logger.notice("Successfully updated role_id #{query_rid} for user with ID #{query_uid}")
{:error, _changeset} ->
Logger.error("User role_id update failed for user with ID #{query_uid}")
_ ->
Logger.info("Something went wrong :-(")
end
end
This runs and does add the role.id to the users table role_id column
kzy_be_api_dev=# SELECT * FROM users;
id | username | first_name | last_name | passphrase | email | phone_num | org_id | role_id | inserted_at | updated_at
--------------------------------------+----------+------------+-----------+---------------------------------------------------------------------------------------------------+------------------------+-----------+--------------------------------------+--------------------------------------+---------------------+---------------------
76a9cde7-d157-479e-bf80-6c0e94477376 | Test1 | | | $argon2id$v=19$m=65536,t=8,p=2$/ignZuzRo81e66v0Pgy49Q$O2KSz7+Depi31rytIvbhYECFbYaTkPqVxruh17HVtM8 | Test1@fakecompany.com | | d81cc533-83a7-47ce-bf61-389fc4a022c0 | e157d756-5d2c-4506-9f6e-158053cfdeda | 2023-08-11 00:24:35 | 2023-08-11 00:24:35
and it gets all the way to “Logger.notice(“Successfully updated role_id #{query_rid} for user with ID #{query_uid}”)” success tuple in my case statement. I was originally getting a Fallback error after clearing up the right side no match error and “fixed” the fallback error by adding this function to the fallback_controller.ex file
def call(conn, _opts) do
Logger.error("FallbackController: No matching route found for #{conn.method} #{conn.request_path}")
conn
|> put_status(404)
|> render(:"404")
end
But now I’m running into this 404.json render error even after adding a render 404.json function to the views/user_view.ex file,
def render("404.json", %{user: _user}) do
%{
error: "Not Found",
message: "The requested resource was not found."
}
end
Wondering if anyone has see and fixed or worked around it. The error stack is below. As always I appreciate everyones time and patience.
[2023-08-10][22:38:09.829][nonode@nohost][info] POST /api/auth/create/
[2023-08-10][22:38:09.829][nonode@nohost][debug] Processing with KzyBeApiWeb.UserController.create/2
Parameters: %{"user" => %{"email" => "Test5@fakecompany.com", "passphrase" => "[FILTERED]", "username" => "Test5"}}
Pipelines: [:api]
[2023-08-10][22:38:09.962][nonode@nohost][debug] QUERY OK db=1.1ms idle=556.2ms
begin []
↳ KzyBeApiWeb.UserController.create/2, at: lib/kzy_be_api_web/controllers/user_controller.ex:29
[2023-08-10][22:38:09.965][nonode@nohost][debug] QUERY OK db=1.4ms
INSERT INTO "orgs" ("inserted_at","updated_at","id") VALUES ($1,$2,$3) [~N[2023-08-11 05:38:09], ~N[2023-08-11 05:38:09], "b97816d8-3a0e-405e-993d-7a11d9bfb4bf"]
↳ anonymous fn/3 in Ecto.Adapters.SQL.checkout_or_transaction/4, at: lib/ecto/adapters/sql.ex:1203
[2023-08-10][22:38:09.969][nonode@nohost][debug] QUERY OK db=2.5ms
INSERT INTO "users" ("username","email","passphrase","org_id","inserted_at","updated_at","id") VALUES ($1,$2,$3,$4,$5,$6,$7) ["Test5", "Test5@fakecompany.com", "$argon2id$v=19$m=65536,t=8,p=2$dkpcz9PEDUpZ2CQQxolRwQ$cjjZC1yTKGLR5FI+fPu0ZteDP3dFPtfEhfDNW1PD7RI", "b97816d8-3a0e-405e-993d-7a11d9bfb4bf", ~N[2023-08-11 05:38:09], ~N[2023-08-11 05:38:09], "78caf6e1-ced6-4a9f-8710-e12533e20414"]
↳ KzyBeApiWeb.UserController.create/2, at: lib/kzy_be_api_web/controllers/user_controller.ex:29
[2023-08-10][22:38:09.970][nonode@nohost][debug] QUERY OK db=1.7ms
commit []
↳ KzyBeApiWeb.UserController.create/2, at: lib/kzy_be_api_web/controllers/user_controller.ex:29
[2023-08-10][22:38:09.974][nonode@nohost][debug] QUERY OK source="users" db=3.0ms idle=565.8ms
SELECT u0."id" FROM "users" AS u0 WHERE (u0."username" = $1) ["Test5"]
↳ KzyBeApiWeb.UserController.update_user_role/1, at: lib/kzy_be_api_web/controllers/user_controller.ex:44
[2023-08-10][22:38:09.974][nonode@nohost][debug] QUERY OK source="roles" db=0.7ms idle=568.8ms
SELECT r0."id" FROM "roles" AS r0 WHERE (r0."role_name" = 'SuperAdmin') []
↳ KzyBeApiWeb.UserController.update_user_role/1, at: lib/kzy_be_api_web/controllers/user_controller.ex:48
[2023-08-10][22:38:09.977][nonode@nohost][debug] QUERY OK db=2.5ms idle=569.6ms
UPDATE "users" SET "role_id" = $1, "updated_at" = $2 WHERE "id" = $3 ["05ca2a87-e004-4ae0-9747-a120f45f0ce9", ~N[2023-08-11 05:38:09], "78caf6e1-ced6-4a9f-8710-e12533e20414"]
↳ KzyBeApiWeb.UserController.update_user_role/1, at: lib/kzy_be_api_web/controllers/user_controller.ex:54
[2023-08-10][22:38:09.977][nonode@nohost][notice] Successfully updated role_id 05ca2a87-e004-4ae0-9747-a120f45f0ce9 for user with ID 78caf6e1-ced6-4a9f-8710-e12533e20414
[2023-08-10][22:38:09.977][nonode@nohost][error] FallbackController: No matching route found for POST /api/auth/create/
[2023-08-10][22:38:09.979][nonode@nohost][error] Error in router: %Phoenix.Template.UndefinedError{available: [], template: "404.json", module: KzyBeApiWeb.UserView, root: "lib/kzy_be_api_web/templates/user", assigns: %{conn: %Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, assigns: %{layout: false}, body_params: %{"user" => %{"email" => "Test5@fakecompany.com", "passphrase" => "R@ndoP@55!", "username" => "Test5"}}, cookies: %{}, halted: false, host: "localhost", method: "POST", owner: #PID<0.577.0>, params: %{"user" => %{"email" => "Test5@fakecompany.com", "passphrase" => "R@ndoP@55!", "username" => "Test5"}}, path_info: ["api", "auth", "create"], path_params: %{}, port: 4000, private: %{:phoenix_view => %{_: KzyBeApiWeb.UserView}, :phoenix_template => "404.json", KzyBeApiWeb.Router => [], :phoenix_endpoint => KzyBeApiWeb.Endpoint, :plug_session_fetch => :done, :plug_session => %{}, :before_send => [#Function<0.76384852/1 in Plug.Session.before_send/2>, #Function<0.54455629/1 in Plug.Telemetry.call/2>], :phoenix_router => KzyBeApiWeb.Router, :phoenix_action => :create, :phoenix_layout => %{_: {KzyBeApiWeb.LayoutView, :app}}, :phoenix_controller => KzyBeApiWeb.UserController, :phoenix_format => "json"}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %{}, req_headers: [{"accept", "application/json, text/plain, */*"}, {"accept-encoding", "gzip, deflate, br"}, {"accept-language", "en-US,en;q=0.5"}, {"connection", "keep-alive"}, {"content-length", "113"}, {"content-type", "application/json"}, {"host", "localhost:4000"}, {"origin", "moz-extension://3c769a57-746a-4274-8642-e84b16e31e0f"}, {"sec-fetch-dest", "empty"}, {"sec-fetch-mode", "cors"}, {"sec-fetch-site", "same-origin"}, {"user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/116.0"}], request_path: "/api/auth/create/", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "F3o9je5URnBQUIgAAADH"}], scheme: :http, script_name: [], secret_key_base: :..., state: :unset, status: 404}}, pattern: "*"}
[2023-08-10][22:38:09.979][nonode@nohost][info] Sent 500 in 149ms
[2023-08-10][22:38:09.979][nonode@nohost][error] #PID<0.577.0> running Phoenix.Endpoint.SyncCodeReloadPlug (connection #PID<0.509.0>, stream id 4) terminated
Server: localhost:4000 (http)
Request: POST /api/auth/create/
** (exit) an exception was raised:
** (Phoenix.Template.UndefinedError) Could not render "404.json" for KzyBeApiWeb.UserView, please define a matching clause for render/2 or define a template at "lib/kzy_be_api_web/templates/user/*". No templates were compiled for this module.
Assigns:
%{conn: %Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, assigns: %{layout: false}, body_params: %{"user" => %{"email" => "Test5@fakecompany.com", "passphrase" => "R@ndoP@55!", "username" => "Test5"}}, cookies: %{}, halted: false, host: "localhost", method: "POST", owner: #PID<0.577.0>, params: %{"user" => %{"email" => "Test5@fakecompany.com", "passphrase" => "R@ndoP@55!", "username" => "Test5"}}, path_info: ["api", "auth", "create"], path_params: %{}, port: 4000, private: %{:phoenix_view => %{_: KzyBeApiWeb.UserView}, :phoenix_template => "404.json", KzyBeApiWeb.Router => [], :phoenix_endpoint => KzyBeApiWeb.Endpoint, :plug_session_fetch => :done, :plug_session => %{}, :before_send => [#Function<0.76384852/1 in Plug.Session.before_send/2>, #Function<0.54455629/1 in Plug.Telemetry.call/2>], :phoenix_router => KzyBeApiWeb.Router, :phoenix_action => :create, :phoenix_layout => %{_: {KzyBeApiWeb.LayoutView, :app}}, :phoenix_controller => KzyBeApiWeb.UserController, :phoenix_format => "json"}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %{}, req_headers: [{"accept", "application/json, text/plain, */*"}, {"accept-encoding", "gzip, deflate, br"}, {"accept-language", "en-US,en;q=0.5"}, {"connection", "keep-alive"}, {"content-length", "113"}, {"content-type", "application/json"}, {"host", "localhost:4000"}, {"origin", "moz-extension://3c769a57-746a-4274-8642-e84b16e31e0f"}, {"sec-fetch-dest", "empty"}, {"sec-fetch-mode", "cors"}, {"sec-fetch-site", "same-origin"}, {"user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/116.0"}], request_path: "/api/auth/create/", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "F3o9je5URnBQUIgAAADH"}], scheme: :http, script_name: [], secret_key_base: :..., state: :unset, status: 404}}
Assigned keys: [:conn]
(phoenix_view 2.0.2) lib/phoenix_view.ex:674: Phoenix.View.__not_found__!/3
(phoenix_template 1.0.1) lib/phoenix/template.ex:126: Phoenix.Template.render_to_iodata/4
(phoenix 1.7.2) lib/phoenix/controller.ex:1010: anonymous fn/5 in Phoenix.Controller.template_render_to_iodata/4
(telemetry 1.2.1) /Users/zano/Desktop/opt/git/kzy-elixir/deps/telemetry/src/telemetry.erl:321: :telemetry.span/3
(phoenix 1.7.2) lib/phoenix/controller.ex:976: Phoenix.Controller.render_and_send/4
(kzy_be_api 0.1.0) lib/kzy_be_api_web/controllers/user_controller.ex:1: KzyBeApiWeb.UserController.action/2
(kzy_be_api 0.1.0) lib/kzy_be_api_web/controllers/user_controller.ex:1: KzyBeApiWeb.UserController.phoenix_controller_pipeline/2
(phoenix 1.7.2) lib/phoenix/router.ex:430: Phoenix.Router.__call__/5
(kzy_be_api 0.1.0) deps/plug/lib/plug/error_handler.ex:80: KzyBeApiWeb.Router.call/2
(kzy_be_api 0.1.0) lib/kzy_be_api_web/endpoint.ex:1: KzyBeApiWeb.Endpoint.plug_builder_call/2
(kzy_be_api 0.1.0) deps/plug/lib/plug/debugger.ex:136: KzyBeApiWeb.Endpoint."call (overridable 3)"/2
(kzy_be_api 0.1.0) lib/kzy_be_api_web/endpoint.ex:1: KzyBeApiWeb.Endpoint.call/2
(phoenix 1.7.2) lib/phoenix/endpoint/sync_code_reload_plug.ex:22: Phoenix.Endpoint.SyncCodeReloadPlug.do_call/4
(plug_cowboy 2.6.1) lib/plug/cowboy/handler.ex:11: Plug.Cowboy.Handler.init/2
(cowboy 2.9.0) /Users/zano/Desktop/opt/git/kzy-elixir/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
(cowboy 2.9.0) /Users/zano/Desktop/opt/git/kzy-elixir/deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3
(cowboy 2.9.0) /Users/zano/Desktop/opt/git/kzy-elixir/deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3
(stdlib 5.0.2) proc_lib.erl:241: :proc_lib.init_p_do_apply/3
[2023-08-10][23:25:13.497][nonode@nohost][debug] QUERY OK source="guardian_tokens" db=1.7ms queue=3.0ms idle=1213.7ms
DELETE FROM "guardian_tokens" AS g0 WHERE (g0."exp" < $1) [1691735113]
↳ Guardian.DB.Token.Sweeper.sweep/1, at: lib/guardian/db/token/sweeper.ex:12