public AmoCrmImportResult Import(AmoCrmImportOptions options) { if (options.SimulateImport) { amoDataProvider = new SimulationImportDataProvider(attendanceUow); } amoDataProvider.SaveDataDuringImport = options.SaveImportData; if (!amoDataProvider.Authenticate()) { return(new AmoCrmImportResult(new [] { new AmoCrmImportResultError { EntityType = AmoCrmEntityTypes.None, Message = "Could not authenticate in amoCRM API" } })); } if (options.StartFromScratch) { ClearExistingAttendanceData(); } AmoCrmImportContext context; try { context = new AmoCrmImportContext(amoDataProvider, entityConverter, !options.IncludeHistoricalData); } catch (ImportSimulationException e) { return(new AmoCrmImportResult(new[] { new AmoCrmImportResultError { EntityType = AmoCrmEntityTypes.None, Message = e.Message } })); } ImportUsers(context); ImportLevels(context); ImportLeadStatuses(context); SetupContactLeadLinks(context); ImportContacts(context); ImportLeads(context); attendanceUow.Commit(); return(new AmoCrmImportResult()); }
private void ImportUsers(AmoCrmImportContext context) { foreach (var user in context.UsersMap.Values) { var existingUser = attendanceUow.Users.GetByAmoId(user.AmoId); if (existingUser == null) { attendanceUow.Users.Add(user); } else { existingUser.CopyValuesFrom(user); attendanceUow.Users.Update(existingUser); } } }
private void ImportLeadStatuses(AmoCrmImportContext context) { foreach (var status in context.LeadStatusMap.Values) { var existingStatus = attendanceUow.LeadStatuses.GetByAmoId(status.AmoId); if (existingStatus == null) { attendanceUow.LeadStatuses.Add(status); } else { existingStatus.CopyValuesFrom(status); attendanceUow.LeadStatuses.Update(existingStatus); } } }
private void ImportLevels(AmoCrmImportContext context) { foreach (var level in context.LevelsMap.Values) { var existingLevel = attendanceUow.Levels.GetByAmoId(level.AmoId); if (existingLevel == null) { attendanceUow.Levels.Add(level); } else { existingLevel.CopyValuesFrom(level); attendanceUow.Levels.Update(existingLevel); } } }
private static void SetupContactLeadLinks(AmoCrmImportContext context) { foreach (var lead in context.LeadsMap.Values) { HashSet <int> leadContactAmoIds; if (!context.LeadContactsMap.TryGetValue(lead.AmoId, out leadContactAmoIds)) { continue; } foreach (var contactAmoId in leadContactAmoIds) { Contact contact; if (!context.ContactsMap.TryGetValue(contactAmoId, out contact)) { continue; } contact.Leads.Add(lead); lead.Contacts.Add(contact); } } }
private void ImportContacts(AmoCrmImportContext context) { foreach (var contact in context.ContactsMap.Values) { // set responsible user User user; if (context.UsersMap.TryGetValue(contact.AmoResponsibleUserId, out user)) { contact.ResponsibleUser = user; } var existingContact = attendanceUow.Contacts.GetByAmoId(contact.AmoId); if (existingContact == null) { attendanceUow.Contacts.Add(contact); } else { existingContact.CopyValuesFrom(contact); attendanceUow.Contacts.Update(existingContact); } } }
private void ImportLeads(AmoCrmImportContext context) { foreach (var lead in context.LeadsMap.Values) { // set responsible user User user; if (context.UsersMap.TryGetValue(lead.AmoResponsibleUserId, out user)) { lead.ResponsibleUser = user; } // set group level if set and exists if (lead.AmoLevelId.HasValue) { var level = attendanceUow.Levels.GetByAmoId(lead.AmoLevelId.Value); // if level is not found in database, this is probably the first import, so get level from import context // (already parsed but not committed to database yet) if (level == null) { context.LevelsMap.TryGetValue(lead.AmoLevelId.Value, out level); } lead.LanguageLevel = level; // set total duration of the group (lead) lead.TotalHours = GetTotalHoursByLevel(lead.LanguageLevel); } // set status var status = attendanceUow.LeadStatuses.GetByAmoId(lead.AmoStatusId); // if status is not found in database, this is probably the first import, so get status from import context // (already parsed but not committed to database yet) if (status == null) { context.LeadStatusMap.TryGetValue(lead.AmoStatusId, out status); } lead.Status = status; var existingLead = attendanceUow.Leads.GetByAmoId(lead.AmoId); if (existingLead == null) { // generate lessons according to schedule and add them to datacontext foreach (var lesson in CreateLessonsForLead(lead)) { // generate default attendance records foreach (var contact in lead.Contacts) { var attendance = new Attendance { Attended = false, Contact = contact, Lesson = lesson }; lesson.Attendances.Add(attendance); attendanceUow.Attendances.Add(attendance); } attendanceUow.Lessons.Add(lesson); } attendanceUow.Leads.Add(lead); } else { existingLead.CopyValuesFrom(lead); attendanceUow.Leads.Update(existingLead); } } }