This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store transactions into SyncLog and return them via separate endpoint so clients can get entries to sync.
- Loading branch information
1 parent
f044556
commit ef1bd12
Showing
31 changed files
with
527 additions
and
317 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule ExMoney.SyncLogApi.SyncLog do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "sync_log" do | ||
field :uid, :string | ||
field :action, :string | ||
field :entity, :string | ||
field :payload, :map | ||
field :synced_at, :naive_datetime | ||
|
||
timestamps() | ||
end | ||
|
||
def changeset(model, params \\ %{}) do | ||
model | ||
|> cast(params, [:action, :entity, :payload, :synced_at]) | ||
|> validate_required([:action, :entity, :payload]) | ||
|> generate_uid() | ||
end | ||
|
||
defp generate_uid(changeset) do | ||
Ecto.Changeset.put_change(changeset, :uid, Ecto.UUID.generate()) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule ExMoney.SyncLogApi do | ||
@moduledoc """ | ||
Api functions to sync things. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias ExMoney.Repo | ||
|
||
alias ExMoney.SyncLogApi.SyncLog | ||
|
||
def store(entity, action, payload) do | ||
%SyncLog{} | ||
|> SyncLog.changeset(%{action: action, entity: entity, payload: payload}) | ||
|> Repo.insert | ||
end | ||
|
||
def get(per_page) do | ||
query = | ||
from sl in SyncLog, | ||
where: is_nil(sl.synced_at), | ||
order_by: [asc: sl.inserted_at], | ||
limit: ^per_page | ||
|
||
Repo.all(query) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule ExMoney.SyncLogWorker do | ||
use GenServer | ||
require Logger | ||
|
||
alias ExMoney.{SyncLogApi, Transactions} | ||
|
||
def start_link(_opts \\ []) do | ||
GenServer.start_link(__MODULE__, :ok, name: :sync_log_worker) | ||
end | ||
|
||
def handle_cast({:store_transaction, transaction_id}, state) do | ||
payload = | ||
Transactions.get_transaction(transaction_id) | ||
|> Map.from_struct() | ||
|> Map.drop([:__meta__, :user_id, :account, :category, :saltedge_account, :user]) | ||
|
||
SyncLogApi.store("Transaction", "create", payload) | ||
|
||
{:noreply, state} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
defmodule ExMoney.Transactions.Transaction do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "transactions" do | ||
field :saltedge_transaction_id, :integer | ||
field :mode, :string | ||
field :status, :string | ||
field :made_on, :date | ||
field :amount, :decimal | ||
field :currency_code, :string | ||
field :description, :string | ||
field :duplicated, :boolean, default: false | ||
field :rule_applied, :boolean, default: false | ||
field :extra, :map | ||
field :uid, :string | ||
|
||
belongs_to :category, ExMoney.Category | ||
belongs_to :user, ExMoney.User | ||
belongs_to :account, ExMoney.Account | ||
belongs_to :saltedge_account, ExMoney.Account, | ||
foreign_key: :saltedge_account_id, | ||
references: :saltedge_account_id | ||
|
||
timestamps() | ||
end | ||
|
||
@required_fields ~w( | ||
saltedge_transaction_id | ||
mode | ||
status | ||
made_on | ||
amount | ||
currency_code | ||
description | ||
duplicated | ||
saltedge_account_id | ||
account_id | ||
user_id | ||
)a | ||
@optional_fields ~w(category_id rule_applied extra)a | ||
|
||
def changeset(model, params \\ %{}) do | ||
model | ||
|> cast(params, @required_fields ++ @optional_fields) | ||
|> validate_required(@required_fields) | ||
|> generate_uid() | ||
end | ||
|
||
def changeset_custom(model, params \\ %{}) do | ||
model | ||
|> cast(params, ~w(amount category_id account_id made_on user_id description extra)a) | ||
|> validate_required(~w(amount category_id account_id made_on user_id)a) | ||
|> negate_amount(params) | ||
|> generate_uid() | ||
end | ||
|
||
def update_changeset(model, params \\ %{}) do | ||
model | ||
|> cast(params, ~w(category_id description rule_applied extra)a) | ||
end | ||
|
||
def negate_amount(changeset, params) when params == %{}, do: changeset | ||
def negate_amount(changeset, %{"type" => "income"}), do: changeset | ||
|
||
def negate_amount(changeset, %{"type" => "expense"}) do | ||
case Ecto.Changeset.fetch_change(changeset, :amount) do | ||
{:ok, amount} -> | ||
Ecto.Changeset.put_change(changeset, :amount, Decimal.mult(amount, Decimal.new(-1))) | ||
:error -> changeset | ||
end | ||
end | ||
|
||
defp generate_uid(changeset) do | ||
Ecto.Changeset.put_change(changeset, :uid, Ecto.UUID.generate()) | ||
end | ||
end |
Oops, something went wrong.