added an API to get list of contacts by bucket id and added project- contact mapping table

This commit is contained in:
ashutosh.nehete 2025-05-17 11:52:45 +05:30
parent ddc6f9e393
commit 45cdbd91c9
6 changed files with 3237 additions and 1 deletions

View File

@ -78,6 +78,7 @@ namespace Marco.Pms.DataAccess.Data
public DbSet<ContactTagMapping> ContactTagMappings { get; set; } public DbSet<ContactTagMapping> ContactTagMappings { get; set; }
public DbSet<EmployeeBucketMapping> EmployeeBucketMappings { get; set; } public DbSet<EmployeeBucketMapping> EmployeeBucketMappings { get; set; }
public DbSet<ContactBucketMapping> ContactBucketMappings { get; set; } public DbSet<ContactBucketMapping> ContactBucketMappings { get; set; }
public DbSet<ContactProjectMapping> ContactProjectMappings { get; set; }
public DbSet<DirectoryUpdateLog> DirectoryUpdateLogs { get; set; } public DbSet<DirectoryUpdateLog> DirectoryUpdateLogs { get; set; }
public DbSet<ContactProjectMapping> ContactProjectMappings { get; set; } public DbSet<ContactProjectMapping> ContactProjectMappings { get; set; }

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,81 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Marco.Pms.DataAccess.Migrations
{
/// <inheritdoc />
public partial class Added_ContactProjectMapping_Table : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ProjectId",
table: "Contacts");
migrationBuilder.CreateTable(
name: "ContactProjectMappings",
columns: table => new
{
Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
ProjectId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
ContactId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
TenantId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci")
},
constraints: table =>
{
table.PrimaryKey("PK_ContactProjectMappings", x => x.Id);
table.ForeignKey(
name: "FK_ContactProjectMappings_Contacts_ContactId",
column: x => x.ContactId,
principalTable: "Contacts",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ContactProjectMappings_Projects_ProjectId",
column: x => x.ProjectId,
principalTable: "Projects",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ContactProjectMappings_Tenants_TenantId",
column: x => x.TenantId,
principalTable: "Tenants",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
})
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateIndex(
name: "IX_ContactProjectMappings_ContactId",
table: "ContactProjectMappings",
column: "ContactId");
migrationBuilder.CreateIndex(
name: "IX_ContactProjectMappings_ProjectId",
table: "ContactProjectMappings",
column: "ProjectId");
migrationBuilder.CreateIndex(
name: "IX_ContactProjectMappings_TenantId",
table: "ContactProjectMappings",
column: "TenantId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ContactProjectMappings");
migrationBuilder.AddColumn<Guid>(
name: "ProjectId",
table: "Contacts",
type: "char(36)",
nullable: true,
collation: "ascii_general_ci");
}
}
}

View File

@ -17,4 +17,4 @@ namespace Marco.Pms.Model.Directory
[ForeignKey("ContactId")] [ForeignKey("ContactId")]
public Contact? Contact { get; set; } public Contact? Contact { get; set; }
} }
} }

View File

@ -63,6 +63,24 @@ namespace Marco.Pms.Services.Controllers
} }
} }
[HttpGet("contact-bucket/{bucketId}")]
public async Task<IActionResult> GetContactsListByBucketId(Guid bucketId)
{
var response = await _directoryHelper.GetContactsListByBucketId(bucketId);
if (response.StatusCode == 200)
{
return Ok(response);
}
else if (response.StatusCode == 401)
{
return Unauthorized(response);
}
else
{
return BadRequest(response);
}
}
[HttpPost] [HttpPost]
public async Task<IActionResult> CreateContact([FromBody] CreateContactDto createContact) public async Task<IActionResult> CreateContact([FromBody] CreateContactDto createContact)
{ {

View File

@ -181,6 +181,102 @@ namespace Marco.Pms.Services.Helpers
} }
public async Task<ApiResponse<object>> GetContactsListByBucketId(Guid id)
{
Guid tenantId = _userHelper.GetTenantId();
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
if (id != Guid.Empty)
{
EmployeeBucketMapping? employeeBucket = await _context.EmployeeBucketMappings.FirstOrDefaultAsync(em => em.BucketId == id && em.EmployeeId == LoggedInEmployee.Id);
if (employeeBucket == null)
{
_logger.LogInfo("Employee ID {EmployeeId} does not have access to bucket ID {BucketId}", LoggedInEmployee.Id);
return ApiResponse<object>.ErrorResponse("You do not have access to this bucket.", "You do not have access to this bucket.", 401);
}
List<ContactBucketMapping> contactBucket = await _context.ContactBucketMappings.Where(cb => cb.BucketId == id).ToListAsync() ?? new List<ContactBucketMapping>();
List<ContactVM> contactVMs = new List<ContactVM>();
if (contactBucket.Count > 0)
{
var contactIds = contactBucket.Select(cb => cb.ContactId).ToList();
List<Contact> contacts = await _context.Contacts.Include(c => c.ContactCategory).Where(c => contactIds.Contains(c.Id) && c.IsActive).ToListAsync();
List<ContactPhone> phones = await _context.ContactsPhones.Where(p => contactIds.Contains(p.ContactId)).ToListAsync();
List<ContactEmail> emails = await _context.ContactsEmails.Where(e => contactIds.Contains(e.ContactId)).ToListAsync();
List<ContactTagMapping>? tags = await _context.ContactTagMappings.Where(ct => contactIds.Contains(ct.ContactId)).ToListAsync();
List<ContactProjectMapping>? contactProjects = await _context.ContactProjectMappings.Where(cp => contactIds.Contains(cp.ContactId)).ToListAsync();
List<ContactBucketMapping>? contactBuckets = await _context.ContactBucketMappings.Where(cp => contactIds.Contains(cp.ContactId)).ToListAsync();
List<Guid> tagIds = new List<Guid>();
List<ContactTagMaster> tagMasters = new List<ContactTagMaster>();
if (tags.Count > 0)
{
tagIds = tags.Select(ct => ct.ContactTagtId).ToList();
tagMasters = await _context.ContactTagMasters.Where(t => tagIds.Contains(t.Id)).ToListAsync();
}
if (contacts.Count > 0)
{
foreach (var contact in contacts)
{
List<ContactEmailVM>? emailVMs = new List<ContactEmailVM>();
List<ContactPhoneVM>? phoneVMs = new List<ContactPhoneVM>();
List<ContactTagVM>? tagVMs = new List<ContactTagVM>();
List<ContactPhone> contactPhones = phones.Where(p => p.ContactId == contact.Id).ToList();
List<ContactEmail> contactEmails = emails.Where(e => e.ContactId == contact.Id).ToList();
List<ContactTagMapping>? contactTags = tags.Where(t => t.ContactId == contact.Id).ToList();
List<ContactProjectMapping>? projectMappings = contactProjects.Where(cp => cp.ContactId == contact.Id).ToList();
List<ContactBucketMapping>? bucketMappings = contactBuckets.Where(cb => cb.ContactId == contact.Id).ToList();
if (contactPhones.Count > 0)
{
foreach (var phone in contactPhones)
{
ContactPhoneVM phoneVM = phone.ToContactPhoneVMFromContactPhone();
phoneVMs.Add(phoneVM);
}
}
if (contactEmails.Count > 0)
{
foreach (var email in contactEmails)
{
ContactEmailVM emailVM = email.ToContactEmailVMFromContactEmail();
emailVMs.Add(emailVM);
}
}
if (contactTags.Count > 0)
{
foreach (var contactTag in contactTags)
{
ContactTagMaster? tagMaster = tagMasters.Find(t => t.Id == contactTag.ContactTagtId);
if (tagMaster != null)
{
ContactTagVM tagVM = tagMaster.ToContactTagVMFromContactTagMaster();
tagVMs.Add(tagVM);
}
}
}
ContactVM contactVM = contact.ToContactVMFromContact();
contactVM.ContactEmails = emailVMs;
contactVM.ContactPhones = phoneVMs;
contactVM.Tags = tagVMs;
contactVM.ProjectIds = projectMappings.Select(cp => cp.ProjectId).ToList();
contactVM.BucketIds = bucketMappings.Select(cb => cb.BucketId).ToList();
contactVMs.Add(contactVM);
}
}
}
_logger.LogInfo("{count} contact from Bucket {BucketId} fetched by Employee {EmployeeId}", contactVMs.Count, id, LoggedInEmployee.Id);
return ApiResponse<object>.SuccessResponse(contactVMs, $"{contactVMs.Count} contacts fetched successfully", 200);
}
_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);
}
public async Task<ApiResponse<object>> GetContactsListByBucketId(Guid id) public async Task<ApiResponse<object>> GetContactsListByBucketId(Guid id)
{ {
Guid tenantId = _userHelper.GetTenantId(); Guid tenantId = _userHelper.GetTenantId();