Skip to content

Commit 98b9471

Browse files
authored
Merge pull request #928 from bcgov/feature/DSS-1149
DSS-1149
2 parents d5c6fa6 + 535ce74 commit 98b9471

File tree

2 files changed

+164
-1
lines changed

2 files changed

+164
-1
lines changed

server/StrDss.Api/Controllers/NetworkCheckerController.cs

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
using Asp.Versioning;
22
using Microsoft.AspNetCore.Authorization;
33
using Microsoft.AspNetCore.Mvc;
4+
using NetTopologySuite.Geometries;
5+
using StrDss.Data.Entities;
6+
using StrDss.Data.Repositories;
7+
using StrDss.Service.HttpClients;
48

59
namespace StrDss.Api.Controllers
610
{
@@ -9,11 +13,170 @@ namespace StrDss.Api.Controllers
913
[ApiController]
1014
public class NetworkCheckerController : ControllerBase
1115
{
16+
private readonly IGeocoderApi _geocoder;
17+
private readonly IOrganizationRepository _orgRepo;
18+
19+
public NetworkCheckerController(IGeocoderApi geocoder, IOrganizationRepository orgRepo)
20+
{
21+
_geocoder = geocoder;
22+
_orgRepo = orgRepo;
23+
}
24+
1225
[AllowAnonymous]
1326
[HttpGet]
1427
public IActionResult GetNetworkStatus()
1528
{
1629
return Ok(new { status = true });
1730
}
31+
32+
[AllowAnonymous]
33+
[HttpGet("geocoder")]
34+
public async Task<IActionResult> TestGeocoder([FromQuery] string address)
35+
{
36+
if (string.IsNullOrWhiteSpace(address))
37+
return BadRequest(new { error = "Address query parameter is required." });
38+
39+
try
40+
{
41+
var physicalAddress = new DssPhysicalAddress
42+
{
43+
OriginalAddressTxt = address
44+
};
45+
46+
var error = await _geocoder.GetAddressAsync(physicalAddress);
47+
48+
return Ok(new
49+
{
50+
success = string.IsNullOrEmpty(error),
51+
error = error,
52+
originalAddress = physicalAddress.OriginalAddressTxt,
53+
matchedAddress = physicalAddress.MatchAddressTxt,
54+
matchScore = physicalAddress.MatchScoreAmt,
55+
siteNo = physicalAddress.SiteNo,
56+
civicNo = physicalAddress.CivicNo,
57+
streetName = physicalAddress.StreetNm,
58+
locality = physicalAddress.LocalityNm,
59+
province = physicalAddress.ProvinceCd,
60+
containingOrganizationId = physicalAddress.ContainingOrganizationId,
61+
hasLocationGeometry = physicalAddress.LocationGeometry != null
62+
});
63+
}
64+
catch (Exception ex)
65+
{
66+
return Ok(new
67+
{
68+
success = false,
69+
error = $"Exception occurred: {ex.Message}"
70+
});
71+
}
72+
}
73+
74+
[AllowAnonymous]
75+
[HttpGet("jurisdiction")]
76+
public async Task<IActionResult> GetJurisdictionInfo([FromQuery] string address)
77+
{
78+
if (string.IsNullOrWhiteSpace(address))
79+
return BadRequest(new { error = "Address query parameter is required." });
80+
81+
try
82+
{
83+
// First, geocode the address
84+
var physicalAddress = new DssPhysicalAddress
85+
{
86+
OriginalAddressTxt = address
87+
};
88+
89+
var geocoderError = await _geocoder.GetAddressAsync(physicalAddress);
90+
91+
if (!string.IsNullOrEmpty(geocoderError))
92+
{
93+
return Ok(new
94+
{
95+
success = false,
96+
error = $"Geocoder error: {geocoderError}",
97+
originalAddress = address
98+
});
99+
}
100+
101+
if (physicalAddress.LocationGeometry == null || !(physicalAddress.LocationGeometry is Point point))
102+
{
103+
return Ok(new
104+
{
105+
success = false,
106+
error = "No valid location geometry found for the address",
107+
originalAddress = address,
108+
matchedAddress = physicalAddress.MatchAddressTxt
109+
});
110+
}
111+
112+
// Look up the containing organization
113+
var containingOrganizationId = await _orgRepo.GetContainingOrganizationId(point);
114+
115+
if (containingOrganizationId == null)
116+
{
117+
return Ok(new
118+
{
119+
success = true,
120+
originalAddress = address,
121+
matchedAddress = physicalAddress.MatchAddressTxt,
122+
locationFound = true,
123+
jurisdictionFound = false,
124+
message = "No containing jurisdiction found for this location"
125+
});
126+
}
127+
128+
// Get the organization details
129+
var organization = await _orgRepo.GetOrganizationByIdAsync(containingOrganizationId.Value);
130+
131+
if (organization == null)
132+
{
133+
return Ok(new
134+
{
135+
success = true,
136+
originalAddress = address,
137+
matchedAddress = physicalAddress.MatchAddressTxt,
138+
locationFound = true,
139+
jurisdictionFound = false,
140+
containingOrganizationId = containingOrganizationId.Value,
141+
message = "Organization not found for the containing organization ID"
142+
});
143+
}
144+
145+
return Ok(new
146+
{
147+
success = true,
148+
originalAddress = address,
149+
matchedAddress = physicalAddress.MatchAddressTxt,
150+
locationFound = true,
151+
jurisdictionFound = true,
152+
jurisdiction = new
153+
{
154+
organizationId = organization.OrganizationId,
155+
organizationName = organization.OrganizationNm,
156+
organizationType = organization.OrganizationType,
157+
organizationCode = organization.OrganizationCd,
158+
isStraaExempt = organization.IsStraaExempt
159+
},
160+
geocoderInfo = new
161+
{
162+
matchScore = physicalAddress.MatchScoreAmt,
163+
siteNo = physicalAddress.SiteNo,
164+
civicNo = physicalAddress.CivicNo,
165+
streetName = physicalAddress.StreetNm,
166+
locality = physicalAddress.LocalityNm,
167+
province = physicalAddress.ProvinceCd
168+
}
169+
});
170+
}
171+
catch (Exception ex)
172+
{
173+
return Ok(new
174+
{
175+
success = false,
176+
error = $"Exception occurred: {ex.Message}",
177+
originalAddress = address
178+
});
179+
}
180+
}
18181
}
19182
}

server/StrDss.Service/HttpClients/GeocoderApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public async Task<string> GetAddressAsync(DssPhysicalAddress address)
6969
address.IsMatchVerified = false;
7070
}
7171

72-
return "";
72+
return address.MatchScoreAmt < _config.GetValue<int>("GEOCODER_MATCH_SCORE_THRESHOLD") ? "No address found, Geocoder match score: " + address.MatchScoreAmt : "";
7373
}
7474
catch (Exception ex)
7575
{

0 commit comments

Comments
 (0)