From 8aace3e1d91d6a9c337d4a646656b3f6b9917bce Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Tue, 25 Nov 2025 18:58:46 +0530 Subject: [PATCH] Added an API to get Purchase invoice status list API --- .../Controllers/MasterController.cs | 11 ++++ Marco.Pms.Services/Service/MasterService.cs | 57 +++++++++++++++++++ .../ServiceInterfaces/IMasterService.cs | 4 ++ 3 files changed, 72 insertions(+) diff --git a/Marco.Pms.Services/Controllers/MasterController.cs b/Marco.Pms.Services/Controllers/MasterController.cs index 8668ee3..78cb989 100644 --- a/Marco.Pms.Services/Controllers/MasterController.cs +++ b/Marco.Pms.Services/Controllers/MasterController.cs @@ -70,6 +70,17 @@ namespace Marco.Pms.Services.Controllers #endregion + #region =================================================================== Purchase Invoice Status APIs =================================================================== + [HttpGet("purchase-invoice-status/list")] + public async Task GetPurchaseInvoiceStatus(CancellationToken ct) + { + var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); + var response = await _masterService.GetPurchaseInvoiceStatusAsync(loggedInEmployee, ct); + return StatusCode(response.StatusCode, response); + } + + #endregion + #region =================================================================== Currency APIs =================================================================== [HttpGet("currencies/list")] diff --git a/Marco.Pms.Services/Service/MasterService.cs b/Marco.Pms.Services/Service/MasterService.cs index ed39779..7398aab 100644 --- a/Marco.Pms.Services/Service/MasterService.cs +++ b/Marco.Pms.Services/Service/MasterService.cs @@ -187,6 +187,63 @@ namespace Marco.Pms.Services.Service } #endregion + #region =================================================================== Purchase Invoice Status APIs =================================================================== + + /// + /// Asynchronously retrieves a list of all available Purchase Invoice Statuses. + /// + /// The employee context (used for logging or future permission checks). + /// Token to observe while waiting for the task to complete. + /// A unified API response containing the list of status DTOs. + public async Task> GetPurchaseInvoiceStatusAsync(Employee loggedInEmployee, CancellationToken cancellationToken = default) + { + // 1. Structural Logging: Capture the context of who is performing the action + _logger.LogInfo("Initiating fetch of Purchase Invoice Statuses for User ID: {UserId}", loggedInEmployee.Id); + + try + { + // 2. Performance & Security: Use AsNoTracking, Select specifically (Projection), and handle CancellationToken + var invoiceStatuses = await _context.PurchaseInvoiceStatus + .AsNoTracking() // Critical for read-only queries to avoid overhead of change tracker + .OrderBy(status => status.Name) + .ToListAsync(cancellationToken); // Respect request cancellation + + // 3. Logging Success: Log the count for monitoring purposes + _logger.LogInfo("Successfully fetched {Count} Purchase Invoice Status records.", invoiceStatuses.Count); + + // 4. Strongly Typed Return: Return specific DTOs, not 'object' + return ApiResponse.SuccessResponse( + invoiceStatuses, + $"{invoiceStatuses.Count} record(s) of Purchase invoice status fetched successfully", + 200 + ); + } + catch (OperationCanceledException) + { + // Handle cases where the user cancels the request (browser closed/timeout) + _logger.LogWarning("Purchase Invoice Status fetch was cancelled by the user."); + return ApiResponse.ErrorResponse( + "Request was cancelled", + "Operation cancelled.", + 499 // Client Closed Request + ); + } + catch (Exception ex) + { + // 5. Security & Error Handling: Log full stack trace securely, return generic message to client + _logger.LogError(ex, "Critical error occurred while fetching Purchase Invoice Statuses for User ID: {UserId}", loggedInEmployee.Id); + + // NEVER return 'ex.Message' directly to the client in production (security risk) + return ApiResponse.ErrorResponse( + "An internal error occurred while processing your request. Please contact support.", + "Internal Server Error", + 500 + ); + } + } + + #endregion + #region =================================================================== Currency APIs =================================================================== public async Task> GetCurrencyAsync(Employee loggedInEmployee, Guid tenantId) diff --git a/Marco.Pms.Services/Service/ServiceInterfaces/IMasterService.cs b/Marco.Pms.Services/Service/ServiceInterfaces/IMasterService.cs index d5eb10c..93b71ec 100644 --- a/Marco.Pms.Services/Service/ServiceInterfaces/IMasterService.cs +++ b/Marco.Pms.Services/Service/ServiceInterfaces/IMasterService.cs @@ -22,6 +22,10 @@ namespace Marco.Pms.Services.Service.ServiceInterfaces Task> GetRecurringPaymentStatusAsync(Employee loggedInEmployee, Guid tenantId); #endregion + #region =================================================================== Purchase Invoice Status APIs =================================================================== + Task> GetPurchaseInvoiceStatusAsync(Employee loggedInEmployee, CancellationToken cancellationToken); + + #endregion #region =================================================================== Currency APIs =================================================================== Task> GetCurrencyAsync(Employee loggedInEmployee, Guid tenantId);