Add region manager assignment endpoints
This commit is contained in:
@@ -91,6 +91,67 @@ public class RegionService(IRegionRepository repo, IUnitOfWork uow, ICurrentUser
|
||||
return new RegionResponse(region.Id, region.Code, region.Name, region.IsActive, region.CreatedAt);
|
||||
}
|
||||
|
||||
private const string RegionalManagerRole = "RegionalManager";
|
||||
|
||||
public async Task<RegionManagerResponse?> GetManagerAsync(Guid regionId)
|
||||
{
|
||||
var region = await repo.GetByIdAsync(regionId);
|
||||
if (region is null || !region.IsActive) return null;
|
||||
var scope = (await repo.GetManagerScopesAsync(regionId)).FirstOrDefault();
|
||||
return scope is null ? null : new RegionManagerResponse(regionId, scope.FK_UserId, scope.User.Username, scope.User.FullName);
|
||||
}
|
||||
|
||||
// A region has exactly one manager: assigning replaces any existing RegionalManager scoped to it.
|
||||
// Returns null when the region does not exist; throws InvalidOperationException for an invalid user.
|
||||
public async Task<RegionManagerResponse?> SetManagerAsync(Guid regionId, SetRegionManagerRequest request, CancellationToken ct = default)
|
||||
{
|
||||
var region = await repo.GetByIdAsync(regionId);
|
||||
if (region is null || !region.IsActive) return null;
|
||||
|
||||
var user = await repo.GetUserWithRoleAsync(request.UserId);
|
||||
if (user is null || !user.IsActive)
|
||||
throw new InvalidOperationException("User not found.");
|
||||
if (user.Role?.Name != RegionalManagerRole)
|
||||
throw new InvalidOperationException($"User must have the {RegionalManagerRole} role to manage a region.");
|
||||
|
||||
await uow.BeginAsync(ct);
|
||||
try
|
||||
{
|
||||
var existing = await repo.GetManagerScopesAsync(regionId);
|
||||
repo.RemoveRegionScopes(existing.Where(s => s.FK_UserId != user.Id));
|
||||
if (await repo.GetRegionScopeAsync(user.Id, regionId) is null)
|
||||
await repo.AddRegionScopeAsync(new Models.UserRegionScope { FK_UserId = user.Id, FK_RegionId = regionId });
|
||||
await uow.CommitAsync(ct);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await uow.RollbackAsync(ct);
|
||||
throw;
|
||||
}
|
||||
return new RegionManagerResponse(regionId, user.Id, user.Username, user.FullName);
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveManagerAsync(Guid regionId, CancellationToken ct = default)
|
||||
{
|
||||
var region = await repo.GetByIdAsync(regionId);
|
||||
if (region is null || !region.IsActive) return false;
|
||||
var existing = await repo.GetManagerScopesAsync(regionId);
|
||||
if (existing.Count == 0) return false;
|
||||
|
||||
await uow.BeginAsync(ct);
|
||||
try
|
||||
{
|
||||
repo.RemoveRegionScopes(existing);
|
||||
await uow.CommitAsync(ct);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await uow.RollbackAsync(ct);
|
||||
throw;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default)
|
||||
{
|
||||
var region = await repo.GetByIdAsync(id);
|
||||
|
||||
Reference in New Issue
Block a user