Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions lib/pulse/portfolio_worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,21 @@ defmodule Pulse.PortfolioWorker do
# LiveView templates already guard for it.
stats = payload["stats"]

# On store restore the payload includes the original updated_at so a server
# restart doesn't make every portfolio look freshly active. Otherwise stamp
# with now.
updated_at = payload["__updated_at"] || DateTime.utc_now()
# `updated_at` should reflect when the user *materially* changed their
# portfolio — adding/removing stocks or editing qty/avg_price — not every
# NATS payload (Quantic also fires one on each periodic stock-price
# refresh, which would otherwise make every dashboard look freshly active
# at the same time). The restore path passes `__updated_at` to bypass
# this check and preserve the historical timestamp.
override = payload["__updated_at"]
materially_changed = holdings_signature(holdings) != holdings_signature(state.holdings)

updated_at =
cond do
override != nil -> override
materially_changed -> DateTime.utc_now()
true -> state.updated_at
end

new_state = %{
state
Expand Down Expand Up @@ -148,4 +159,14 @@ defmodule Pulse.PortfolioWorker do
# plain math.
defp holding_value_in_usd(%{"value_in_usd" => value}) when is_number(value), do: value
defp holding_value_in_usd(h), do: holding_value(h)

# Deterministic fingerprint of what the user controls (symbol set, quantities,
# cost basis). Excludes price-derived fields so a stock-refresh payload that
# only updates `price` / `value_in_base` / `value_in_usd` doesn't register
# as a material change.
defp holdings_signature(holdings) do
holdings
|> Enum.map(fn h -> {h["symbol"], h["quantity"], h["avg_price"]} end)
|> Enum.sort()
end
end
76 changes: 76 additions & 0 deletions test/pulse/portfolio_worker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,80 @@ defmodule Pulse.PortfolioWorkerTest do
portfolio = Pulse.PortfolioWorker.get_portfolio("test-nostats")
assert portfolio.stats == nil
end

describe "updated_at material-change detection" do
setup do
{:ok, _pid} = Pulse.PortfolioSupervisor.start_worker("test-mc")

Pulse.PortfolioWorker.update_holdings("test-mc", %{
"holdings" => [
%{"symbol" => "AAPL", "quantity" => 10, "avg_price" => 150.0, "price" => 150.0}
]
})

Process.sleep(50)
initial = Pulse.PortfolioWorker.get_portfolio("test-mc").updated_at
assert %DateTime{} = initial

[initial_updated_at: initial]
end

test "price-only payloads do NOT bump updated_at", %{initial_updated_at: initial} do
Process.sleep(20)

Pulse.PortfolioWorker.update_holdings("test-mc", %{
"holdings" => [
%{"symbol" => "AAPL", "quantity" => 10, "avg_price" => 150.0, "price" => 165.0}
]
})

Process.sleep(50)
portfolio = Pulse.PortfolioWorker.get_portfolio("test-mc")
assert portfolio.updated_at == initial
end

test "adding a holding bumps updated_at", %{initial_updated_at: initial} do
Process.sleep(20)

Pulse.PortfolioWorker.update_holdings("test-mc", %{
"holdings" => [
%{"symbol" => "AAPL", "quantity" => 10, "avg_price" => 150.0, "price" => 150.0},
%{"symbol" => "MSFT", "quantity" => 5, "avg_price" => 300.0, "price" => 300.0}
]
})

Process.sleep(50)
portfolio = Pulse.PortfolioWorker.get_portfolio("test-mc")
assert DateTime.compare(portfolio.updated_at, initial) == :gt
end

test "editing quantity bumps updated_at", %{initial_updated_at: initial} do
Process.sleep(20)

Pulse.PortfolioWorker.update_holdings("test-mc", %{
"holdings" => [
%{"symbol" => "AAPL", "quantity" => 15, "avg_price" => 150.0, "price" => 150.0}
]
})

Process.sleep(50)
portfolio = Pulse.PortfolioWorker.get_portfolio("test-mc")
assert DateTime.compare(portfolio.updated_at, initial) == :gt
end

test "__updated_at override (restore path) preserves the historical timestamp" do
preserved = ~U[2026-01-01 12:00:00Z]

Pulse.PortfolioWorker.update_holdings("test-mc", %{
"holdings" => [
%{"symbol" => "AAPL", "quantity" => 99, "avg_price" => 999.0, "price" => 200.0}
],
"__updated_at" => preserved
})

Process.sleep(50)
portfolio = Pulse.PortfolioWorker.get_portfolio("test-mc")
assert portfolio.updated_at == preserved
end
end
end
Loading