Feature_Directory #90

Merged
vikas.nale merged 501 commits from Feature_Directory into main 2025-06-10 14:44:36 +00:00
12 changed files with 17 additions and 3364 deletions
Showing only changes of commit 17398dafa6 - Show all commits

View File

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

View File

@ -1,81 +0,0 @@
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

@ -390,6 +390,9 @@ namespace Marco.Pms.DataAccess.Migrations
.IsRequired()
.HasColumnType("longtext");
b.Property<Guid?>("ProjectId")
.HasColumnType("char(36)");
b.Property<Guid>("TenantId")
.HasColumnType("char(36)");
@ -545,32 +548,6 @@ namespace Marco.Pms.DataAccess.Migrations
b.ToTable("ContactsPhones");
});
modelBuilder.Entity("Marco.Pms.Model.Directory.ContactProjectMapping", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<Guid>("ContactId")
.HasColumnType("char(36)");
b.Property<Guid>("ProjectId")
.HasColumnType("char(36)");
b.Property<Guid>("TenantId")
.HasColumnType("char(36)");
b.HasKey("Id");
b.HasIndex("ContactId");
b.HasIndex("ProjectId");
b.HasIndex("TenantId");
b.ToTable("ContactProjectMappings");
});
modelBuilder.Entity("Marco.Pms.Model.Directory.ContactTagMapping", b =>
{
b.Property<Guid>("Id")
@ -2701,33 +2678,6 @@ namespace Marco.Pms.DataAccess.Migrations
b.Navigation("Contact");
});
modelBuilder.Entity("Marco.Pms.Model.Directory.ContactProjectMapping", b =>
{
b.HasOne("Marco.Pms.Model.Directory.Contact", "Contact")
.WithMany()
.HasForeignKey("ContactId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Marco.Pms.Model.Projects.Project", "Project")
.WithMany()
.HasForeignKey("ProjectId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Marco.Pms.Model.Entitlements.Tenant", "Tenant")
.WithMany()
.HasForeignKey("TenantId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Contact");
b.Navigation("Project");
b.Navigation("Tenant");
});
modelBuilder.Entity("Marco.Pms.Model.Directory.ContactTagMapping", b =>
{
b.HasOne("Marco.Pms.Model.Directory.Contact", "Contact")

View File

@ -9,7 +9,7 @@ namespace Marco.Pms.Model.Directory
public class Contact : TenantRelation
{
public Guid Id { get; set; }
//public Guid? ProjectId { get; set; }
public Guid? ProjectId { get; set; }
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public string Organization { get; set; } = string.Empty;

View File

@ -1,20 +0,0 @@
using System.ComponentModel.DataAnnotations.Schema;
using Marco.Pms.Model.Projects;
using Marco.Pms.Model.Utilities;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
namespace Marco.Pms.Model.Directory
{
public class ContactProjectMapping : TenantRelation
{
public Guid Id { get; set; }
public Guid ProjectId { get; set; }
[ValidateNever]
[ForeignKey("ProjectId")]
public Project? Project { get; set; }
public Guid ContactId { get; set; }
[ValidateNever]
[ForeignKey("ContactId")]
public Contact? Contact { get; set; }
}
}

View File

@ -2,7 +2,7 @@
{
public class CreateContactDto
{
public List<Guid>? ProjectIds { get; set; }
public Guid? ProjectId { get; set; }
public string? Name { get; set; }
public List<CreateContactPhoneDto>? ContactPhones { get; set; }
public List<CreateContactEmailDto>? ContactEmails { get; set; }

View File

@ -3,7 +3,7 @@
public class UpdateContactDto
{
public Guid Id { get; set; }
public List<Guid>? ProjectIds { get; set; }
public Guid? ProjectId { get; set; }
public string? Name { get; set; }
public List<UpdateContactPhoneDto>? ContactPhones { get; set; }
public List<UpdateContactEmailDto>? ContactEmails { get; set; }

View File

@ -13,6 +13,7 @@ namespace Marco.Pms.Model.Mapper
return new Contact
{
ProjectId = createContactDto.ProjectId,
Name = createContactDto.Name ?? string.Empty,
ContactCategoryId = createContactDto.ContactCategoryId,
Description = createContactDto.Description ?? string.Empty,
@ -29,6 +30,7 @@ namespace Marco.Pms.Model.Mapper
return new Contact
{
Id = updateContactDto.Id,
ProjectId = updateContactDto.ProjectId,
Name = updateContactDto.Name ?? string.Empty,
ContactCategoryId = updateContactDto.ContactCategoryId,
CreatedAt = contact.CreatedAt,
@ -44,6 +46,7 @@ namespace Marco.Pms.Model.Mapper
return new ContactVM
{
Id = contact.Id,
ProjectId = contact.ProjectId,
Name = contact.Name,
ContactCategory = contact.ContactCategory != null ? contact.ContactCategory.ToContactCategoryVMFromContactCategoryMaster() : null,
Description = contact.Description ?? string.Empty,

View File

@ -5,12 +5,11 @@ namespace Marco.Pms.Model.ViewModels.Directory
public class ContactVM
{
public Guid Id { get; set; }
public List<Guid>? ProjectIds { get; set; }
public Guid? ProjectId { get; set; }
public string? Name { get; set; }
public List<ContactPhoneVM>? ContactPhones { get; set; }
public List<ContactEmailVM>? ContactEmails { get; set; }
public ContactCategoryVM? ContactCategory { get; set; }
public List<Guid>? BucketIds { get; set; }
public string? Description { get; set; }
public string? Organization { get; set; }
public string? Address { get; set; }

View File

@ -63,24 +63,6 @@ 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]
public async Task<IActionResult> CreateContact([FromBody] CreateContactDto createContact)
{

View File

@ -122,8 +122,6 @@ namespace Marco.Pms.Services.Helpers
var phones = phoneNo.Where(p => p.ContactId == contact.Id).ToList();
var emails = Emails.Where(e => e.ContactId == contact.Id).ToList();
var tagMappingss = Tags.Where(t => t.ContactId == contact.Id).ToList();
var projectMapping = contactProjects.Where(p => p.ContactId == contact.Id).ToList();
var bucketMapping = contactBuckets.Where(b => b.ContactId == contact.Id).ToList();
if (emails != null && emails.Count > 0)
@ -136,7 +134,7 @@ namespace Marco.Pms.Services.Helpers
}
}
if (phones != null && phones.Count > 0)
if (phones != null)
{
foreach (var phone in phones)
{
@ -146,7 +144,7 @@ namespace Marco.Pms.Services.Helpers
}
}
if (tagMappingss != null && tagMappingss.Count > 0)
if (tagMappingss != null)
{
foreach (var tagMapping in tagMappingss)
{
@ -159,17 +157,9 @@ namespace Marco.Pms.Services.Helpers
}
}
contactVM = contact.ToContactVMFromContact();
if (projectMapping != null && projectMapping.Count > 0)
{
contactVM.ProjectIds = projectMapping.Select(p => p.ProjectId).ToList();
}
if (bucketMapping != null && bucketMapping.Count > 0)
{
contactVM.BucketIds = bucketMapping.Select(p => p.BucketId).ToList();
}
contactVM.ContactEmails = contactEmailVms;
contactVM.ContactPhones = contactPhoneVms;
contactVM.Tags = conatctTagVms;
@ -181,102 +171,6 @@ 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)
{
Guid tenantId = _userHelper.GetTenantId();
@ -417,8 +311,6 @@ namespace Marco.Pms.Services.Helpers
var tags = await _context.ContactTagMasters.Where(t => t.TenantId == tenantId).ToListAsync();
var tagNames = tags.Select(t => t.Name.ToLower()).ToList();
var buckets = await _context.Buckets.Where(b => b.TenantId == tenantId).Select(b => b.Id).ToListAsync();
var projects = await _context.Projects.Where(p => p.TenantId == tenantId).Select(p => p.Id).ToListAsync();
if (createContact.ContactPhones != null)
{
@ -441,7 +333,6 @@ namespace Marco.Pms.Services.Helpers
_context.ContactsEmails.AddRange(emails);
_logger.LogInfo("{count} email addresses are saved in contact with ID {ContactId} by employee with ID {LoggedEmployeeId}", emails.Count, contact.Id, LoggedInEmployee.Id);
}
if (createContact.BucketIds != null)
{
foreach (var bucket in createContact.BucketIds)
@ -457,29 +348,8 @@ namespace Marco.Pms.Services.Helpers
}
}
_context.ContactBucketMappings.AddRange(contactBucketMappings);
_logger.LogInfo("Contact with ID {ContactId} added to {count} number of buckets by employee with ID {LoggedEmployeeId}", contact.Id, contactBucketMappings.Count, LoggedInEmployee.Id);
_logger.LogInfo("Contact with ID {ContactId} added to {count} number of buckets by employee with ID {LoggedEmployeeId}", contact.Id, LoggedInEmployee.Id);
}
if (createContact.ProjectIds != null)
{
List<ContactProjectMapping> projectMappings = new List<ContactProjectMapping>();
foreach (var projectId in createContact.ProjectIds)
{
if (projects.Contains(projectId))
{
ContactProjectMapping projectMapping = new ContactProjectMapping
{
ProjectId = projectId,
ContactId = contact.Id,
TenantId = tenantId
};
projectMappings.Add(projectMapping);
}
}
_context.ContactProjectMappings.AddRange(projectMappings);
_logger.LogInfo("Contact with ID {ContactId} added to {count} number of project by employee with ID {LoggedEmployeeId}", contact.Id, projectMappings.Count, LoggedInEmployee.Id);
}
if (createContact.Tags != null)
{
foreach (var tag in createContact.Tags)
@ -1782,8 +1652,7 @@ namespace Marco.Pms.Services.Helpers
{
var newtag = new ContactTagMaster
{
Name = tag.Name,
TenantId = tenantId
Name = tag.Name
};
_context.ContactTagMasters.Add(newtag);
ContactTagMapping tagMapping = new ContactTagMapping
@ -1794,9 +1663,7 @@ namespace Marco.Pms.Services.Helpers
contactTagMappings.Add(tagMapping);
}
}
_context.ContactTagMappings.AddRange(contactTagMappings);
_logger.LogInfo("{count} number of tags added to Contact with ID {ContactId} by employee with ID {LoggedEmployeeId}", contactTagMappings.Count, contact.Id, LoggedInEmployee.Id);
}
await _context.SaveChangesAsync();
@ -1833,8 +1700,6 @@ namespace Marco.Pms.Services.Helpers
contactVM.ContactPhones = phoneVMs;
contactVM.ContactEmails = emailVMs;
contactVM.Tags = tagVMs;
contactVM.ProjectIds = contactProjects.Select(cp => cp.ProjectId).ToList();
contactVM.BucketIds = bucketMappings.Select(cb => cb.BucketId).ToList();
return ApiResponse<object>.SuccessResponse(contactVM, "Contact Created Successfully", 200);
}
@ -1875,9 +1740,6 @@ namespace Marco.Pms.Services.Helpers
var tagIds = contactTags.Select(t => t.ContactTagId).Distinct().ToList();
List<ContactProjectMapping> contactProjects = await _context.ContactProjectMappings.AsNoTracking().Where(m => m.ContactId == contact.Id).ToListAsync();
var projectIds = contactProjects.Select(t => t.ProjectId).Distinct().ToList();
List<ContactTagMaster> tags = await _context.ContactTagMasters.Where(t => tagIds.Contains(t.Id)).ToListAsync();
List<ContactTagMaster> allTags = await _context.ContactTagMasters.Where(t => t.TenantId == tenantId).ToListAsync();
var tagNames = allTags.Select(t => t.Name.ToLower()).ToList();
@ -2017,8 +1879,7 @@ namespace Marco.Pms.Services.Helpers
ContactTagMaster contactTag = new ContactTagMaster
{
Name = tag.Name,
Description = "",
TenantId = tenantId
Description = ""
};
_context.ContactTagMasters.Add(contactTag);