lib/toolbox/incident/id_generator.ex

defmodule Toolbox.Incident.IdGenerator do
  @moduledoc """
  Module provides functions to generate incident id.
  """

  @doc """
  Generates incident id based on incident type, timestamp and serialization_vector.

  Serialization vector is list of attributes relevant which together with type and timestamp
  identifies given incident.

  iex> Toolbox.Incident.IdGenerator.generate("/incident/type", 0123456789, ["/asset/type/1"])
  "/incident/type/f4c3877a-d50b-3a16-b6ec-3ebb2b308d40"

  """
  def generate(incident_type, timestamp, vector) do
    generated = UUID.uuid3(nil, :erlang.term_to_binary([incident_type, timestamp, vector]))

    if String.ends_with?(incident_type, "/") do
      "#{incident_type}#{generated}"
    else
      "#{incident_type}/#{generated}"
    end
  end
end