Example #1
0
        public async Task <IActionResult> ApproveInvoice([FromBody] ApproveInvoiceInput approveInvoiceInput)
        {
            var apiResponse = new ApiResponse();

            if (!this.ModelState.IsValid)
            {
                apiResponse.IsSuccessful = false;

                return(this.BadRequest(apiResponse));
            }

            try
            {
                approveInvoiceInput.Authority = HttpContext.User.Claims.FirstOrDefault(x => x.Type == "fullname")?.Value;

                apiResponse = await this.invoiceService.ApproveInvoice(approveInvoiceInput);

                if (apiResponse.IsSuccessful)
                {
                    return(this.Ok(apiResponse));
                }

                return(this.BadRequest(apiResponse));
            }
            catch (Exception exception)
            {
                return(this.StatusCode(
                           500,
                           new ApiResponse
                {
                    IsSuccessful = false,
                    Message = exception.InnerException.Message
                }));
            }
        }
Example #2
0
        /// <inheritdoc />
        /// <summary>
        /// The approve invoice.
        /// </summary>
        /// <param name="approveInvoiceInput">
        /// The approve invoice input.
        /// </param>
        /// <returns>
        /// The <see cref="T:System.Threading.Tasks.Task" />.
        /// </returns>
        public async Task <ApiResponse> ApproveInvoice(ApproveInvoiceInput approveInvoiceInput)
        {
            var invoice = await this.invoiceRepository.FindById(approveInvoiceInput.InvoiceId);

            if (invoice == null)
            {
                return(new ApiResponse
                {
                    IsSuccessful = false,
                    Message = "Invoice not found"
                });
            }

            var authority = invoice.Authorities.FirstOrDefault(
                x => x.Name.Equals(approveInvoiceInput.Authority, StringComparison.OrdinalIgnoreCase));

            if (authority == null)
            {
                return(new ApiResponse
                {
                    IsSuccessful = false,
                    Message = "Not authorised to approve"
                });
            }

            authority.Approved   = approveInvoiceInput.Approve;
            authority.ApprovedOn = DateTime.Now;

            this.authorityRepository.Update(authority);
            await this.authorityRepository.SaveChanges();

            return(new ApiResponse
            {
                IsSuccessful = true,
                Message = "Invoice approved"
            });
        }