130 lines
4.7 KiB
C#
130 lines
4.7 KiB
C#
using Distribution.Infra.Authorization;
|
|
using Distribution.Models;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Distribution.Features.Store;
|
|
|
|
[ApiController]
|
|
[Route("api/stores")]
|
|
public class StoreController(
|
|
StoreService service,
|
|
IScopeAccessService scopeAccess,
|
|
ICurrentUserService currentUser) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
[Authorize("store.view")]
|
|
public async Task<IActionResult> GetAll(
|
|
[FromQuery] Guid? routeId,
|
|
[FromQuery] string? search,
|
|
[FromQuery] int page = 1,
|
|
[FromQuery] int pageSize = 20) =>
|
|
Ok(await service.GetAllAsync(routeId, search, page, pageSize));
|
|
|
|
[HttpGet("{id:guid}")]
|
|
[Authorize("store.view")]
|
|
public async Task<IActionResult> GetById(Guid id)
|
|
{
|
|
var userId = currentUser.UserId;
|
|
if (userId.HasValue && !await scopeAccess.HasStoreScopeAsync(userId.Value, id))
|
|
return StatusCode(403);
|
|
var result = await service.GetByIdAsync(id);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpPost]
|
|
[Authorize("store.manage")]
|
|
public async Task<IActionResult> Create(CreateStoreRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.CreateAsync(request, ct);
|
|
return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
|
|
}
|
|
|
|
[HttpPut("{id:guid}")]
|
|
[Authorize("store.manage")]
|
|
public async Task<IActionResult> Update(Guid id, UpdateStoreRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.UpdateAsync(id, request, ct);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpDelete("{id:guid}")]
|
|
[Authorize("store.manage")]
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
|
{
|
|
var deleted = await service.DeleteAsync(id, ct);
|
|
return deleted ? NoContent() : NotFound();
|
|
}
|
|
|
|
[HttpPut("{id:guid}/credit-limit")]
|
|
[Authorize("store.manage")]
|
|
public async Task<IActionResult> UpdateCreditLimit(Guid id, UpdateCreditLimitRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.UpdateCreditLimitAsync(id, request, ct);
|
|
return result is null ? StatusCode(StatusCodes.Status403Forbidden) : Ok(result);
|
|
}
|
|
|
|
[HttpPut("{id:guid}/outstanding-balance")]
|
|
[Authorize("store.manage")]
|
|
public async Task<IActionResult> UpdateOutstandingBalance(Guid id, UpdateOutstandingBalanceRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.UpdateOutstandingBalanceAsync(id, request, ct);
|
|
return result is null ? StatusCode(StatusCodes.Status403Forbidden) : Ok(result);
|
|
}
|
|
|
|
[HttpGet("{id:guid}/outstanding-balance")]
|
|
[Authorize("store.view")]
|
|
public async Task<IActionResult> GetOutstandingBalance(Guid id)
|
|
{
|
|
var result = await service.GetOutstandingBalanceAsync(id);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpPut("{id:guid}/segment")]
|
|
[Authorize("store.manage")]
|
|
public async Task<IActionResult> ClassifySegment(Guid id, ClassifySegmentRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.ClassifySegmentAsync(id, request, ct);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpGet("{id:guid}/visit-history")]
|
|
[Authorize("store.view")]
|
|
public async Task<IActionResult> GetVisitHistory(Guid id)
|
|
{
|
|
var result = await service.GetVisitHistoryAsync(id);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpGet("{id:guid}/schemes/{schemeId:guid}/eligibility")]
|
|
[Authorize("store.view")]
|
|
public async Task<IActionResult> CheckSchemeEligibility(Guid id, Guid schemeId)
|
|
{
|
|
var result = await service.CheckSchemeEligibilityAsync(id, schemeId);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpGet("{id:guid}/dashboard")]
|
|
[Authorize("store.view")]
|
|
public async Task<IActionResult> GetDashboard(Guid id, [FromQuery] string period)
|
|
{
|
|
var result = await service.GetDashboardAsync(id, period);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpGet("{id:guid}/analytics")]
|
|
public async Task<IActionResult> GetAnalytics(Guid id, [FromQuery] int months = 12, [FromQuery] int overdueDays = 30)
|
|
{
|
|
var result = await service.GetAnalyticsAsync(id, months, overdueDays);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
|
|
[HttpPatch("{id:guid}/status")]
|
|
[Authorize("store.manage")]
|
|
public async Task<IActionResult> PatchStatus(Guid id, PatchStoreStatusRequest request, CancellationToken ct)
|
|
{
|
|
var result = await service.SetActiveStatusAsync(id, request.IsActive, ct);
|
|
return result is null ? NotFound() : Ok(result);
|
|
}
|
|
}
|