public async Task <IEnumerable <Jurisdiction> > GetJurisdictionsAsync(string[] types, string stateProvinceCode, string countryCode) { var country = (await cache.GetCountriesAsync()).SingleOrDefault(c => c.era_countrycode == countryCode); if (country == null) { return(Array.Empty <Jurisdiction>()); } var stateProvince = (await cache.GetStateProvincesAsync()).SingleOrDefault(sp => sp._era_relatedcountry_value == country.era_countryid && sp.era_code == stateProvinceCode); if (stateProvince == null) { return(Array.Empty <Jurisdiction>()); } return((await cache.GetJurisdictionsAsync()) .Where(j => j._era_relatedprovincestate_value == stateProvince.era_provinceterritoriesid && (!types.Any() || types.Any(t => t.Equals(j.era_type)))) .Select(j => new Jurisdiction { Code = j.era_jurisdictionid, Name = j.era_jurisdictionname, Type = j.era_type, StateProvinceCode = stateProvinceCode, CountryCode = countryCode })); }
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?.Country; 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); }