fix: add course value normalization in changeset

Normalize course values to valid range (0-359 degrees) during packet
insertion. Invalid course values (negative or >= 360) are automatically
set to 0, preventing bad data from entering the database.

This complements the fix in the APRS parser and ensures data integrity
at the application level as well.
This commit is contained in:
Graham McIntire 2026-03-22 13:44:46 -05:00
parent 2ca0cf9d39
commit ca9bcf21a7
No known key found for this signature in database

View file

@ -148,6 +148,7 @@ defmodule Aprsme.Packet do
|> maybe_create_geometry_from_lat_lon()
|> maybe_set_has_position()
|> normalize_symbols()
|> normalize_course()
end
defp normalize_symbols(changeset) do
@ -182,6 +183,25 @@ defmodule Aprsme.Packet do
put_change(changeset, :symbol_table_id, "/")
end
defp normalize_course(changeset) do
course = get_field(changeset, :course) || get_change(changeset, :course)
case course do
nil ->
changeset
c when is_integer(c) and c >= 0 and c <= 359 ->
changeset
c when is_integer(c) ->
# Invalid course value (negative or >= 360), normalize to 0
put_change(changeset, :course, 0)
_ ->
changeset
end
end
@spec maybe_create_geometry_from_lat_lon(Ecto.Changeset.t()) :: Ecto.Changeset.t()
defp maybe_create_geometry_from_lat_lon(changeset) do
lat = get_field(changeset, :lat) || get_change(changeset, :lat)