private static void PopulateClientCFs(Contact contact, int amo_acc, Client1C client1C) { if (contact.custom_fields_values is not null) { foreach (var p in client1C.GetType().GetProperties()) { if (FieldLists.Contacts[amo_acc].ContainsKey(p.Name) && contact.custom_fields_values.Any(x => x.field_id == FieldLists.Contacts[amo_acc][p.Name])) { var value = contact.custom_fields_values.First(x => x.field_id == FieldLists.Contacts[amo_acc][p.Name]).values[0].value; if (p.PropertyType == typeof(Guid?) && Guid.TryParse((string)value, out Guid guidValue)) { p.SetValue(client1C, guidValue); continue; } if (p.PropertyType == typeof(DateTime?) || p.PropertyType == typeof(DateTime)) { if ((long)value < -2208996153) { value = -2208996153; } var dt = DateTimeOffset.FromUnixTimeSeconds((long)value).UtcDateTime.AddDays(1); p.SetValue(client1C, new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0)); continue; } p.SetValue(client1C, value); } } } }
private static void PopulateCFs(Client1C client1C, int acc_id, Contact contact) { foreach (var p in client1C.GetType().GetProperties()) { if (FieldLists.Contacts[acc_id].ContainsKey(p.Name) && p.GetValue(client1C) is not null) { var value = p.GetValue(client1C); if (p.Name == "dob" || p.Name == "pass_issued_at") { DateTime dt = (DateTime)p.GetValue(client1C); value = ((DateTimeOffset)dt.AddHours(3)).ToUnixTimeSeconds(); } //if (p.Name == "phone" || p.Name == "email") continue; try { if ((string)value == "") { continue; } } catch { } if (contact.custom_fields_values is null) { contact.custom_fields_values = new(); } contact.AddNewCF(FieldLists.Contacts[acc_id][p.Name], value); } } }
public CreateOrUpdateAmoContact(Client1C client1C, Amo amo, Log log, RecentlyUpdatedEntityFilter filter) { _amo = amo; _log = log; _client1C = client1C; _filter = filter; }
private static void UpdateAmoId(Contact contact, Client1C client1C) { if (client1C.amo_ids is null) { client1C.amo_ids = new(); } if (!client1C.amo_ids.Any(x => x.account_id == contact.account_id)) { client1C.amo_ids.Add(new() { account_id = (int)contact.account_id, entity_id = (int)contact.id }); return; } if (!client1C.amo_ids.Any(x => x.entity_id == contact.id)) { client1C.amo_ids.First(x => x.account_id == contact.account_id).entity_id = (int)contact.id; return; } }
internal Guid UpdateClient(Client1C client) { if (client.client_id_1C is null || client.client_id_1C == default) { throw new Exception("Unable to update 1C client, no UID."); } string method = "EditStudent"; string content = JsonConvert.SerializeObject(client, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include }); Request1C request = new("POST", method, content, _cred1C); Result result = new(); var response = request.GetResponse(); try { JsonConvert.PopulateObject(WebUtility.UrlDecode(response), result); } catch (Exception e) { throw new Exception($"Unable to process response from 1C: {e.Message}, Response: {response}"); } return(result.client_id_1C); }
internal Guid AddClient(Client1C client) { if (string.IsNullOrEmpty(client.email) && string.IsNullOrEmpty(client.phone)) { throw new Exception("Unable to add client to 1C: no phone or email."); } client.client_id_1C = null; string method = "EditStudent"; string content = JsonConvert.SerializeObject(client, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include });; Request1C request = new("POST", method, content, _cred1C); Result result = new(); var response = request.GetResponse(); try { JsonConvert.PopulateObject(WebUtility.UrlDecode(response), result); } catch (Exception e) { throw new Exception($"Unable to process response from 1C: {e.Message}, Response: {response}"); } return(result.client_id_1C); }
private static void UpdateContactInAmo(Client1C client1C, IAmoRepo <Contact> contRepo, int contact_id, int acc_id, RecentlyUpdatedEntityFilter filter) { Contact contact = new() { id = contact_id, name = client1C.name, custom_fields_values = new(), }; AddUIDToEntity(client1C, acc_id, contact); PopulateCFs(client1C, acc_id, contact); try { filter.AddEntity(contact_id); contRepo.Save(contact); } catch (Exception e) { throw new Exception($"Unable to update contact {contact_id} in amo: {e.Message}"); } }
internal Client1C GetClient(Client1C client) => GetClient((Guid)client.client_id_1C);
private static void AddUIDToEntity(Client1C client1C, int acc_id, Contact contact) { contact.AddNewCF(FieldLists.Contacts[acc_id]["client_id_1C"], client1C.client_id_1C.Value.ToString("D")); }