39eab2e3a2
Extends RegionService, RegionRepository, and RegionController with the endpoints from mainPlan.md sec 2 beyond plain CRUD: setRegionalTarget, setRegionalBudget, mapAgencyToRegion, setRegionalTaxRule, getRegionHierarchy, and getRegionDashboard. Target/budget/tax-rule mutation is gated to users with region scope at RegionalManager level or RegionalAgency level across all regions; mapAgencyToRegion links an existing User holding the RegionalAgency role rather than introducing a separate agency entity, matching the convention already used by Claim.FK_AgencyUserId elsewhere in the codebase. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
82 lines
3.5 KiB
C#
82 lines
3.5 KiB
C#
using Distribution.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Distribution.Features.Region;
|
|
|
|
public class RegionRepository(AppDBContext context) : IRegionRepository
|
|
{
|
|
public async Task<Models.Region?> GetByIdAsync(Guid id) =>
|
|
await context.Regions.FindAsync(id);
|
|
|
|
public async Task<List<Models.Region>> GetAllAsync() =>
|
|
await context.Regions.ToListAsync();
|
|
|
|
public async Task AddAsync(Models.Region entity) =>
|
|
await context.Regions.AddAsync(entity);
|
|
|
|
public void Update(Models.Region entity) =>
|
|
context.Regions.Update(entity);
|
|
|
|
public void Remove(Models.Region entity)
|
|
{
|
|
entity.IsActive = false;
|
|
context.Regions.Update(entity);
|
|
}
|
|
|
|
public async Task<Models.RegionalTarget?> GetTargetAsync(Guid regionId, string period) =>
|
|
await context.RegionalTargets.FirstOrDefaultAsync(t =>
|
|
t.FK_RegionId == regionId && t.Period == period && t.IsActive);
|
|
|
|
public async Task AddTargetAsync(Models.RegionalTarget target) =>
|
|
await context.RegionalTargets.AddAsync(target);
|
|
|
|
public void UpdateTarget(Models.RegionalTarget target) =>
|
|
context.RegionalTargets.Update(target);
|
|
|
|
public async Task<Models.RegionalBudget?> GetBudgetAsync(Guid regionId, string period) =>
|
|
await context.RegionalBudgets.FirstOrDefaultAsync(b =>
|
|
b.FK_RegionId == regionId && b.Period == period && b.IsActive);
|
|
|
|
public async Task AddBudgetAsync(Models.RegionalBudget budget) =>
|
|
await context.RegionalBudgets.AddAsync(budget);
|
|
|
|
public void UpdateBudget(Models.RegionalBudget budget) =>
|
|
context.RegionalBudgets.Update(budget);
|
|
|
|
public async Task<Models.RegionAgencyMap?> GetAgencyMapAsync(Guid regionId, Guid agencyUserId) =>
|
|
await context.RegionAgencyMaps.FirstOrDefaultAsync(m =>
|
|
m.FK_RegionId == regionId && m.FK_AgencyUserId == agencyUserId && m.IsActive);
|
|
|
|
public async Task AddAgencyMapAsync(Models.RegionAgencyMap map) =>
|
|
await context.RegionAgencyMaps.AddAsync(map);
|
|
|
|
public async Task<Models.RegionalTaxRule?> GetTaxRuleAsync(Guid regionId) =>
|
|
await context.RegionalTaxRules.FirstOrDefaultAsync(r => r.FK_RegionId == regionId && r.IsActive);
|
|
|
|
public async Task AddTaxRuleAsync(Models.RegionalTaxRule rule) =>
|
|
await context.RegionalTaxRules.AddAsync(rule);
|
|
|
|
public void UpdateTaxRule(Models.RegionalTaxRule rule) =>
|
|
context.RegionalTaxRules.Update(rule);
|
|
|
|
public async Task<int?> GetUserRoleLevelAsync(Guid userId) =>
|
|
await context.Users
|
|
.Where(u => u.Id == userId && u.IsActive)
|
|
.Select(u => (int?)u.Role.Level)
|
|
.FirstOrDefaultAsync();
|
|
|
|
public async Task<List<Models.Route>> GetRoutesAsync(Guid regionId) =>
|
|
await context.Routes.Where(r => r.FK_RegionId == regionId && r.IsActive).ToListAsync();
|
|
|
|
public async Task<List<Models.Store>> GetStoresByRouteIdsAsync(List<Guid> routeIds) =>
|
|
await context.Stores.Where(s => routeIds.Contains(s.FK_RouteId) && s.IsActive).ToListAsync();
|
|
|
|
public async Task<List<Models.Order>> GetOrdersForDashboardAsync(Guid regionId, DateTime periodStart, DateTime periodEndExclusive) =>
|
|
await (from o in context.Orders
|
|
join s in context.Stores on o.FK_StoreId equals s.Id
|
|
join r in context.Routes on s.FK_RouteId equals r.Id
|
|
where r.FK_RegionId == regionId && o.IsActive && s.IsActive && r.IsActive
|
|
&& o.OrderDate >= periodStart && o.OrderDate < periodEndExclusive
|
|
select o).ToListAsync();
|
|
}
|