Created an API to get contact profile by its Id

This commit is contained in:
ashutosh.nehete 2025-05-20 09:46:03 +05:30
parent efc5e22f7b
commit 811e0ac807
6 changed files with 191 additions and 4 deletions

View File

@ -51,6 +51,20 @@ namespace Marco.Pms.Model.Mapper
Address = contact.Address ?? string.Empty 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 //Contact Phone Mapper
public static ContactPhone ToContactPhoneFromCreateContactPhoneDto(this CreateContactPhoneDto createContactPhoneDto, Guid tenantId, Guid contactId) public static ContactPhone ToContactPhoneFromCreateContactPhoneDto(this CreateContactPhoneDto createContactPhoneDto, Guid tenantId, Guid contactId)
@ -207,7 +221,9 @@ namespace Marco.Pms.Model.Mapper
{ {
Id = note.Id, Id = note.Id,
Note = note.Note, Note = note.Note,
ContactId = note.ContactId ContactId = note.ContactId,
CreatedAt = note.CreatedAt,
CreatedBy = note.Createdby != null ? note.Createdby.ToBasicEmployeeVMFromEmployee() : null
}; };
} }
} }

View File

@ -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 class ContactNoteVM
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
public string Note { get; set; } = string.Empty; 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; } public Guid ContactId { get; set; }
} }
} }

View File

@ -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<ContactPhoneVM>? ContactPhones { get; set; }
public List<ContactEmailVM>? ContactEmails { get; set; }
public ContactCategoryVM? ContactCategory { get; set; }
public List<BasicProjectVM>? Projects { get; set; }
public List<BucketVM>? Buckets { get; set; }
public List<ContactTagVM>? Tags { get; set; }
public List<ContactNoteVM>? Notes { get; set; }
}
}

View File

@ -0,0 +1,8 @@
namespace Marco.Pms.Model.ViewModels.Projects
{
public class BasicProjectVM
{
public Guid Id { get; set; }
public string? Name { get; set; }
}
}

View File

@ -99,6 +99,20 @@ namespace Marco.Pms.Services.Controllers
} }
} }
[HttpGet("profile/{id}")]
public async Task<IActionResult> GetContactProfile(Guid id)
{
var response = await _directoryHelper.GetContactProfile(id);
if (response.StatusCode == 200)
{
return Ok(response);
}
else
{
return BadRequest(response);
}
}
// -------------------------------- Contact Notes -------------------------------- // -------------------------------- Contact Notes --------------------------------
[HttpPost("note")] [HttpPost("note")]

View File

@ -2,9 +2,11 @@
using Marco.Pms.Model.Directory; using Marco.Pms.Model.Directory;
using Marco.Pms.Model.Dtos.Directory; using Marco.Pms.Model.Dtos.Directory;
using Marco.Pms.Model.Mapper; using Marco.Pms.Model.Mapper;
using Marco.Pms.Model.Projects;
using Marco.Pms.Model.Utilities; using Marco.Pms.Model.Utilities;
using Marco.Pms.Model.ViewModels.Directory; using Marco.Pms.Model.ViewModels.Directory;
using Marco.Pms.Model.ViewModels.Master; using Marco.Pms.Model.ViewModels.Master;
using Marco.Pms.Model.ViewModels.Projects;
using MarcoBMS.Services.Helpers; using MarcoBMS.Services.Helpers;
using MarcoBMS.Services.Service; using MarcoBMS.Services.Service;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -213,7 +215,6 @@ namespace Marco.Pms.Services.Helpers
_logger.LogInfo("Employee ID {EmployeeId} sent an empty Bucket id", LoggedInEmployee.Id); _logger.LogInfo("Employee ID {EmployeeId} sent an empty Bucket id", LoggedInEmployee.Id);
return ApiResponse<object>.ErrorResponse("Bucket ID is empty", "Bucket ID is empty", 400); return ApiResponse<object>.ErrorResponse("Bucket ID is empty", "Bucket ID is empty", 400);
} }
public async Task<ApiResponse<object>> CreateContact(CreateContactDto createContact) public async Task<ApiResponse<object>> CreateContact(CreateContactDto createContact)
{ {
Guid tenantId = _userHelper.GetTenantId(); Guid tenantId = _userHelper.GetTenantId();
@ -627,6 +628,121 @@ namespace Marco.Pms.Services.Helpers
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id); _logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id);
return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400); return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400);
} }
public async Task<ApiResponse<object>> 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<object>.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<ContactPhone>? phones = await _context.ContactsPhones.Where(p => p.ContactId == contact.Id).ToListAsync();
if (phones.Any())
{
List<ContactPhoneVM>? phoneVMs = new List<ContactPhoneVM>();
foreach (var phone in phones)
{
ContactPhoneVM phoneVM = phone.ToContactPhoneVMFromContactPhone();
phoneVMs.Add(phoneVM);
}
contactVM.ContactPhones = phoneVMs;
}
List<ContactEmail>? emails = await _context.ContactsEmails.Where(e => e.ContactId == contact.Id).ToListAsync();
if (emails.Any())
{
List<ContactEmailVM>? emailVMs = new List<ContactEmailVM>();
foreach (var email in emails)
{
ContactEmailVM emailVM = email.ToContactEmailVMFromContactEmail();
emailVMs.Add(emailVM);
}
contactVM.ContactEmails = emailVMs;
}
List<ContactProjectMapping>? contactProjects = await _context.ContactProjectMappings.Where(cp => cp.ContactId == contact.Id).ToListAsync();
if (contactProjects.Any())
{
List<Guid> projectIds = contactProjects.Select(cp => cp.ProjectId).ToList();
List<Project>? projects = await _context.Projects.Where(p => projectIds.Contains(p.Id) && p.TenantId == tenantId).ToListAsync();
List<BasicProjectVM>? projectVMs = new List<BasicProjectVM>();
foreach (var project in projects)
{
BasicProjectVM projectVM = new BasicProjectVM
{
Id = project.Id,
Name = project.Name
};
projectVMs.Add(projectVM);
}
contactVM.Projects = projectVMs;
}
List<ContactBucketMapping>? contactBuckets = await _context.ContactBucketMappings.Where(cb => cb.ContactId == contact.Id).ToListAsync();
List<EmployeeBucketMapping>? employeeBuckets = await _context.EmployeeBucketMappings.Where(eb => eb.EmployeeId == LoggedInEmployee.Id).ToListAsync();
if (contactBuckets.Any() && employeeBuckets.Any())
{
List<Guid> contactBucketIds = contactBuckets.Select(cb => cb.BucketId).ToList();
List<Guid> employeeBucketIds = employeeBuckets.Select(eb => eb.BucketId).ToList();
List<Bucket>? buckets = await _context.Buckets.Where(b => contactBucketIds.Contains(b.Id) && employeeBucketIds.Contains(b.Id)).ToListAsync();
List<BucketVM>? bucketVMs = new List<BucketVM>();
foreach (var bucket in buckets)
{
BucketVM bucketVM = bucket.ToBucketVMFromBucket();
bucketVMs.Add(bucketVM);
}
contactVM.Buckets = bucketVMs;
}
List<ContactTagMapping>? contactTags = await _context.ContactTagMappings.Where(ct => ct.ContactId == contact.Id).ToListAsync();
if (contactTags.Any())
{
List<Guid> tagIds = contactTags.Select(ct => ct.ContactTagId).ToList();
List<ContactTagMaster> tagMasters = await _context.ContactTagMasters.Where(t => tagIds.Contains(t.Id)).ToListAsync();
List<ContactTagVM> tagVMs = new List<ContactTagVM>();
foreach (var tagMaster in tagMasters)
{
ContactTagVM tagVM = tagMaster.ToContactTagVMFromContactTagMaster();
tagVMs.Add(tagVM);
}
contactVM.Tags = tagVMs;
}
List<ContactNote>? notes = await _context.ContactNotes.Where(n => n.ContactId == contact.Id && n.IsActive).ToListAsync();
if (notes.Any())
{
List<Guid>? noteIds = notes.Select(n => n.Id).ToList();
List<DirectoryUpdateLog>? noteUpdateLogs = await _context.DirectoryUpdateLogs.Include(l => l.Employee).Where(l => noteIds.Contains(l.Id)).OrderByDescending(l => l.UpdateAt).ToListAsync();
List<ContactNoteVM>? noteVMs = new List<ContactNoteVM>();
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<object>.SuccessResponse(contactVM, "Contact profile fetched successfully");
}
_logger.LogInfo("Employee ID {EmployeeId} sent an empty contact id", LoggedInEmployee.Id);
return ApiResponse<object>.ErrorResponse("Contact ID is empty", "Contact ID is empty", 400);
}
// -------------------------------- Contact Notes -------------------------------- // -------------------------------- Contact Notes --------------------------------
@ -695,7 +811,8 @@ namespace Marco.Pms.Services.Helpers
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
ContactNoteVM noteVM = contactNote.ToContactNoteVMFromContactNote(); 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); _logger.LogInfo("Employee {EmployeeId} updated note {NoteId} at contact {ContactId}", LoggedInEmployee.Id, noteVM.Id, contact.Id);
return ApiResponse<object>.SuccessResponse(noteVM, "Note updated successfully", 200); return ApiResponse<object>.SuccessResponse(noteVM, "Note updated successfully", 200);