shoc-backend/Api.SeaHavenIndustries/Helper/ZipCodeDistance.cs
Alexandre Brandizzi 7d245eb717
refactor: enforce backend boundaries and optimize dispatch (#30)
* refactor(api): enforce service and data-service boundaries

* refactor(api): complete feature service boundaries

* refactor(identity): enforce service and data boundaries

* refactor(vendors): enforce service and data boundaries

* refactor(workorders): enforce service and data boundaries

* refactor(backend): enforce architecture and optimize dispatch

* style(backend): format changed architecture files

* fix(architecture): address backend review follow-ups

* fix(backend): sanitize exception disclosure in changed API endpoints

Replace raw exception-message disclosure (ex.Message) returned to API
callers with a stable sanitized public message plus correlated structured
internal logging, across the endpoints changed in this PR.

- Add SanitizedErrors helper: logs the original exception at Error with a
  generated correlation id and returns a stable public message referencing
  it so support can trace without exposing internals.
- Inject ILogger<T> into the 14 changed controllers and route every
  ex.Message/dbex.Message disclosure through the helper, preserving status
  codes, response shapes, and business data (e.g. OpenWorkOrders).
- Leave FluentValidation (vex.Errors) and existing fixed-message catches
  untouched; out-of-scope controllers (Account/Contact/Employee/Asset/
  PMSchedule) are unchanged.
- Add focused tests proving internal exception text is not returned and
  that Error logging carrying the original exception is invoked.

* fix(architecture): abstract job run state access

* style: format board update service

* test: use collection assertion idiom
2026-07-24 17:35:34 -03:00

50 lines
1.9 KiB
C#

using SeaHaven.Services.Interfaces;
namespace Api.SeaHavenIndustries.Helper
{
public class ZipCodeDistance : IZipCodeDistance
{
private readonly Dictionary<string, (double lat, double lng)> _coords = new();
private bool _loaded;
public void Load(string csvPath)
{
if (_loaded) return;
foreach (var line in File.ReadLines(csvPath).Skip(1))
{
var parts = line.Split(',');
if (parts.Length >= 3
&& double.TryParse(parts[1].Trim(), out var lat)
&& double.TryParse(parts[2].Trim(), out var lng))
{
_coords[parts[0].Trim().PadLeft(5, '0')] = (lat, lng);
}
}
_loaded = true;
}
public double? GetDistanceMiles(string? zip1, string? zip2)
{
if (string.IsNullOrWhiteSpace(zip1) || string.IsNullOrWhiteSpace(zip2)) return null;
var z1 = zip1.Trim().PadLeft(5, '0');
var z2 = zip2.Trim().PadLeft(5, '0');
if (z1.Length > 5) z1 = z1.Substring(0, 5);
if (z2.Length > 5) z2 = z2.Substring(0, 5);
if (!_coords.TryGetValue(z1, out var c1) || !_coords.TryGetValue(z2, out var c2)) return null;
return Haversine(c1.lat, c1.lng, c2.lat, c2.lng);
}
private static double Haversine(double lat1, double lon1, double lat2, double lon2)
{
const double R = 3958.8;
var dLat = ToRad(lat2 - lat1);
var dLon = ToRad(lon2 - lon1);
var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
Math.Cos(ToRad(lat1)) * Math.Cos(ToRad(lat2)) *
Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
return R * 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
}
private static double ToRad(double deg) => deg * Math.PI / 180.0;
}
}