public async Task <ActionResult <LoanRespObj> > AddLoanBooking([FromBody] LoanReviewOperationObj model) { try { using (var _trans = _context.Database.BeginTransaction()) { var identity = await _identityServer.UserDataAsync(); var user = identity.UserName; model.CreatedBy = user; model.UpdatedBy = user; var appicationResponse = _repo.AddOperationReview(model); if (appicationResponse.loanPayment != null && appicationResponse.AnyIdentifier > 0) { await _schedule.AddLoanSchedule(appicationResponse.AnyIdentifier, appicationResponse.loanPayment); } await _context.SaveChangesAsync(); _trans.Commit(); return(new LoanRespObj { Status = new APIResponseStatus { IsSuccessful = true, Message = new APIResponseMessage { FriendlyMessage = "Successful" } } }); } } catch (Exception ex) { var errorCode = ErrorID.Generate(5); _logger.Error($"ErrorID : {errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}"); return(new LoanRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Error Occurred", TechnicalMessage = ex?.Message, MessageId = errorCode } } }); } }
public async Task <ActionResult <ApprovalRegRespObj> > GoForApproval([FromBody] ApprovalObj entity) { using (var _trans = _dataContext.Database.BeginTransaction()) { try { var currentUserId = _httpContextAccessor.HttpContext.User?.FindFirst(x => x.Type == "userId").Value; var user = await _serverRequest.UserDataAsync(); var loan = _dataContext.credit_loan.Find(entity.TargetId); var req = new IndentityServerApprovalCommand { ApprovalComment = entity.Comment, ApprovalStatus = entity.ApprovalStatusId, TargetId = entity.TargetId, WorkflowToken = loan.WorkflowToken, ReferredStaffId = entity.ReferredStaffId }; var previousDetails = _dataContext.cor_approvaldetail.Where(x => x.WorkflowToken.Contains(loan.WorkflowToken) && x.TargetId == entity.TargetId).ToList(); var lastDate = loan.CreatedOn; if (previousDetails.Count() > 0) { lastDate = previousDetails.OrderByDescending(x => x.ApprovalDetailId).FirstOrDefault().Date; } if (previousDetails.Count() > 0) { lastDate = previousDetails.OrderByDescending(x => x.ApprovalDetailId).FirstOrDefault().Date; } var details = new cor_approvaldetail { Comment = entity.Comment, Date = DateTime.Now, ArrivalDate = previousDetails.Count() > 0 ? lastDate : loan.CreatedOn, StatusId = entity.ApprovalStatusId, TargetId = entity.TargetId, StaffId = user.StaffId, WorkflowToken = loan.WorkflowToken }; var result = await _serverRequest.StaffApprovalRequestAsync(req); if (!result.IsSuccessStatusCode) { return(new ApprovalRegRespObj { Status = new APIResponseStatus { Message = new APIResponseMessage { FriendlyMessage = result.ReasonPhrase } } }); } var stringData = await result.Content.ReadAsStringAsync(); var response = JsonConvert.DeserializeObject <ApprovalRegRespObj>(stringData); if (!response.Status.IsSuccessful) { return(new ApprovalRegRespObj { Status = response.Status }); } if (response.ResponseId == (int)ApprovalStatus.Processing) { loan.ApprovalStatusId = (int)ApprovalStatus.Processing; await _dataContext.cor_approvaldetail.AddAsync(details); await _dataContext.SaveChangesAsync(); _trans.Commit(); return(new ApprovalRegRespObj { ResponseId = (int)ApprovalStatus.Processing, Status = new APIResponseStatus { IsSuccessful = true, Message = response.Status.Message } }); } if (response.ResponseId == (int)ApprovalStatus.Revert) { loan.ApprovalStatusId = (int)ApprovalStatus.Revert; await _dataContext.cor_approvaldetail.AddAsync(details); await _dataContext.SaveChangesAsync(); _trans.Commit(); return(new ApprovalRegRespObj { ResponseId = (int)ApprovalStatus.Revert, Status = new APIResponseStatus { IsSuccessful = true, Message = response.Status.Message } }); } if (response.ResponseId == (int)ApprovalStatus.Approved) { var applicationResponse = _repo.DisburseLoan(entity.TargetId, user.StaffId, user.UserName, entity.Comment); var customer = _dataContext.credit_loancustomer.Find(loan.CustomerId); await _serverRequest.SendMail(new MailObj { fromAddresses = new List <FromAddress> { }, toAddresses = new List <ToAddress> { new ToAddress { address = customer.Email, name = customer.FirstName } }, subject = "Loan Successfully Approved", content = $"Hi {customer.FirstName}, <br> Your loan application has been finally approved. <br/> Loan Amount : {loan.PrincipalAmount}", sendIt = true, }); //Update CustomerTransaction if (applicationResponse.DisbursementEntry != null && applicationResponse.IntegralFeeEntry != null) { _customerTrans.CustomerTransaction(applicationResponse.DisbursementEntry); _customerTrans.CustomerTransaction(applicationResponse.IntegralFeeEntry); } //Generate Schedule if (applicationResponse.loanPayment != null && applicationResponse.AnyIdentifier > 0) { await _schedule.AddLoanSchedule(applicationResponse.AnyIdentifier, applicationResponse.loanPayment); } //Update IFRS _ifrs.UpdateScoreCardHistoryByLoanDisbursement(entity.TargetId, user.UserId); //Pay with Flutterwave if (applicationResponse.FlutterObj != null) { var flutter = _serverRequest.GetFlutterWaveKeys().Result; if (flutter.keys.useFlutterWave) { var res = _flutter.createBulkTransfer(applicationResponse.FlutterObj).Result; loan.ApprovalStatusId = (int)ApprovalStatus.Approved; await _dataContext.cor_approvaldetail.AddAsync(details); await _dataContext.SaveChangesAsync(); _trans.Commit(); if (res.status.ToLower().Trim() != "success") { return(new ApprovalRegRespObj { ResponseId = (int)ApprovalStatus.Revert, Status = new APIResponseStatus { IsSuccessful = true, Message = new APIResponseMessage { FriendlyMessage = "Loan disbursed successfully but transfer creation failed" } } }); } else if (res.status.ToLower().Trim() == "success") { return(new ApprovalRegRespObj { ResponseId = (int)ApprovalStatus.Revert, Status = new APIResponseStatus { IsSuccessful = true, Message = new APIResponseMessage { FriendlyMessage = "Loan disbursed successfully and transfer creation successful" } } }); } } } loan.ApprovalStatusId = (int)ApprovalStatus.Approved; await _dataContext.cor_approvaldetail.AddAsync(details); await _dataContext.SaveChangesAsync(); _trans.Commit(); return(new ApprovalRegRespObj { ResponseId = (int)ApprovalStatus.Approved, Status = new APIResponseStatus { IsSuccessful = true, Message = response.Status.Message } }); } if (response.ResponseId == (int)ApprovalStatus.Disapproved) { loan.ApprovalStatusId = (int)ApprovalStatus.Disapproved; await _dataContext.cor_approvaldetail.AddAsync(details); await _dataContext.SaveChangesAsync(); _trans.Commit(); return(new ApprovalRegRespObj { ResponseId = (int)ApprovalStatus.Disapproved, Status = new APIResponseStatus { IsSuccessful = true, Message = response.Status.Message } }); } return(new ApprovalRegRespObj { Status = response.Status }); } catch (Exception ex) { _trans.Rollback(); var errorCode = ErrorID.Generate(5); _logger.Error($"ErrorID : {errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}"); return(new ApprovalRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = "Error Occurred", TechnicalMessage = ex?.Message, MessageId = errorCode } } }); } finally { _trans.Dispose(); } } }