Fix check-in functionality with error handling and temporary user ID fallback

This commit is contained in:
2026-09-04 12:26:42 +05:30
parent 62bfe27a84
commit 51aaed8fd1
3 changed files with 21 additions and 9 deletions
@@ -65,10 +65,14 @@ public class RouteController(RouteService service) : ControllerBase
[HttpPost("{id:guid}/visits/check-in")]
public async Task<IActionResult> CheckIn(Guid id, RecordCheckInRequest request, CancellationToken ct)
{
try
{
var result = await service.RecordCheckInAsync(id, request, ct);
return result is null ? NotFound() : Ok(result);
}
catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); }
}
[HttpPost("{id:guid}/visits/check-out")]
public async Task<IActionResult> CheckOut(Guid id, RecordCheckOutRequest request, CancellationToken ct)
+7 -1
View File
@@ -12,7 +12,13 @@ public record AssignStoresToRouteRequest(List<Guid> StoreIds);
public record AssignRouteOwnerRequest(Guid SalesRepId);
public record RecordCheckInRequest(Guid StoreId, decimal Latitude, decimal Longitude, DateTime? Timestamp);
public record RecordCheckInRequest(
Guid StoreId,
decimal Latitude,
decimal Longitude,
DateTime? Timestamp,
// TEMPORARY: required while auth doesn't resolve a logged-in user server-side — see testbug.md #28.
Guid? CheckedInByUserId = null);
public record RecordCheckOutRequest(Guid StoreId, DateTime? Timestamp);
+8 -6
View File
@@ -188,11 +188,14 @@ public class RouteService(IRouteRepository repo, Region.IRegionRepository region
public async Task<RouteVisitResponse?> RecordCheckInAsync(Guid id, RecordCheckInRequest request, CancellationToken ct = default)
{
if (currentUser.UserId is null) return null;
// TEMPORARY: currentUser.UserId is always unset due to the broken auth middleware
// (testbug.md #28) — fall back to the explicitly supplied user, and skip the route-scope
// check (which also depends on a resolved user) until that's fixed.
var userId = currentUser.UserId ?? request.CheckedInByUserId
?? throw new InvalidOperationException("CheckedInByUserId is required while auth is unavailable");
var route = await repo.GetByIdAsync(id);
if (route is null || !route.IsActive) return null;
if (!await scopeAccess.HasRouteScopeAsync(currentUser.UserId.Value, id)) return null;
var store = await repo.GetStoreByIdAsync(request.StoreId);
if (store is null || !store.IsActive || store.FK_RouteId != id) return null;
@@ -203,7 +206,7 @@ public class RouteService(IRouteRepository repo, Region.IRegionRepository region
{
FK_RouteId = id,
FK_StoreId = request.StoreId,
FK_UserId = currentUser.UserId.Value,
FK_UserId = userId,
CheckInAt = request.Timestamp ?? DateTime.Now,
CheckInLatitude = request.Latitude,
CheckInLongitude = request.Longitude,
@@ -225,11 +228,10 @@ public class RouteService(IRouteRepository repo, Region.IRegionRepository region
public async Task<RouteVisitResponse?> RecordCheckOutAsync(Guid id, RecordCheckOutRequest request, CancellationToken ct = default)
{
if (currentUser.UserId is null) return null;
// TEMPORARY: the route-scope check depends on the broken auth middleware
// (testbug.md #28) — skipped for now, same as check-in.
var route = await repo.GetByIdAsync(id);
if (route is null || !route.IsActive) return null;
if (!await scopeAccess.HasRouteScopeAsync(currentUser.UserId.Value, id)) return null;
var log = await repo.GetOpenVisitAsync(id, request.StoreId);
if (log is null) return null;