public async Task <SupplierRegRespObj> Handle(GothroughApprovalCommand request, CancellationToken cancellationToken) { try { var supplierInfor = await _repo.GetSupplierAsync(request.SupplierId); if (supplierInfor == null) { return(new SupplierRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = $"Supplier Not found" } } }); } var enumName = (ApprovalStatus)supplierInfor.ApprovalStatusId; if (supplierInfor.ApprovalStatusId != (int)ApprovalStatus.Pending) { return(new SupplierRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = $"Unable to push supplier with status '{enumName.ToString()}' for approvals" } } }); } var user = await _serverRequest.UserDataAsync(); using (var _transaction = await _dataContext.Database.BeginTransactionAsync()) { try { var targetList = new List <int>(); targetList.Add(supplierInfor.SupplierId); GoForApprovalRequest wfRequest = new GoForApprovalRequest { Comment = "Supplier Registration", OperationId = (int)OperationsEnum.SupplierRegistrationApproval, TargetId = targetList, ApprovalStatus = (int)ApprovalStatus.Processing, DeferredExecution = true, StaffId = user.StaffId, CompanyId = user.CompanyId, EmailNotification = false, ExternalInitialization = false, StatusId = (int)ApprovalStatus.Processing, }; var result = await _serverRequest.GotForApprovalAsync(wfRequest); if (!result.IsSuccessStatusCode) { new SupplierRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage { FriendlyMessage = $"{result.ReasonPhrase} {result.StatusCode}" } } }; } var stringData = await result.Content.ReadAsStringAsync(); GoForApprovalRespObj res = JsonConvert.DeserializeObject <GoForApprovalRespObj>(stringData); if (res.ApprovalProcessStarted) { supplierInfor.ApprovalStatusId = (int)ApprovalStatus.Processing; supplierInfor.WorkflowToken = res.Status.CustomToken; await _repo.UpdateSupplierAsync(supplierInfor); await _transaction.CommitAsync(); return(new SupplierRegRespObj { SupplierId = supplierInfor.SupplierId, Status = new APIResponseStatus { IsSuccessful = res.Status.IsSuccessful, Message = res.Status.Message } }); } if (res.EnableWorkflow || !res.HasWorkflowAccess) { supplierInfor.ApprovalStatusId = (int)ApprovalStatus.Processing; await _repo.UpdateSupplierAsync(supplierInfor); await _transaction.RollbackAsync(); return(new SupplierRegRespObj { Status = new APIResponseStatus { IsSuccessful = res.Status.IsSuccessful, Message = res.Status.Message } }); } if (!res.EnableWorkflow) { supplierInfor.ApprovalStatusId = (int)ApprovalStatus.Approved; supplierInfor.SupplierNumber = SupplierNumber.Generate(15); await _repo.UpdateSupplierAsync(supplierInfor); await _transaction.CommitAsync(); return(new SupplierRegRespObj { Status = new APIResponseStatus { IsSuccessful = true, Message = new APIResponseMessage { FriendlyMessage = "Successful" } } }); } return(new SupplierRegRespObj { Status = new APIResponseStatus { IsSuccessful = res.Status.IsSuccessful, Message = res.Status.Message } }); } catch (Exception ex) { await _transaction.RollbackAsync(); #region Log error to file var errorCode = ErrorID.Generate(4); _logger.Error($"ErrorID : {errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}"); return(new SupplierRegRespObj { Status = new APIResponseStatus { Message = new APIResponseMessage { FriendlyMessage = "Error occured!! Please try again later", MessageId = errorCode, TechnicalMessage = $"ErrorID : {errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}" } } }); #endregion } finally { await _transaction.DisposeAsync(); } } } catch (Exception ex) { #region Log error to file var errorCode = ErrorID.Generate(4); _logger.Error($"ErrorID : {errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}"); return(new SupplierRegRespObj { Status = new APIResponseStatus { Message = new APIResponseMessage { FriendlyMessage = "Error occured!! Please try again later", MessageId = errorCode, TechnicalMessage = $"ErrorID : {errorCode} Ex : {ex?.Message ?? ex?.InnerException?.Message} ErrorStack : {ex?.StackTrace}" } } }); #endregion } }
public async Task <StaffApprovalRegRespObj> Handle(SupplierStaffApprovalCommand request, CancellationToken cancellationToken) { try { var apiResponse = new StaffApprovalRegRespObj { Status = new APIResponseStatus { IsSuccessful = false, Message = new APIResponseMessage() } }; if (request.ApprovalStatus == (int)ApprovalStatus.Revert && request.ReferredStaffId < 1) { apiResponse.Status.Message.FriendlyMessage = "Please select staff to revert to"; return(apiResponse); } var currentUserId = _accessor.HttpContext.User?.FindFirst(x => x.Type == "userId").Value; var user = await _serverRequest.UserDataAsync(); var supplier = await _repo.GetSupplierAsync(request.TargetId); var detail = BuildApprovalDetailObject(request, supplier, user.StaffId); var req = new IndentityServerApprovalCommand { ApprovalComment = request.ApprovalComment, ApprovalStatus = request.ApprovalStatus, TargetId = request.TargetId, WorkflowToken = supplier.WorkflowToken, ReferredStaffId = request.ReferredStaffId }; var result = await _serverRequest.StaffApprovalRequestAsync(req); if (!result.IsSuccessStatusCode) { apiResponse.Status.Message.FriendlyMessage = result.ReasonPhrase; return(apiResponse); } var stringData = await result.Content.ReadAsStringAsync(); response = JsonConvert.DeserializeObject <StaffApprovalRegRespObj>(stringData); if (!response.Status.IsSuccessful) { apiResponse.Status = response.Status; return(apiResponse); } if (response.ResponseId == (int)ApprovalStatus.Processing) { await _detailService.AddUpdateApprovalDetailsAsync(detail); supplier.ApprovalStatusId = (int)ApprovalStatus.Processing; await _repo.UpdateSupplierAsync(supplier); apiResponse.ResponseId = (int)ApprovalStatus.Processing; apiResponse.Status.IsSuccessful = true; apiResponse.Status.Message = response.Status.Message; return(apiResponse); } if (response.ResponseId == (int)ApprovalStatus.Revert) { await _detailService.AddUpdateApprovalDetailsAsync(detail); supplier.ApprovalStatusId = (int)ApprovalStatus.Revert; await _repo.UpdateSupplierAsync(supplier); apiResponse.ResponseId = (int)ApprovalStatus.Revert; apiResponse.Status.IsSuccessful = true; apiResponse.Status.Message = response.Status.Message; return(apiResponse); } if (response.ResponseId == (int)ApprovalStatus.Approved) { await _detailService.AddUpdateApprovalDetailsAsync(detail); supplier.ApprovalStatusId = (int)ApprovalStatus.Approved; supplier.SupplierNumber = SupplierNumber.Generate(10); await _repo.UpdateSupplierAsync(supplier); apiResponse.ResponseId = (int)ApprovalStatus.Approved; apiResponse.Status.IsSuccessful = true; apiResponse.Status.Message = response.Status.Message; return(apiResponse); } if (response.ResponseId == (int)ApprovalStatus.Disapproved) { await _detailService.AddUpdateApprovalDetailsAsync(detail); supplier.ApprovalStatusId = (int)ApprovalStatus.Disapproved; await _repo.UpdateSupplierAsync(supplier); apiResponse.ResponseId = (int)ApprovalStatus.Disapproved; apiResponse.Status.IsSuccessful = true; apiResponse.Status.Message = response.Status.Message; return(apiResponse); } apiResponse.ResponseId = detail.ApprovalDetailId; apiResponse.Status = response.Status; return(apiResponse); } catch (Exception ex) { throw ex; } }