Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: interface separation discussion #2955

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 61 additions & 5 deletions assets/src/models/detoursList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,24 @@ export interface GroupedSimpleDetours {
past: SimpleDetour[]
}

const MyNormalizedType = type({
my_id: detourId,
my_route: string(),
my_via_variant: string(),
my_direction: string(),
my_name: string(),
my_intersection: string(),
my_updated_at: number(),
my_activated_at: coerce(date(), string(), (dateStr) => new Date(dateStr)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if we're dealing with a draft detour (never activated), this and estimated_duration would be null? (I could see past detours retaining their activated_at and estimated_duration because there's no reason to delete them, as long as status is also included somewhere in this normalized type)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this model also include the state blob, since some endpoints also return that to the frontend?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't spend the time to figure out every field that we'd want to pass through, just grabbed the ones that are explicitly grabbed in the serde module. We'd need to go through and figure out what fields are needed top level and decide how we want to model the 'shape' portion of the detour, whether that lives in its own table or blob field

my_estimated_duration: string(),
})

type MyNormalizedType = Infer<typeof MyNormalizedType>

export const GroupedDetoursData = type({
active: array(ActivatedDetourData),
draft: array(SimpleDetourData),
past: array(SimpleDetourData),
active: array(MyNormalizedType),
draft: array(MyNormalizedType),
past: array(MyNormalizedType),
})

export type GroupedDetoursData = Infer<typeof GroupedDetoursData>
Expand All @@ -72,8 +86,50 @@ export const groupedDetoursFromData = (
groupedDetours: GroupedDetoursData
): GroupedSimpleDetours => ({
active: groupedDetours.active
.map(fromNormalizedToActivatedData)
.map(simpleDetourFromActivatedData)
.sort((a, b) => b.activatedAt.getTime() - a.activatedAt.getTime()),
draft: groupedDetours.draft.map((detour) => simpleDetourFromData(detour)),
past: groupedDetours.past.map((detour) => simpleDetourFromData(detour)),
draft: groupedDetours.draft.map(fromNormalizedToSimple).map((detour) => simpleDetourFromData(detour)),
past: groupedDetours.past.map(fromNormalizedToSimple).map((detour) => simpleDetourFromData(detour)),
})

const fromNormalizedToSimple = ({
my_id,
my_route,
my_via_variant,
my_direction,
my_name,
my_intersection,
my_updated_at,
}: MyNormalizedType): SimpleDetourData => ({
id: my_id,
route: my_route,
via_variant: my_via_variant,
direction: my_direction,
name: my_name,
intersection: my_intersection,
updated_at: my_updated_at
})

const fromNormalizedToActivatedData = ({
my_id,
my_route,
my_via_variant,
my_direction,
my_name,
my_intersection,
my_updated_at,
my_activated_at,
my_estimated_duration }: MyNormalizedType): ActivatedDetourData => ({
activated_at: my_activated_at,
estimated_duration: my_estimated_duration,
details: {
id: my_id,
route: my_route,
via_variant: my_via_variant,
direction: my_direction,
name: my_name,
intersection: my_intersection,
updated_at: my_updated_at
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'd need a third fromNormalizedToXYZ to cover the simplest form {state, author, id} that is returned when we open a single detour, but I don't know what to call that one. It was previously DetourWithState, but I don't love that name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a couple other spots I missed, but just wanted to give an example for isolating the frontend from backend db changes for at least one of the end points

})
133 changes: 133 additions & 0 deletions lib/skate/detours/detours.ex
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,137 @@ defmodule Skate.Detours.Detours do
|> DateTime.from_naive!("Etc/UTC")
|> DateTime.to_unix()
end

@spec normalize_from_frontend(map(), any()) :: %{
:author_id => binary(),
:state => map(),
optional(:activated_at) => any(),
optional(:deactivated_at) => any(),
optional(:id) => any(),
optional(:status) => :active | :draft | :past
}
def normalize_from_frontend(params, author_id) do
allowed_params = Map.take(params, ["id", "state", "activated_at", "deactivated_at"])

normalized_params =
Map.merge(
%{"id" => nil, "state" => nil, "activated_at" => nil, "deactivated_at" => nil},
allowed_params
)

do_normalize_from_frontend(normalized_params, author_id)
end

# create
defp do_normalize_from_frontend(%{"id" => nil, "state" => state}, author_id) do
%{
state: state,
author_id: author_id,
status: :draft
}
end

# update
defp do_normalize_from_frontend(
%{"id" => id, "state" => state, "activated_at" => nil},
author_id
) do
%{
id: id,
state: state,
author_id: author_id
}
end

# activate
defp do_normalize_from_frontend(
%{"id" => id, "state" => state, "activated_at" => activated_at, "deactivated_at" => nil},
author_id
)
when not is_nil(activated_at) do
%{
id: id,
state: state,
author_id: author_id,
activated_at: activated_at,
status: :active
}
end

# de-activate
defp do_normalize_from_frontend(
%{"id" => id, "state" => state, "deactivated_at" => deactivated_at},
author_id
)
when not is_nil(deactivated_at) do
%{
id: id,
state: state,
author_id: author_id,
deactivated_at: deactivated_at,
status: :past
}
end

@spec normalize_from_backend(
Skate.Detours.Detour.WithState.t()
| Skate.Detours.Detour.Detailed.t()
| %{
active: [Skate.Detours.Detour.Detailed.t()],
draft: [Skate.Detours.Detour.Detailed.t()],
past: [Skate.Detours.Detour.Detailed.t()]
}
) :: %{
my_activated_at: nil,
my_direction: nil,
my_estimated_duration: nil,
my_id: nil,
my_intersection: nil,
my_name: nil,
my_route: nil,
my_updated_at: nil,
my_via_variant: nil
}
def normalize_from_backend(%Skate.Detours.Detour.WithState{} = _detour) do
# unpack data into decided upon interface from current struct type
%{
my_id: nil,
my_route: nil,
my_via_variant: nil,
my_direction: nil,
my_name: nil,
my_intersection: nil,
my_updated_at: nil,
my_activated_at: nil,
my_estimated_duration: nil
}
end

def normalize_from_backend(%{active: active, draft: draft, past: past}) do
%{
active: normalize_from_backend(active),
draft: normalize_from_backend(draft),
past: normalize_from_backend(past)
}
end

def normalize_from_backend(nil), do: nil

def normalize_from_backend([%Skate.Detours.Detour.Detailed{} | _] = detours),
do: Enum.map(detours, &normalize_from_backend/1)

def normalize_from_backend(%Skate.Detours.Detour.Detailed{} = _detour) do
# unpack data into decided upon interface from current struct type
%{
my_id: nil,
my_route: nil,
my_via_variant: nil,
my_direction: nil,
my_name: nil,
my_intersection: nil,
my_updated_at: nil,
my_activated_at: nil,
my_estimated_duration: nil
}
end
end
9 changes: 9 additions & 0 deletions lib/skate_web/controllers/detours_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ defmodule SkateWeb.DetoursController do
def update_snapshot(conn, %{"snapshot" => snapshot}) do
%{id: user_id} = AuthManager.Plug.current_resource(conn)

# normalized_request = Detours.normalize_from_frontend(snapshot, user_id)
# {:ok, %{id: returned_uuid}} = Detours.upsert(normalized_request)

{:ok, %Skate.Detours.Db.Detour{id: returned_uuid}} =
Detours.upsert_from_snapshot(user_id, snapshot)

Expand All @@ -24,6 +27,9 @@ defmodule SkateWeb.DetoursController do
def detour(conn, %{"detour_id" => detour_id}) do
detour = Detours.get_detour_with_state!(detour_id)

# normalized_detour = Detours.normalize_from_backend(detour)
# json(conn, %{data: normalized_detour})

json(conn, %{data: detour})
end

Expand All @@ -33,6 +39,9 @@ defmodule SkateWeb.DetoursController do

detours = Detours.grouped_detours(user_id)

# normalized_detours = Enum.map(detours, &Detours.normalize_from_backend/1)
# json(conn, %{data: normalized_detours})

json(conn, %{data: detours})
end

Expand Down
Loading