From 37042b48b712d09ba2e9a2f7c01169bfe4620f17 Mon Sep 17 00:00:00 2001 From: Ho Ngoc Hai Date: Wed, 4 Mar 2026 10:35:54 +0700 Subject: [PATCH] feat(inventory-service): add shopId filter to transactions endpoint BFF needs to query inventory transactions by shopId. The existing endpoint only supported inventoryItemId. Now accepts either shopId or inventoryItemId as query parameters. Co-Authored-By: Claude Opus 4.6 --- .../Application/Queries/InventoryQueries.cs | 9 ++++++ .../Queries/InventoryQueryHandlers.cs | 30 +++++++++++++++++++ .../Controllers/InventoryController.cs | 22 +++++++++----- .../IInventoryRepository.cs | 10 +++++++ .../Repositories/InventoryRepository.cs | 22 ++++++++++++++ 5 files changed, 86 insertions(+), 7 deletions(-) diff --git a/services/inventory-service-net/src/InventoryService.API/Application/Queries/InventoryQueries.cs b/services/inventory-service-net/src/InventoryService.API/Application/Queries/InventoryQueries.cs index 500610ca..1ecf4e85 100644 --- a/services/inventory-service-net/src/InventoryService.API/Application/Queries/InventoryQueries.cs +++ b/services/inventory-service-net/src/InventoryService.API/Application/Queries/InventoryQueries.cs @@ -32,6 +32,15 @@ public record GetTransactionsQuery( int Skip = 0, int Take = 50) : IRequest>; +/// +/// EN: Query to get transactions for a shop. +/// VI: Query lấy transactions cho một shop. +/// +public record GetTransactionsByShopQuery( + Guid ShopId, + int Skip = 0, + int Take = 50) : IRequest>; + /// /// EN: Query to get low stock items. /// VI: Query lấy các items stock thấp. diff --git a/services/inventory-service-net/src/InventoryService.API/Application/Queries/InventoryQueryHandlers.cs b/services/inventory-service-net/src/InventoryService.API/Application/Queries/InventoryQueryHandlers.cs index 98150b66..89fc036a 100644 --- a/services/inventory-service-net/src/InventoryService.API/Application/Queries/InventoryQueryHandlers.cs +++ b/services/inventory-service-net/src/InventoryService.API/Application/Queries/InventoryQueryHandlers.cs @@ -95,6 +95,36 @@ public class GetTransactionsQueryHandler } } +/// +/// EN: Handler for GetTransactionsByShopQuery. +/// VI: Handler cho GetTransactionsByShopQuery. +/// +public class GetTransactionsByShopQueryHandler + : IRequestHandler> +{ + private readonly IInventoryRepository _repository; + + public GetTransactionsByShopQueryHandler(IInventoryRepository repository) + { + _repository = repository; + } + + public async Task> Handle( + GetTransactionsByShopQuery request, + CancellationToken ct) + { + var (transactions, total) = await _repository.GetTransactionsByShopAsync( + request.ShopId, + request.Skip, + request.Take, + ct); + + var dtos = transactions.Select(t => t.ToDto()).ToList(); + + return new PagedResult(dtos, total); + } +} + /// /// EN: Handler for GetLowStockItemsQuery. /// VI: Handler cho GetLowStockItemsQuery. diff --git a/services/inventory-service-net/src/InventoryService.API/Controllers/InventoryController.cs b/services/inventory-service-net/src/InventoryService.API/Controllers/InventoryController.cs index 2a3b2daf..001e9e4b 100644 --- a/services/inventory-service-net/src/InventoryService.API/Controllers/InventoryController.cs +++ b/services/inventory-service-net/src/InventoryService.API/Controllers/InventoryController.cs @@ -251,23 +251,31 @@ public class InventoryController : ControllerBase } /// - /// EN: Get transaction history for inventory item. - /// VI: Lấy lịch sử transactions cho inventory item. + /// EN: Get transaction history by inventory item ID or shop ID. + /// VI: Lấy lịch sử transactions theo inventory item ID hoặc shop ID. /// [HttpGet("transactions")] - [SwaggerOperation(Summary = "Get transaction history")] + [SwaggerOperation(Summary = "Get transaction history by item or shop")] [SwaggerResponse(200, "Transactions retrieved successfully")] [SwaggerResponse(400, "Invalid request")] public async Task>>> GetTransactions( - [FromQuery] Guid inventoryItemId, + [FromQuery] Guid? inventoryItemId = null, + [FromQuery] Guid? shopId = null, [FromQuery] int skip = 0, [FromQuery] int take = 50, CancellationToken ct = default) { - if (inventoryItemId == Guid.Empty) - return BadRequest(ApiResponse>.Fail("Inventory item ID is required")); + if (shopId.HasValue && shopId.Value != Guid.Empty) + { + var shopQuery = new GetTransactionsByShopQuery(shopId.Value, skip, take); + var shopResult = await _mediator.Send(shopQuery, ct); + return Ok(ApiResponse>.Ok(shopResult)); + } - var query = new GetTransactionsQuery(inventoryItemId, skip, take); + if (!inventoryItemId.HasValue || inventoryItemId.Value == Guid.Empty) + return BadRequest(ApiResponse>.Fail("Either shopId or inventoryItemId is required")); + + var query = new GetTransactionsQuery(inventoryItemId.Value, skip, take); var result = await _mediator.Send(query, ct); return Ok(ApiResponse>.Ok(result)); diff --git a/services/inventory-service-net/src/InventoryService.Domain/AggregatesModel/InventoryAggregate/IInventoryRepository.cs b/services/inventory-service-net/src/InventoryService.Domain/AggregatesModel/InventoryAggregate/IInventoryRepository.cs index 1f4dcba6..90e3abbe 100644 --- a/services/inventory-service-net/src/InventoryService.Domain/AggregatesModel/InventoryAggregate/IInventoryRepository.cs +++ b/services/inventory-service-net/src/InventoryService.Domain/AggregatesModel/InventoryAggregate/IInventoryRepository.cs @@ -77,6 +77,16 @@ public interface IInventoryRepository : IRepository int take = 50, CancellationToken cancellationToken = default); + /// + /// EN: Get all transactions for a shop with pagination. + /// VI: Lấy tất cả transactions cho một shop với phân trang. + /// + Task<(IReadOnlyList Transactions, int Total)> GetTransactionsByShopAsync( + Guid shopId, + int skip = 0, + int take = 50, + CancellationToken cancellationToken = default); + /// /// EN: Add inventory item asynchronously. /// VI: Thêm inventory item bất đồng bộ. diff --git a/services/inventory-service-net/src/InventoryService.Infrastructure/Repositories/InventoryRepository.cs b/services/inventory-service-net/src/InventoryService.Infrastructure/Repositories/InventoryRepository.cs index b098bfb9..1b7a37a8 100644 --- a/services/inventory-service-net/src/InventoryService.Infrastructure/Repositories/InventoryRepository.cs +++ b/services/inventory-service-net/src/InventoryService.Infrastructure/Repositories/InventoryRepository.cs @@ -110,6 +110,28 @@ public class InventoryRepository : IInventoryRepository return (items, total); } + public async Task<(IReadOnlyList Transactions, int Total)> GetTransactionsByShopAsync( + Guid shopId, + int skip = 0, + int take = 50, + CancellationToken cancellationToken = default) + { + var items = await _context.InventoryItems + .Include(i => i.Transactions) + .Where(i => i.ShopId == shopId) + .ToListAsync(cancellationToken); + + var allTransactions = items + .SelectMany(i => i.Transactions) + .OrderByDescending(t => t.CreatedAt) + .ToList(); + + var total = allTransactions.Count; + var paged = allTransactions.Skip(skip).Take(take).ToList(); + + return (paged, total); + } + public async Task AddAsync(InventoryItem item, CancellationToken cancellationToken = default) { var entity = await _context.InventoryItems.AddAsync(item, cancellationToken);