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.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 00dc520..d58b105 100644 --- a/Marco.Pms.Services/Helpers/DirectoryHelper.cs +++ b/Marco.Pms.Services/Helpers/DirectoryHelper.cs @@ -1552,7 +1552,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(); @@ -1966,6 +1965,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 -------------------------------- @@ -2034,7 +2148,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);