private async Task <ZohoContact> CreateOrUpdateVendor(Order order) { var supplier = await _oc.Suppliers.GetAsync <HSSupplier>(order.ToCompanyID); var addresses = await _oc.SupplierAddresses.ListAsync <HSAddressSupplier>(order.ToCompanyID); var users = await _oc.SupplierUsers.ListAsync(order.ToCompanyID); var currencies = await _zoho.Currencies.ListAsync(); var vendor = await _zoho.Contacts.ListAsync(new ZohoFilter() { Key = "contact_name", Value = supplier.Name }); if (vendor.Items.Any()) { return(await _zoho.Contacts.SaveAsync <ZohoContact>( ZohoContactMapper.Map( vendor.Items.FirstOrDefault(), supplier, addresses.Items.FirstOrDefault(), users.Items.FirstOrDefault(), currencies.Items.FirstOrDefault(c => c.currency_code == "USD")))); } else { return(await _zoho.Contacts.CreateAsync <ZohoContact>( ZohoContactMapper.Map( supplier, addresses.Items.FirstOrDefault(), users.Items.FirstOrDefault(), currencies.Items.FirstOrDefault(c => c.currency_code == "USD")))); } }
private async Task <ZohoContact> CreateOrUpdateContact(Order order) { var ocBuyer = await _oc.Buyers.GetAsync <HSBuyer>(order.FromCompanyID); var buyerAddress = await _oc.Addresses.GetAsync <HSAddressBuyer>(order.FromCompanyID, order.BillingAddressID); var buyerUserGroup = await _oc.UserGroups.GetAsync <HSLocationUserGroup>(order.FromCompanyID, order.BillingAddressID); var ocUsers = await _oc.Users.ListAsync <HSUser>(ocBuyer.ID, buyerUserGroup.ID); var location = new HSBuyerLocation { Address = buyerAddress, UserGroup = buyerUserGroup }; // TODO: MODEL update ~ eventually add a filter to get the primary contact user var currencies = await _zoho.Currencies.ListAsync(); var zContactList = await _zoho.Contacts.ListAsync( new ZohoFilter() { Key = "contact_name", Value = $"{location.Address?.AddressName} - {location.Address?.xp.LocationID}" }, new ZohoFilter() { Key = "company_name", Value = $"{ocBuyer.Name} - {location.Address?.xp.LocationID}" }); var zContact = await _zoho.Contacts.GetAsync(zContactList.Items.FirstOrDefault()?.contact_id); if (zContact.Item != null) { var map = ZohoContactMapper.Map( zContact.Item, ocBuyer, ocUsers.Items, currencies.Items.FirstOrDefault(c => c.currency_code == (location.UserGroup.xp.Currency != null ? location.UserGroup.xp.Currency.ToString() : "USD")), location); return(await _zoho.Contacts.SaveAsync <ZohoContact>(map)); } var contact = ZohoContactMapper.Map( ocBuyer, ocUsers.Items, currencies.Items.FirstOrDefault(c => c.currency_code == (location.UserGroup.xp.Currency != null ? location.UserGroup.xp.Currency.ToString() : "USD")), location); return(await _zoho.Contacts.CreateAsync <ZohoContact>(contact)); }