diff --git a/js/components/operatorSignIn/list.tsx b/js/components/operatorSignIn/list.tsx index c6c930db..6ec0b86a 100644 --- a/js/components/operatorSignIn/list.tsx +++ b/js/components/operatorSignIn/list.tsx @@ -45,7 +45,7 @@ export const List = ({ line }: { line: HeavyRailLine }): ReactElement => { .replace(/ /g, "")} - {si.signed_in_by_user.replace( + {si.signed_in_by.replace( /@/g, // 173 is a soft hyphen, which here acts as a breaking suggestion // We are hiding the hyphen above with CSS diff --git a/js/models/signin.ts b/js/models/signin.ts index 5feb6275..8d9dd022 100644 --- a/js/models/signin.ts +++ b/js/models/signin.ts @@ -3,7 +3,7 @@ import { z } from "zod"; export const SignIn = z.object({ rail_line: z.enum(["blue", "orange", "red"]), signed_in_at: z.string(), - signed_in_by_user: z.string(), + signed_in_by: z.string(), signed_in_employee: z.string(), }); diff --git a/js/test/components/operatorSignIn/list.test.tsx b/js/test/components/operatorSignIn/list.test.tsx index aa1043d1..03aed850 100644 --- a/js/test/components/operatorSignIn/list.test.tsx +++ b/js/test/components/operatorSignIn/list.test.tsx @@ -24,7 +24,7 @@ jest.mock("../../../hooks/useSignIns", () => ({ signed_in_at: DateTime.fromISO("2024-07-22T12:45:52.000-04:00", { zone: "America/New_York", }), - signed_in_by_user: "user@example.com", + signed_in_by: "user@example.com", signed_in_employee: EMPLOYEES[0].badge, }, ], diff --git a/js/test/hooks/useSignins.test.ts b/js/test/hooks/useSignins.test.ts index a86ee572..402a3c1d 100644 --- a/js/test/hooks/useSignins.test.ts +++ b/js/test/hooks/useSignins.test.ts @@ -13,7 +13,7 @@ const TEST_DATA = { { rail_line: "blue", signed_in_at: "2024-07-22T16:42:32Z", - signed_in_by_user: "user@example.com", + signed_in_by: "user@example.com", signed_in_employee: "123", }, ], diff --git a/lib/orbit_web/controllers/sign_in_controller.ex b/lib/orbit_web/controllers/sign_in_controller.ex index a6116854..1fe7fbe4 100644 --- a/lib/orbit_web/controllers/sign_in_controller.ex +++ b/lib/orbit_web/controllers/sign_in_controller.ex @@ -31,16 +31,29 @@ defmodule OrbitWeb.SignInController do ^start_datetime <= si.signed_in_at and si.signed_in_at < ^end_datetime and si.rail_line == ^rail_line, - preload: [:signed_in_employee, :signed_in_by_user], - order_by: [desc: :signed_in_at] + join: e in assoc(si, :signed_in_employee), + join: u in assoc(si, :signed_in_by_user), + preload: [signed_in_employee: e, signed_in_by_user: u], + left_join: signed_in_by_employee in Employee, + on: u.email == signed_in_by_employee.email, + order_by: [desc: :signed_in_at], + select: %{ + signin: si, + signed_in_by_employee: signed_in_by_employee + } ) ) - |> Enum.map(fn si -> + |> Enum.map(fn i -> %{ - rail_line: si.rail_line, - signed_in_at: DateTime.to_iso8601(si.signed_in_at), - signed_in_by_user: si.signed_in_by_user.email, - signed_in_employee: si.signed_in_employee.badge_number + rail_line: i.signin.rail_line, + signed_in_at: DateTime.to_iso8601(i.signin.signed_in_at), + signed_in_by: + if i.signed_in_by_employee do + Employee.display_name(i.signed_in_by_employee) + else + i.signin.signed_in_by_user.email + end, + signed_in_employee: i.signin.signed_in_employee.badge_number } end) } diff --git a/test/orbit_web/controllers/sign_in_controller_test.exs b/test/orbit_web/controllers/sign_in_controller_test.exs index 2416b857..7fe17258 100644 --- a/test/orbit_web/controllers/sign_in_controller_test.exs +++ b/test/orbit_web/controllers/sign_in_controller_test.exs @@ -27,7 +27,7 @@ defmodule OrbitWeb.SignInControllerTest do %{ "rail_line" => "blue", "signed_in_at" => _date, - "signed_in_by_user" => _user, + "signed_in_by" => _user, "signed_in_employee" => _badge } ] @@ -64,6 +64,47 @@ defmodule OrbitWeb.SignInControllerTest do } = json_response(conn, 200) end + @tag :authenticated + test "shows officials' names if available", %{conn: conn} do + insert(:employee, email: "test@example.com") + + insert(:operator_sign_in, + signed_in_at: DateTime.new!(~D[2024-07-21], ~T[12:00:00], @timezone), + signed_in_by_user: build(:user, email: "test@example.com") + ) + + conn = get(conn, ~p"/api/signin", %{"line" => "blue", "service_date" => "2024-07-21"}) + + assert %{ + "data" => [ + %{ + "signed_in_by" => "Preferredy Person" + } + ] + } = json_response(conn, 200) + end + + @tag :authenticated + test "shows official's email address if name is unavailable", %{conn: conn} do + insert(:operator_sign_in, + signed_in_at: DateTime.new!(~D[2024-07-21], ~T[12:00:00], @timezone), + signed_in_by_user: + build(:user, %{ + email: "does_not_exist_im_fairly_certain@example.com" + }) + ) + + conn = get(conn, ~p"/api/signin", %{"line" => "blue", "service_date" => "2024-07-21"}) + + assert %{ + "data" => [ + %{ + "signed_in_by" => "does_not_exist_im_fairly_certain@example.com" + } + ] + } = json_response(conn, 200) + end + @tag :authenticated test "sorted by signed_in_at descending (recent first)", %{conn: conn} do insert(:operator_sign_in,