using Marco.Pms.Model.Dtos.Directory; using OfficeOpenXml; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Mail; using System.Text; using System.Threading.Tasks; namespace Marco.Pms.UtilityApplication { public class DirectoryHelper { public async Task<(List, List) > GenerateCreateContactDto(List contactsData, List categoryMasters, List tagsMaster, List projectMaster) { List lstCreateContactDto = new List(); List failedContacts = new List(); CreateContactDto dto = null; foreach (Contacts contact in contactsData) { if (contact.Name != "") { dto = new CreateContactDto(); dto.Name = contact.Name; dto.Organization = contact.Organization; dto.Address = contact.Address; dto.ContactCategoryId = GetContactCategoryID(contact, categoryMasters); dto.Description = contact.MoreInformation; dto.ContactPhones = GetContactPhones(contact); dto.ContactEmails = GetContactEmails(contact); dto.ProjectIds = GetContactProjectIDs(contact, projectMaster); dto.BucketIds = GetContactBucketIDs(contact); dto.Tags = GetContactTags(contact, tagsMaster); lstCreateContactDto.Add(dto); } else { // export contact to log failedContacts.Add(contact); } } return (lstCreateContactDto, failedContacts); } private List? GetContactTags(Contacts contact, List tagsMaster) { List result = new List(); ContactTagDto dto = null; foreach (string tag in contact.Tags.Split(",")) { dto = new ContactTagDto(); dto.Name = tag; var tagObject = tagsMaster.SingleOrDefault(c => c.Tag == tag.Trim()); dto.Id = (tagObject != null && tagObject.TagID != "" ? Guid.Parse(tagObject.TagID) : null); result.Add(dto); } return result; } private Guid? GetContactCategoryID(Contacts contact, List categoryMaster) { if (contact.Category != "") { var category = categoryMaster.SingleOrDefault(c => c.Categories == contact.Category.Trim()); if (category != null && category.CategoryID != "") { return new Guid(category.CategoryID); } } return null; } private List? GetContactBucketIDs(Contacts contact) { List? result = null; if (contact.BucketId != "") { result = new List(); result.Add(new Guid(contact.BucketId)); } return result; } private List? GetContactEmails(Contacts contact) { List result = new List(); if (contact.Email1 != "" && IsValidEmailMailAddress(contact.Email1)) { CreateContactEmailDto dto = new CreateContactEmailDto(); dto.Label = "Work"; dto.EmailAddress = contact.Email1; result.Add(dto); } if (contact.Email2 != "" && IsValidEmailMailAddress(contact.Email2)) { CreateContactEmailDto dto = new CreateContactEmailDto(); dto.Label = "Work"; dto.EmailAddress = contact.Email2; result.Add(dto); } return result.Count > 0 ? result : null; } private List? GetContactProjectIDs(Contacts contact, List projectMasters) { List? result = null; if (contact.Project != "") { var category = projectMasters.SingleOrDefault(c => c.ProjectName == contact.Project.Trim()); if (category != null && category.ProjectID != "") { if (result == null) result = new List(); result.Add(new Guid(category.ProjectID)); } } return result; } private List? GetContactPhones(Contacts contact) { List result = new List(); if (contact.Mobile1 != "" && IsValidPhoneNumberBasic(contact.Mobile1)) { CreateContactPhoneDto dto = new CreateContactPhoneDto(); dto.Label = "Office"; dto.PhoneNumber = contact.Mobile1; result.Add(dto); } if (contact.Mobile2 != "" && IsValidPhoneNumberBasic(contact.Mobile2)) { CreateContactPhoneDto dto = new CreateContactPhoneDto(); dto.Label = "Office"; dto.PhoneNumber = contact.Mobile2; result.Add(dto); } if (contact.Mobile3 != "" && IsValidPhoneNumberBasic(contact.Mobile3)) { CreateContactPhoneDto dto = new CreateContactPhoneDto(); dto.Label = "Office"; dto.PhoneNumber = contact.Mobile3; result.Add(dto); } return result.Count > 0 ? result : null; } public static bool IsValidPhoneNumberBasic(string phoneNumber) { if (string.IsNullOrWhiteSpace(phoneNumber)) { return false; } // Remove common non-digit characters for a cleaner check string cleanedNumber = new string(phoneNumber.Where(char.IsDigit).ToArray()); // Basic check: Assume a typical phone number length (e.g., 7 to 15 digits) // This is highly region-specific. For India, mobile numbers are usually 10 digits. // For US, 10 digits for local, 11 with country code. return cleanedNumber.Length >= 7 && cleanedNumber.Length <= 15; } public static bool IsValidEmailMailAddress(string email) { if (string.IsNullOrWhiteSpace(email)) { return false; } try { // Attempt to create a MailAddress object. // If the format is invalid, it will throw a FormatException. MailAddress m = new MailAddress(email); // Optional: You can add more specific checks here if needed, // e.g., checking for specific domain names, but MailAddress itself is very good. return true; } catch (FormatException) { // The email format is invalid. return false; } catch (Exception ex) { // Catch any other unexpected exceptions. Console.WriteLine($"An unexpected error occurred during email validation: {ex.Message}"); return false; } } public async Task<(List contactsData, List categoryMasters, List tagsMaster, List projectMaster)> ReadExcelData(string filePath) { if (!File.Exists(filePath)) { throw new FileNotFoundException($"Excel file not found at: {filePath}"); } // Set the license context for EPPlus // If you are using EPPlus in a commercial application, you need to acquire a commercial license. // For non-commercial use or development, use the NonCommercial license context. ExcelPackage.License.SetNonCommercialPersonal("Vikas"); List contactsData = new List(); List categoryMasters = new List(); List tagsMaster = new List(); List projectMaster = new List(); using (var package = new ExcelPackage(new FileInfo(filePath))) { // --- Read Transactions Sheet --- ExcelWorksheet transactionsSheet = package.Workbook.Worksheets["CategoryMaster"]; // Assuming sheet name "Transactions" if (transactionsSheet != null) { int rowCount = transactionsSheet.Dimension.Rows; for (int row = 2; row <= rowCount; row++) // Assuming header in row 1 { try { categoryMasters.Add(new CategoryMaster { CategoryID = transactionsSheet.Cells[row, 1].Text.Trim(), Categories = transactionsSheet.Cells[row, 2].Text.Trim(), Description = transactionsSheet.Cells[row, 3].Text.Trim(), }); } catch (FormatException ex) { Console.WriteLine($"Error parsing transaction data on row {row}: {ex.Message}"); // Optionally, skip row or log error more verbosely } } } else { Console.WriteLine("Transactions sheet not found in the Excel file."); } ExcelWorksheet tagsMasterSheet = package.Workbook.Worksheets["TagMaster"]; // Assuming sheet name "Transactions" if (tagsMasterSheet != null) { int rowCount = tagsMasterSheet.Dimension.Rows; for (int row = 2; row <= rowCount; row++) // Assuming header in row 1 { try { tagsMaster.Add(new TagsMaster { TagID = tagsMasterSheet.Cells[row, 1].Text.Trim(), Tag = tagsMasterSheet.Cells[row, 2].Text.Trim(), }); } catch (FormatException ex) { Console.WriteLine($"Error parsing tags data on row {row}: {ex.Message}"); // Optionally, skip row or log error more verbosely } } } else { Console.WriteLine("tags sheet not found in the Excel file."); } ExcelWorksheet projectsMasterSheet = package.Workbook.Worksheets["ProjectMaster"]; // Assuming sheet name "Transactions" if (projectsMasterSheet != null) { int rowCount = projectsMasterSheet.Dimension.Rows; for (int row = 2; row <= rowCount; row++) // Assuming header in row 1 { try { projectMaster.Add(new ProjectMaster { ProjectID = projectsMasterSheet.Cells[row, 1].Text.Trim(), ProjectName = projectsMasterSheet.Cells[row, 2].Text.Trim(), }); } catch (FormatException ex) { Console.WriteLine($"Error parsing projects data on row {row}: {ex.Message}"); // Optionally, skip row or log error more verbosely } } } else { Console.WriteLine("projects sheet not found in the Excel file."); } // --- Read Master Data Sheet --- ExcelWorksheet masterSheet = package.Workbook.Worksheets["Contacts"]; // Assuming sheet name "MasterData" if (masterSheet != null) { int rowCount = masterSheet.Dimension.Rows; for (int row = 3; row <= rowCount; row++) // Assuming header in row 1 { try { contactsData.Add(new Contacts { BucketId = masterSheet.Cells[row, 1].Text.Trim(), Owner = masterSheet.Cells[row, 2].Text.Trim(), Name = masterSheet.Cells[row, 3].Text.Trim(), Designation = masterSheet.Cells[row, 4].Text.Trim(), Organization = masterSheet.Cells[row, 5].Text.Trim(), Mobile1 = masterSheet.Cells[row, 6].Text.Trim(), Mobile2 = masterSheet.Cells[row, 7].Text.Trim(), Mobile3 = masterSheet.Cells[row, 8].Text.Trim(), Email1 = masterSheet.Cells[row, 9].Text.Trim(), Email2 = masterSheet.Cells[row, 10].Text.Trim(), Location = masterSheet.Cells[row, 11].Text.Trim(), Address = masterSheet.Cells[row, 12].Text.Trim(), MoreInformation = masterSheet.Cells[row, 13].Text.Trim(), Category = masterSheet.Cells[row, 14].Text.Trim(), Tags = masterSheet.Cells[row, 15].Text.Trim(), Project = masterSheet.Cells[row, 16].Text.Trim() }); } catch (FormatException ex) { Console.WriteLine($"Error parsing master data on row {row}: {ex.Message}"); // Optionally, skip row or log error more verbosely } } } else { Console.WriteLine("MasterData sheet not found in the Excel file."); } } return (contactsData, categoryMasters, tagsMaster, projectMaster); } } }