public async Task <IEnumerable <Jurisdiction> > GetJurisdictionsAsync(JurisdictionType[] types, string stateProvinceCode, string countryCode) { var country = (await listsRepository.GetCountriesAsync()).SingleOrDefault(c => c.era_countrycode == countryCode); if (country == null) { return(Array.Empty <Jurisdiction>()); } var stateProvince = (await listsRepository.GetStateProvincesAsync()).SingleOrDefault(sp => sp._era_relatedcountry_value == country.era_countryid && sp.era_code == stateProvinceCode); if (stateProvince == null) { return(Array.Empty <Jurisdiction>()); } return((await listsRepository.GetJurisdictionsAsync()) .Where(j => j._era_relatedprovincestate_value == stateProvince.era_provinceterritoriesid && (types == null || !types.Any() || types.Any(t => ((int)t).ToString().Equals(j.era_type)))) .Select(j => new Jurisdiction { Code = j.era_jurisdictionid, Name = j.era_jurisdictionname, Type = string.IsNullOrWhiteSpace(j.era_type) ? JurisdictionType.Undefined : Enum.Parse <JurisdictionType>(j.era_type, true), StateProvinceCode = stateProvinceCode, CountryCode = countryCode }).OrderBy(j => j.Name)); }
private async Task <Submission> ResolveEntitiesReferences(Submission submission) { var countries = (await cachedListsProvider.GetCountriesAsync()).ToArray(); var stateProvinces = (await cachedListsProvider.GetStateProvincesAsync()).ToArray(); var jurisdictions = (await cachedListsProvider.GetJurisdictionsAsync()).ToArray(); var supports = (await cachedListsProvider.GetSupportsAsync()).ToArray(); foreach (var supplierInformation in submission.Suppliers) { var cityCode = supplierInformation?.Address?.CityCode; if (cityCode != null) { var jurisdiction = jurisdictions.SingleOrDefault(j => j.era_jurisdictionid.Equals(cityCode, StringComparison.OrdinalIgnoreCase)); if (jurisdiction == null) { throw new ValidationException($"City code '{cityCode}' doesn't exists in Dynamics"); } supplierInformation.Address.CityCode = jurisdiction.era_jurisdictionid; } var stateProvinceCode = supplierInformation?.Address?.StateProvinceCode; if (stateProvinceCode != null) { var stateProvince = stateProvinces.SingleOrDefault(sp => sp.era_code.Equals(stateProvinceCode, StringComparison.OrdinalIgnoreCase)); if (stateProvince == null) { throw new ValidationException($"StateProvinceCode '{stateProvinceCode}' doesn't exists in Dynamics"); } supplierInformation.Address.StateProvinceCode = stateProvince.era_provinceterritoriesid; } var countryCode = supplierInformation?.Address?.CountryCode; if (countryCode != null) { var country = countries.SingleOrDefault(c => c.era_countrycode.Equals(supplierInformation.Address?.CountryCode, StringComparison.OrdinalIgnoreCase)); if (country == null) { throw new ValidationException($"CountryCode '{supplierInformation?.Address?.CountryCode}' doesn't exists in Dynamics"); } supplierInformation.Address.CountryCode = country.era_countryid; } } foreach (var lineItem in submission.LineItems) { var support = supports.SingleOrDefault(s => s.era_name.Equals(lineItem?.SupportProvided, StringComparison.OrdinalIgnoreCase)); if (support == null) { throw new ValidationException($"in line item referral '{lineItem?.ReferralNumber}', SupportProvided '{lineItem?.SupportProvided}' is null or doesn't exists in Dynamics"); } lineItem.SupportProvided = support.era_supportid; } return(submission); }