InvenTrack pushes low-stock alerts to a dashboard over SignalR. The rule that decides whether an alert fires is two clauses long, and one of them is a design decision rather than an obvious check.
if (transaction.Type != StockTransactionType.Out || !product.IsLowStock)
{
return;
}
The second clause is the threshold, and it lives on the entity beside the data it depends on: IsLowStock is QuantityInStock <= LowStockThreshold. The first clause is the one worth explaining.
Why inbound movements stay quiet
A stock movement is either In or Out. Receiving twenty units of a product sitting well below its threshold still leaves it below the threshold, so a naive "is it low after this write" check would fire an alert. That alert would be technically true and actively unhelpful: it reports a situation that just got better as though something had gone wrong.
An inbound movement that still leaves the product below its threshold is an improving situation and does not warrant an alert.
That sentence is the comment above the method, and it is the whole justification for the first clause. The alert is not really about the level being low — it is about the level being low and having just got lower.
What this is and is not
This is a level-triggered alert with a direction filter. It fires on every outbound movement while the product is low, so shipping ten units one at a time from an already-low product produces ten alerts. It is not edge-triggered: nothing compares the previous quantity with the new one, so nothing detects the single moment the threshold is crossed.
Edge-triggering is the better answer for alert volume, and it is a real piece of work rather than a one-line change — the handler would need the previous value, which means either an extra read or carrying it through from the mutation. The direction filter is the cheap approximation, and it removes the most annoying category of false alert for the cost of one enum comparison.
Two things the transport gets right
The hub is typed. Clients are declared as an interface rather than magic strings, so renaming a message breaks the build instead of production:
public interface IStockAlertClient
{
/// <summary>Raised only when a movement leaves a product at or below its threshold.</summary>
Task LowStockAlert(LowStockAlert alert);
/// <summary>Raised for every committed movement, so open screens can refresh their data.</summary>
Task StockChanged(StockChangedNotification notification);
}
And delivery failures cannot fail the request. The stock movement is already committed by the time the alert is published, so the publish is wrapped and a failure is logged rather than thrown — the interface says so explicitly, and the caller catches anyway. Losing an alert is bad; rolling back a committed warehouse movement because a websocket was unhappy is worse.
Notifying about a state change is a side effect of that change, not part of it. If the notification can fail the write, it is wired in the wrong direction.