Skip to content

Commit

Permalink
clean the code
Browse files Browse the repository at this point in the history
  • Loading branch information
the-last-pastafarian committed Apr 11, 2024
1 parent 89e2f5a commit cf365c8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 128 deletions.
123 changes: 14 additions & 109 deletions server/lib/field_hub/couch_service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,14 @@ defmodule FieldHub.CouchService do
end

@doc """
Returns up to five last changes for the specified project.
If the project has less than five changes or is new, less or no change would be returned.
Returns up to n last changes for the specified project.
If the project has less than n changes or is new, less or no change would be returned.
__Parameters__
- `project_identifier` - The name of the project.
## Example
iex> get_last_5_changes("development")
iex> get_n_last_changes("project_a",1)
[
%{
"changes" => [%{"rev" => "2-4f773e67d44d4bc99008713d9f9d164f"}],
Expand Down Expand Up @@ -388,131 +388,36 @@ defmodule FieldHub.CouchService do
Map.has_key?(doc, "_deleted")
end)
|> Enum.sort_by(
&extract_most_recent_date/1,
&compare_datetimes/2
&extract_most_recent_change_info/1,
&compare_change_info/2
)
|> Enum.take(n)
end

def extract_most_recent_date(change) do
modification_dates =
Map.get(change["doc"], "modified", [])

last_modified =
modification_dates
|> List.last(%{})
|> Map.get("date", nil)
|> case do
nil ->
nil

date_string ->
{:ok, datetime, _seconds} = DateTime.from_iso8601(date_string)
datetime
end

def extract_most_recent_change_info(%{"doc" => %{"modified" => []}} = change) do
{:ok, creation_date, _seconds} = DateTime.from_iso8601(change["doc"]["created"]["date"])
user_name = change["doc"]["created"]["user"]

case {last_modified, creation_date} do
{nil, created} ->
created

{modified, _created} ->
modified
end
{:created, creation_date, user_name}
end

def extract_most_recent_user(change) do
modification_dates =
Map.get(change["doc"], "modified", [])
def extract_most_recent_change_info(change) do
last_modification = List.last(change["doc"]["modified"])

last_modified =
modification_dates
|> List.last(%{})
|> Map.get("user", nil)
|> case do
nil ->
nil

user_name ->
user_name
end
{:ok, modification_date, _seconds} = DateTime.from_iso8601(last_modification["date"])
user_name = last_modification["user"]

creator = change["doc"]["created"]["user"]

case {last_modified, creator} do
{nil, created} ->
created

{modified, _created} ->
modified
end
{:modified, modification_date, user_name}
end

defp compare_datetimes(a, b) do
defp compare_change_info({_, a, _}, {_, b, _}) do
if DateTime.compare(a, b) == :gt do
true
else
false
end
end

@doc """
Returns a formatted string representing the date and author of the last change in a given project.
If the file has been deleted the date and the author can't be identified.
__Parameters__
- `project_identifier` - The name of the project.
## Example
iex> get_last_change_date("development")
"2024-02-29 (edited by anonymous)"
"""
def get_last_change_date(changes_data, project_identifier) do
case Map.get(changes_data, "id") do
nil ->
:no_changes_found

result ->
HTTPoison.get!(
"#{base_url()}/#{project_identifier}/#{result}",
get_user_credentials()
|> headers()
)
|> case do
%{status_code: 200, body: body} = _ ->
body
|> Jason.decode()
|> case do
{:ok, result} ->
result
|> Map.fetch("modified")
|> case do
{:ok, []} ->
result["created"]["date"] <>
" (created by " <> result["created"]["user"] <> ")"

{:ok, time_stamp} ->
ts = List.last(time_stamp)
Map.get(ts, "date") <> " (edited by " <> Map.get(ts, "user") <> ")"
end
end

%{status_code: 404, body: body} = _ ->
body
|> Jason.decode()
|> case do
{:ok, reason} ->
Map.fetch(reason, "reason")
|> case do
{:ok, "deleted"} -> "Unknown (the file has been deleted)"
_ -> "unknown"
end
end
end
end
end

@doc """
Returns the result of a `_all_docs` CouchDB query for the provided list of ids.
Expand Down
9 changes: 0 additions & 9 deletions server/lib/field_hub_web/live/project_show_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,4 @@ defmodule FieldHubWeb.ProjectShowLive do
|> assign(:contact, contact)
|> assign(:staff, staff)
end

defp extract_change_user(change) do
CouchService.extract_most_recent_user(change)
end

defp extract_change_date(change) do
CouchService.extract_most_recent_date(change)
|> Calendar.strftime("%c")
end
end
24 changes: 14 additions & 10 deletions server/test/field_hub/couch_service_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,24 @@ defmodule FieldHub.CouchServiceTest do

test "get_last_5_changes/1 return up to 5 last changes" do
assert [
%{"changes" => [%{"rev" => _}], "id" => _, "seq" => _, "doc" =>_},
%{"changes" => [%{"rev" => _}], "id" => _, "seq" => _, "doc" =>_},
%{"changes" => [%{"rev" => _}], "id" => _, "seq" => _, "doc" =>_},
%{"changes" => [%{"rev" => _}], "id" => _, "seq" => _, "doc" =>_},
%{"changes" => [%{"rev" => _}], "id" => _, "seq" => _, "doc" =>_}
%{"changes" => [%{"rev" => _}], "id" => _, "seq" => _, "doc" => _},
%{"changes" => [%{"rev" => _}], "id" => _, "seq" => _, "doc" => _},
%{"changes" => [%{"rev" => _}], "id" => _, "seq" => _, "doc" => _},
%{"changes" => [%{"rev" => _}], "id" => _, "seq" => _, "doc" => _},
%{"changes" => [%{"rev" => _}], "id" => _, "seq" => _, "doc" => _}
] = CouchService.get_last_5_changes(@project)
end

test "get_last_change_date/2 return date and author of a change" do
# assert {"2023-01-05T10:32:09.2"<>_<>" (edited by sample_data)"} = CouchService.get_last_change_date(List.first(CouchService.get_last_5_changes(@project)),@project)
assert ["2023-01-05T10:32:09.",_] = [
String.split(
CouchService.get_last_change_date(List.first(CouchService.get_last_5_changes(@project)),@project), "."),
]

assert ["2023-01-05T10:32:09.", _] = [
String.split(
CouchService.get_last_change_date(
List.first(CouchService.get_last_5_changes(@project)),
@project
),
"."
)
]
end
end

0 comments on commit cf365c8

Please sign in to comment.