defmodule DaProductApp.Activity.CustomEventsLog do require Logger import Ecto.Query, warn: false alias DaProductApp.Repo alias DaProductApp.Activity.CustomEventLog @doc """ Returns the list of custom event logs. """ def list_custom_event_logs do Repo.all(CustomEventLog) end @doc """ Gets a single custom event log by ID. """ def get_custom_event_log!(id), do: Repo.get!(CustomEventLog, id) @doc """ Creates a custom event log entry. """ def e(attrs \\ %{}) do %CustomEventLog{} |> CustomEventLog.changeset(attrs) |> Repo.insert() end @doc """ Updates a custom event log entry. """ def update_custom_event_log(%CustomEventLog{} = custom_event_log, attrs) do custom_event_log |> CustomEventLog.changeset(attrs) |> Repo.update() end @doc """ Creates a custom event log. """ def create_custom_event_log(attrs \\ %{}) do Logger.info("Creating custom event log with attributes: #{inspect(attrs)}") %CustomEventLog{} |> CustomEventLog.changeset(attrs) |> Repo.insert() end @doc """ Deletes a custom event log entry. """ def delete_custom_event_log(%CustomEventLog{} = custom_event_log) do Repo.delete(custom_event_log) end @doc """ Returns an `%Ecto.Changeset{}` for tracking custom event log changes. """ def change_custom_event_log(%CustomEventLog{} = custom_event_log, attrs \\ %{}) do CustomEventLog.changeset(custom_event_log, attrs) end @doc """ Updates the transaction_id for a given refrence_id. """ def update_transaction_id(refrence_id, transaction_id) do Logger.info("Updating transaction_id for refrence_id: #{refrence_id}") query = from cel in CustomEventLog, where: cel.refrence_id == ^refrence_id case Repo.update_all(query, set: [transaction_id: to_string(transaction_id)]) do {n, nil} when n > 0 -> Logger.info("Successfully updated #{n} records with transaction_id: #{transaction_id}") {:ok, n} {0, nil} -> Logger.warn("No records found with refrence_id: #{refrence_id}") {:error, :not_found} error -> Logger.error("Error updating transaction_id: #{inspect(error)}") {:error, :update_failed} end end end