public AirtimeServiceResponse SwitchAirtimeVendors(IEnumerable <IAirtimeService> airtimeServices) { AirtimeServiceResponse airtimeServiceResponse = new AirtimeServiceResponse(); try { _logger.LogInformation($"Inside switch airtime vendors method"); var _Vendoragents = this.Vendors.AllVendors(); if (_Vendoragents?.Count > 0) { _logger.LogInformation($"Vendor agents is greater than zero"); var ActiveVendor = _Vendoragents.FirstOrDefault(X => X.commissioned == true && X.Active); if (ActiveVendor == null) { ActiveVendor = _Vendoragents[0]; } _logger.LogInformation($"about to call first vendor"); //airtimeServiceResponse = airtimeServices //.FirstOrDefault(x => x.AgentName().ToLower() == "CWG".ToLower()) //.BuyAirtime(this); airtimeServiceResponse = airtimeServices .FirstOrDefault(x => x.AgentName().ToLower() == ActiveVendor.Name.ToLower()) .BuyAirtime(this); //airtimeServiceResponse.ProccedByVendorId = "1"; airtimeServiceResponse.ProccedByVendorId = ActiveVendor.Id.ToString(); if (airtimeServiceResponse.ResponseStatus == TransactionStatus.Failed.ToString()) { int vendorcount = _Vendoragents.Count() - 1; List <Vendors> Othervendors = _Vendoragents.ToList <Vendors>(); Othervendors.Remove(ActiveVendor); var vendorsarray = Othervendors.ToArray <Vendors>(); ActiveVendor.Active = false; ActiveVendor.DateUpdated = DateTime.Now; this.Vendors.UpdateVendorAgent(ActiveVendor); for (int i = 0; i < vendorcount; i++) { Vendors NewActiveVendor = (Vendors)vendorsarray[i]; airtimeServiceResponse = airtimeServices .FirstOrDefault(x => x.AgentName().ToLower() == vendorsarray[i].Name.ToLower() && x.AgentName().ToLower() != ActiveVendor.Name.ToLower()) .BuyAirtime(this); if (airtimeServiceResponse.ResponseStatus == TransactionStatus.Success.ToString()) { NewActiveVendor.Active = true; NewActiveVendor.DateUpdated = DateTime.Now; this.Vendors.UpdateVendorAgent(NewActiveVendor); airtimeServiceResponse.ProccedByVendorId = NewActiveVendor.Id.ToString(); break; } if (airtimeServiceResponse.ResponseStatus == TransactionStatus.Pending.ToString()) { NewActiveVendor.Active = false; NewActiveVendor.DateUpdated = DateTime.Now; if (_Vendoragents?.Count > 1) { var unusedactive = _Vendoragents.FirstOrDefault(x => x.Id != NewActiveVendor.Id); if (unusedactive != null) { unusedactive.Active = true; unusedactive.DateUpdated = DateTime.Now; this.Vendors.UpdateVendorAgent(unusedactive); } } this.Vendors.UpdateVendorAgent(NewActiveVendor); airtimeServiceResponse.ProccedByVendorId = NewActiveVendor.Id.ToString(); break; } if (airtimeServiceResponse.ResponseStatus == TransactionStatus.Failed.ToString()) { airtimeServiceResponse.ProccedByVendorId = NewActiveVendor.Id.ToString(); if (_Vendoragents?.Count > 1 && i == vendorcount - 1) { var unusedactive = _Vendoragents.FirstOrDefault(x => x.Id != NewActiveVendor.Id); if (unusedactive != null) { unusedactive.Active = true; unusedactive.DateUpdated = DateTime.Now; this.Vendors.UpdateVendorAgent(unusedactive); } } } } } if (airtimeServiceResponse.ResponseStatus == TransactionStatus.Pending.ToString()) { ActiveVendor.Active = false; ActiveVendor.DateUpdated = DateTime.Now; if (_Vendoragents?.Count > 1) { var unusedactive = _Vendoragents.FirstOrDefault(x => x.Id != ActiveVendor.Id); if (unusedactive != null) { unusedactive.Active = true; unusedactive.DateUpdated = DateTime.Now; this.Vendors.UpdateVendorAgent(unusedactive); } } this.Vendors.UpdateVendorAgent(ActiveVendor); } return(airtimeServiceResponse); } else { _logger.LogInformation($"No vendor agent found"); airtimeServiceResponse.ResponseMessage = AirtimeValidationMessages.ErrorMessages.TransactionFailed; airtimeServiceResponse.ResponseStatus = TransactionStatus.Failed.ToString(); return(airtimeServiceResponse); } } catch (Exception ex) { _logger.LogInformation($"error when inside switching to vendor --{this?.phoneNumber?.Value}--: {ex?.Message}"); _logger.LogInformation($"error when inside switching to vendor --{this?.phoneNumber?.Value}--: {ex?.InnerException?.Message}"); _logger.LogInformation($"error when inside switching to vendor stack trace --{this?.phoneNumber?.Value}--: {ex?.StackTrace}"); airtimeServiceResponse.ResponseMessage = AirtimeValidationMessages.ErrorMessages.TransactionFailed; airtimeServiceResponse.ResponseStatus = TransactionStatus.Failed.ToString(); return(airtimeServiceResponse); } }
public void UpdateVendorAgent(Vendors vendors) { vendorsManager.UpdateVendorAgents(vendors); }