Added the new API to markAsCompletedthe invoice
This commit is contained in:
parent
2b19890b53
commit
c92e71b292
@ -307,7 +307,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
response.CreatedBy = _mapper.Map<BasicEmployeeVM>(loggedInEmployee);
|
||||
response.BalanceAmount = response.BasicAmount + response.TaxAmount;
|
||||
|
||||
return Ok(ApiResponse<object>.SuccessResponse(response, "Invoice Created Successfully", 201));
|
||||
return StatusCode(201, ApiResponse<object>.SuccessResponse(response, "Invoice Created Successfully", 201));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -387,7 +387,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
_logger.LogInfo("Successfully created received payment {PaymentId} for invoice {InvoiceId}",
|
||||
receivedInvoicePayment.Id, model.InvoiceId);
|
||||
|
||||
return Ok(ApiResponse<object>.SuccessResponse(response, "Payment invoice received successfully", 201));
|
||||
return StatusCode(201, ApiResponse<object>.SuccessResponse(response, "Payment invoice received successfully", 201));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -398,5 +398,61 @@ namespace Marco.Pms.Services.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Marks the specified invoice as completed if it exists and is not already completed.
|
||||
/// </summary>
|
||||
/// <param name="invoiceId">The unique identifier of the invoice to mark as completed.</param>
|
||||
/// <returns>An action result indicating success or the nature of the error.</returns>
|
||||
|
||||
[HttpPut("invoice/marked/completed/{invoiceId}")]
|
||||
public async Task<IActionResult> MarkAsCompletedAsync(Guid invoiceId)
|
||||
{
|
||||
// Create a new async database context for the current request's scope.
|
||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
||||
|
||||
// Retrieve the current logged in employee for audit/logging (optional use).
|
||||
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||
|
||||
// Attempt to find the invoice with tenant isolation; use AsNoTracking if no updates needed (but here we update so tracking is okay).
|
||||
var invoice = await _context.Invoices
|
||||
.FirstOrDefaultAsync(i => i.Id == invoiceId && i.TenantId == tenantId);
|
||||
|
||||
// Log and return 404 if the invoice does not exist.
|
||||
if (invoice == null)
|
||||
{
|
||||
_logger.LogWarning("Invoice not found for ID {InvoiceId} and TenantId {TenantId}", invoiceId, tenantId);
|
||||
return NotFound(ApiResponse<object>.ErrorResponse("Invoice not found", "The specified invoice does not exist", 404));
|
||||
}
|
||||
|
||||
// If the invoice is already marked as completed, log and return meaningful error.
|
||||
if (invoice.MarkAsCompleted)
|
||||
{
|
||||
_logger.LogWarning("Attempt to mark already completed invoice {InvoiceId} as completed by user {UserId}", invoiceId, loggedInEmployee.Id);
|
||||
return BadRequest(ApiResponse<object>.ErrorResponse(
|
||||
"Invoice already completed",
|
||||
"Invoice is already marked as completed", 400));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Mark invoice as completed.
|
||||
invoice.MarkAsCompleted = true;
|
||||
|
||||
// Persist the change to the database.
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
_logger.LogInfo("Invoice {InvoiceId} marked as completed by user {UserId}", invoiceId, loggedInEmployee.Id);
|
||||
|
||||
return Ok(ApiResponse<object>.SuccessResponse(new { }, "Invoice is marked as completed successfully", 200));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error occurred while marking invoice {InvoiceId} as completed by user {UserId}", invoiceId, loggedInEmployee.Id);
|
||||
return StatusCode(500, ApiResponse<object>.ErrorResponse(
|
||||
"Internal server error",
|
||||
"An unexpected error occurred while processing the request", 500));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user