diff --git a/Marco.Pms.Model/Mapper/DirectoryMapper.cs b/Marco.Pms.Model/Mapper/DirectoryMapper.cs index 2807e9a..68ea44e 100644 --- a/Marco.Pms.Model/Mapper/DirectoryMapper.cs +++ b/Marco.Pms.Model/Mapper/DirectoryMapper.cs @@ -51,6 +51,20 @@ namespace Marco.Pms.Model.Mapper Address = contact.Address ?? string.Empty }; } + public static ContactProfileVM ToContactProfileVMFromContact(this Contact contact) + { + return new ContactProfileVM + { + Id = contact.Id, + Name = contact.Name, + ContactCategory = contact.ContactCategory != null ? contact.ContactCategory.ToContactCategoryVMFromContactCategoryMaster() : null, + Description = contact.Description ?? string.Empty, + Organization = contact.Organization ?? string.Empty, + Address = contact.Address ?? string.Empty, + CreatedAt = contact.CreatedAt, + CreatedBy = contact.CreatedBy != null ? contact.CreatedBy.ToBasicEmployeeVMFromEmployee() : null + }; + } //Contact Phone Mapper public static ContactPhone ToContactPhoneFromCreateContactPhoneDto(this CreateContactPhoneDto createContactPhoneDto, Guid tenantId, Guid contactId) @@ -207,7 +221,9 @@ namespace Marco.Pms.Model.Mapper { Id = note.Id, Note = note.Note, - ContactId = note.ContactId + ContactId = note.ContactId, + CreatedAt = note.CreatedAt, + CreatedBy = note.Createdby != null ? note.Createdby.ToBasicEmployeeVMFromEmployee() : null }; } } diff --git a/Marco.Pms.Model/ViewModels/Directory/ContactNoteVM.cs b/Marco.Pms.Model/ViewModels/Directory/ContactNoteVM.cs index 5e1c340..c3ca209 100644 --- a/Marco.Pms.Model/ViewModels/Directory/ContactNoteVM.cs +++ b/Marco.Pms.Model/ViewModels/Directory/ContactNoteVM.cs @@ -1,9 +1,15 @@ -namespace Marco.Pms.Model.ViewModels.Directory +using Marco.Pms.Model.ViewModels.Activities; + +namespace Marco.Pms.Model.ViewModels.Directory { public class ContactNoteVM { public Guid Id { get; set; } public string Note { get; set; } = string.Empty; + public DateTime CreatedAt { get; set; } + public BasicEmployeeVM? CreatedBy { get; set; } + public DateTime? UpdatedAt { get; set; } + public BasicEmployeeVM? UpdatedBy { get; set; } public Guid ContactId { get; set; } } } diff --git a/Marco.Pms.Model/ViewModels/Directory/ContactProfileVM.cs b/Marco.Pms.Model/ViewModels/Directory/ContactProfileVM.cs new file mode 100644 index 0000000..8103c5c --- /dev/null +++ b/Marco.Pms.Model/ViewModels/Directory/ContactProfileVM.cs @@ -0,0 +1,26 @@ +using Marco.Pms.Model.ViewModels.Activities; +using Marco.Pms.Model.ViewModels.Master; +using Marco.Pms.Model.ViewModels.Projects; + +namespace Marco.Pms.Model.ViewModels.Directory +{ + public class ContactProfileVM + { + public Guid Id { get; set; } + public string? Name { get; set; } + public string? Description { get; set; } + public string? Organization { get; set; } + public string? Address { get; set; } + public DateTime CreatedAt { get; set; } + public BasicEmployeeVM? CreatedBy { get; set; } + public DateTime? UpdatedAt { get; set; } + public BasicEmployeeVM? UpdatedBy { get; set; } + public List? ContactPhones { get; set; } + public List? ContactEmails { get; set; } + public ContactCategoryVM? ContactCategory { get; set; } + public List? Projects { get; set; } + public List? Buckets { get; set; } + public List? Tags { get; set; } + public List? Notes { get; set; } + } +} diff --git a/Marco.Pms.Model/ViewModels/Projects/BasicProjectVM.cs b/Marco.Pms.Model/ViewModels/Projects/BasicProjectVM.cs new file mode 100644 index 0000000..e08caa9 --- /dev/null +++ b/Marco.Pms.Model/ViewModels/Projects/BasicProjectVM.cs @@ -0,0 +1,8 @@ +namespace Marco.Pms.Model.ViewModels.Projects +{ + public class BasicProjectVM + { + public Guid Id { get; set; } + public string? Name { get; set; } + } +} diff --git a/Marco.Pms.Services/Controllers/DirectoryController.cs b/Marco.Pms.Services/Controllers/DirectoryController.cs index a901e38..12317d3 100644 --- a/Marco.Pms.Services/Controllers/DirectoryController.cs +++ b/Marco.Pms.Services/Controllers/DirectoryController.cs @@ -99,6 +99,20 @@ namespace Marco.Pms.Services.Controllers } } + [HttpGet("profile/{id}")] + public async Task GetContactProfile(Guid id) + { + var response = await _directoryHelper.GetContactProfile(id); + if (response.StatusCode == 200) + { + return Ok(response); + } + else + { + return BadRequest(response); + } + } + // -------------------------------- Contact Notes -------------------------------- [HttpPost("note")] diff --git a/Marco.Pms.Services/Helpers/DirectoryHelper.cs b/Marco.Pms.Services/Helpers/DirectoryHelper.cs index 04b0785..c2f8345 100644 --- a/Marco.Pms.Services/Helpers/DirectoryHelper.cs +++ b/Marco.Pms.Services/Helpers/DirectoryHelper.cs @@ -2,9 +2,11 @@ using Marco.Pms.Model.Directory; using Marco.Pms.Model.Dtos.Directory; using Marco.Pms.Model.Mapper; +using Marco.Pms.Model.Projects; using Marco.Pms.Model.Utilities; using Marco.Pms.Model.ViewModels.Directory; using Marco.Pms.Model.ViewModels.Master; +using Marco.Pms.Model.ViewModels.Projects; using MarcoBMS.Services.Helpers; using MarcoBMS.Services.Service; using Microsoft.EntityFrameworkCore; @@ -213,7 +215,6 @@ namespace Marco.Pms.Services.Helpers _logger.LogInfo("Employee ID {EmployeeId} sent an empty Bucket id", LoggedInEmployee.Id); return ApiResponse.ErrorResponse("Bucket ID is empty", "Bucket ID is empty", 400); } - public async Task> CreateContact(CreateContactDto createContact) { Guid tenantId = _userHelper.GetTenantId(); @@ -623,6 +624,121 @@ namespace Marco.Pms.Services.Helpers _logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id); return ApiResponse.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400); } + public async Task> GetContactProfile(Guid id) + { + Guid tenantId = _userHelper.GetTenantId(); + var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); + if (id != Guid.Empty) + { + Contact? contact = await _context.Contacts.Include(c => c.ContactCategory).Include(c => c.CreatedBy).FirstOrDefaultAsync(c => c.Id == id && c.IsActive); + if (contact == null) + { + _logger.LogWarning("Employee with ID {LoggedInEmployeeId} tries to update contact with ID {ContactId} is not found in database", LoggedInEmployee.Id); + return ApiResponse.ErrorResponse("Contact not found", "Contact not found", 404); + } + ContactProfileVM contactVM = contact.ToContactProfileVMFromContact(); + DirectoryUpdateLog? updateLog = await _context.DirectoryUpdateLogs.Include(l => l.Employee).Where(l => l.RefereanceId == contact.Id).OrderByDescending(l => l.UpdateAt).FirstOrDefaultAsync(); + if (updateLog != null) + { + contactVM.UpdatedAt = updateLog.UpdateAt; + contactVM.UpdatedBy = updateLog.Employee != null ? updateLog.Employee.ToBasicEmployeeVMFromEmployee() : null; + } + + List? phones = await _context.ContactsPhones.Where(p => p.ContactId == contact.Id).ToListAsync(); + if (phones.Any()) + { + List? phoneVMs = new List(); + foreach (var phone in phones) + { + ContactPhoneVM phoneVM = phone.ToContactPhoneVMFromContactPhone(); + phoneVMs.Add(phoneVM); + } + contactVM.ContactPhones = phoneVMs; + } + + List? emails = await _context.ContactsEmails.Where(e => e.ContactId == contact.Id).ToListAsync(); + if (emails.Any()) + { + List? emailVMs = new List(); + foreach (var email in emails) + { + ContactEmailVM emailVM = email.ToContactEmailVMFromContactEmail(); + emailVMs.Add(emailVM); + } + contactVM.ContactEmails = emailVMs; + } + + List? contactProjects = await _context.ContactProjectMappings.Where(cp => cp.ContactId == contact.Id).ToListAsync(); + if (contactProjects.Any()) + { + List projectIds = contactProjects.Select(cp => cp.ProjectId).ToList(); + List? projects = await _context.Projects.Where(p => projectIds.Contains(p.Id) && p.TenantId == tenantId).ToListAsync(); + List? projectVMs = new List(); + foreach (var project in projects) + { + BasicProjectVM projectVM = new BasicProjectVM + { + Id = project.Id, + Name = project.Name + }; + projectVMs.Add(projectVM); + } + contactVM.Projects = projectVMs; + } + List? contactBuckets = await _context.ContactBucketMappings.Where(cb => cb.ContactId == contact.Id).ToListAsync(); + List? employeeBuckets = await _context.EmployeeBucketMappings.Where(eb => eb.EmployeeId == LoggedInEmployee.Id).ToListAsync(); + if (contactBuckets.Any() && employeeBuckets.Any()) + { + List contactBucketIds = contactBuckets.Select(cb => cb.BucketId).ToList(); + List employeeBucketIds = employeeBuckets.Select(eb => eb.BucketId).ToList(); + List? buckets = await _context.Buckets.Where(b => contactBucketIds.Contains(b.Id) && employeeBucketIds.Contains(b.Id)).ToListAsync(); + List? bucketVMs = new List(); + foreach (var bucket in buckets) + { + BucketVM bucketVM = bucket.ToBucketVMFromBucket(); + bucketVMs.Add(bucketVM); + } + contactVM.Buckets = bucketVMs; + } + List? contactTags = await _context.ContactTagMappings.Where(ct => ct.ContactId == contact.Id).ToListAsync(); + if (contactTags.Any()) + { + List tagIds = contactTags.Select(ct => ct.ContactTagId).ToList(); + List tagMasters = await _context.ContactTagMasters.Where(t => tagIds.Contains(t.Id)).ToListAsync(); + List tagVMs = new List(); + foreach (var tagMaster in tagMasters) + { + ContactTagVM tagVM = tagMaster.ToContactTagVMFromContactTagMaster(); + tagVMs.Add(tagVM); + } + contactVM.Tags = tagVMs; + } + List? notes = await _context.ContactNotes.Where(n => n.ContactId == contact.Id && n.IsActive).ToListAsync(); + if (notes.Any()) + { + List? noteIds = notes.Select(n => n.Id).ToList(); + List? noteUpdateLogs = await _context.DirectoryUpdateLogs.Include(l => l.Employee).Where(l => noteIds.Contains(l.Id)).OrderByDescending(l => l.UpdateAt).ToListAsync(); + List? noteVMs = new List(); + foreach (var note in notes) + { + DirectoryUpdateLog? noteUpdateLog = noteUpdateLogs.Where(n => n.RefereanceId == note.Id).OrderByDescending(l => l.UpdateAt).FirstOrDefault(); + ContactNoteVM noteVM = note.ToContactNoteVMFromContactNote(); + if (noteUpdateLog != null) + { + noteVM.UpdatedAt = noteUpdateLog.UpdateAt; + noteVM.UpdatedBy = noteUpdateLog.Employee != null ? noteUpdateLog.Employee.ToBasicEmployeeVMFromEmployee() : null; + } + noteVMs.Add(noteVM); + } + contactVM.Notes = noteVMs; + } + _logger.LogInfo("Employee ID {EmployeeId} fetched profile of contact {COntactId}", LoggedInEmployee.Id, contact.Id); + return ApiResponse.SuccessResponse(contactVM, "Contact profile fetched successfully"); + + } + _logger.LogInfo("Employee ID {EmployeeId} sent an empty contact id", LoggedInEmployee.Id); + return ApiResponse.ErrorResponse("Contact ID is empty", "Contact ID is empty", 400); + } // -------------------------------- Contact Notes -------------------------------- @@ -691,7 +807,8 @@ namespace Marco.Pms.Services.Helpers await _context.SaveChangesAsync(); ContactNoteVM noteVM = contactNote.ToContactNoteVMFromContactNote(); - + noteVM.UpdatedAt = DateTime.UtcNow; + noteVM.UpdatedBy = LoggedInEmployee.ToBasicEmployeeVMFromEmployee(); _logger.LogInfo("Employee {EmployeeId} updated note {NoteId} at contact {ContactId}", LoggedInEmployee.Id, noteVM.Id, contact.Id); return ApiResponse.SuccessResponse(noteVM, "Note updated successfully", 200);