public async Task <HeldInvoiceFlatResponse> ExecuteAsync(Guid payRunId, Guid invoiceId, HeldInvoiceCreationDomain heldInvoiceCreationDomain) { heldInvoiceCreationDomain.PayRunInvoiceId = invoiceId; var payRun = await _payRunGateway.GetPayRunAsync(payRunId) .EnsureExistsAsync($"Pay run with id {payRunId} not found"); var validPayRunStatuses = new[] { PayrunStatus.WaitingForReview, PayrunStatus.WaitingForApproval }; if (!validPayRunStatuses.Contains(payRun.Status)) { throw new ApiException($"Pay run must be in review or waiting for approval to allow status change", HttpStatusCode.BadRequest); } var payRunInvoice = await _payRunInvoiceGateway.GetPayRunInvoiceAsync(heldInvoiceCreationDomain.PayRunInvoiceId, PayRunInvoiceFields.None, true).EnsureExistsAsync($"Pay run invoice with id {heldInvoiceCreationDomain.PayRunInvoiceId} not found"); // Update pay run invoice status payRunInvoice.InvoiceStatus = InvoiceStatus.Held; // Create held invoice record var heldInvoice = heldInvoiceCreationDomain.ToEntity(); _heldInvoiceGateway.AddHeldInvoice(heldInvoice); await _dbManager.SaveAsync(); return(heldInvoice.ToFlatDomain().ToResponse()); }
public async Task <bool> ExecuteAsync(Guid payRunId, Guid payRunInvoiceId, InvoiceStatus newStatus) { var payRun = await _payRunGateway.GetPayRunAsync(payRunId) .EnsureExistsAsync($"Pay run with id {payRunId} not found"); var validPayRunStatuses = new[] { PayrunStatus.WaitingForReview, PayrunStatus.WaitingForApproval }; if (!validPayRunStatuses.Contains(payRun.Status)) { throw new ApiException($"Pay run must be in review or waiting for approval to allow status change", HttpStatusCode.BadRequest); } var payRunInvoice = await _payRunInvoiceGateway.GetPayRunInvoiceAsync(payRunInvoiceId, PayRunInvoiceFields.None, true) .EnsureExistsAsync($"Pay run invoice with id {payRunInvoiceId} not found"); // If invoice is held, released from a different endpoint. // Status change here is limited based on pay run status but release can happen regardless of pay run status if (payRunInvoice.InvoiceStatus == InvoiceStatus.Held) { throw new ApiException("Status change not allowed. Use release endpoint", HttpStatusCode.BadRequest); } var validStatuses = new[] { InvoiceStatus.Accepted, InvoiceStatus.Rejected, InvoiceStatus.Draft }; if ((validStatuses.Contains(newStatus) && payRunInvoice.InvoiceStatus != InvoiceStatus.Held) || (payRunInvoice.InvoiceStatus == InvoiceStatus.Held && newStatus == InvoiceStatus.Released)) { payRunInvoice.InvoiceStatus = newStatus; await _dbManager.SaveAsync(); return(true); } throw new ApiException($"Status change for pay run invoice with id {payRunInvoiceId} not allowed", HttpStatusCode.BadRequest); }