From 18ac33bf1a53a46aa3c69dbc541b956367c1ad0b Mon Sep 17 00:00:00 2001 From: the-last-pastafarian Date: Fri, 1 Mar 2024 11:24:29 +0100 Subject: [PATCH] Functions to retrieve last changes (last file updated, the time and the author of the modification --- server/lib/field_hub/couch_service.ex | 42 +++++++++++++++++++++------ 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/server/lib/field_hub/couch_service.ex b/server/lib/field_hub/couch_service.ex index 8bef9e4d54..dc67907722 100644 --- a/server/lib/field_hub/couch_service.ex +++ b/server/lib/field_hub/couch_service.ex @@ -340,15 +340,24 @@ defmodule FieldHub.CouchService do end @doc """ - Get CouchDB's info for the last change of a given project. + Returns the information about the last change for the specified project. + Otherwise, it returns a map representing the last change if it exists. __Parameters__ - - `project_identifier` the project's name. + - `project_identifier` - The name of the project. + + ## Example + iex> get_last_change_info("development") + %{ + "seq" => "68-89221cbd0e434ddf10a997c068b293b6", + "id" => "project", + "changes" => [%{"rev" => "2-ed2a74eb95293ba7be1f66b070fe278e"}] + } """ def get_last_change_info(project_identifier) do changes = HTTPoison.get!( - "#{base_url()}/#{project_identifier}/_changes", + "#{base_url()}/#{project_identifier}/_changes?style=all_docs", get_user_credentials() |> headers() ) @@ -358,17 +367,32 @@ defmodule FieldHub.CouchService do last_sequence = Map.get(changes, "last_seq") case changes do - %{"results" => []} -> 0 - %{"results" => result } -> - Enum.find(result,fn map -> map["seq"] == last_sequence end) + %{"results" => []} -> + %{ + "seq" => last_sequence, + "id" => "project", + "changes" => [] + } + %{"results" => result} -> + Enum.find(result, fn map -> map["seq"] == last_sequence end) 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 André Leroi-Gourhan)" + + """ def get_last_change_date(project_identifier) do case Map.get(get_last_change_info(project_identifier), "id") do - :no_changes_found -> - :no_changes_found - + nil -> :no_changes_found result -> HTTPoison.get!( "#{base_url()}/#{project_identifier}/#{result}",