added an API to get list of contacts by bucket id and added project- contact mapping table
This commit is contained in:
parent
81d3e016c2
commit
9da45240c3
@ -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; }
|
||||||
|
|
||||||
|
3040
Marco.Pms.DataAccess/Migrations/20250517050718_Added_ContactProjectMapping_Table.Designer.cs
generated
Normal file
3040
Marco.Pms.DataAccess/Migrations/20250517050718_Added_ContactProjectMapping_Table.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -9,7 +9,7 @@ namespace Marco.Pms.Model.Directory
|
|||||||
public class Contact : TenantRelation
|
public class Contact : TenantRelation
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public Guid? ProjectId { get; set; }
|
//public Guid? ProjectId { get; set; }
|
||||||
public string Name { get; set; } = string.Empty;
|
public string Name { get; set; } = string.Empty;
|
||||||
public string Description { get; set; } = string.Empty;
|
public string Description { get; set; } = string.Empty;
|
||||||
public string Organization { get; set; } = string.Empty;
|
public string Organization { get; set; } = string.Empty;
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
{
|
{
|
||||||
public class CreateContactDto
|
public class CreateContactDto
|
||||||
{
|
{
|
||||||
public Guid? ProjectId { get; set; }
|
public List<Guid>? ProjectIds { get; set; }
|
||||||
public string? Name { get; set; }
|
public string? Name { get; set; }
|
||||||
public List<CreateContactPhoneDto>? ContactPhones { get; set; }
|
public List<CreateContactPhoneDto>? ContactPhones { get; set; }
|
||||||
public List<CreateContactEmailDto>? ContactEmails { get; set; }
|
public List<CreateContactEmailDto>? ContactEmails { get; set; }
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
public class UpdateContactDto
|
public class UpdateContactDto
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public Guid? ProjectId { get; set; }
|
public List<Guid>? ProjectIds { get; set; }
|
||||||
public string? Name { get; set; }
|
public string? Name { get; set; }
|
||||||
public List<UpdateContactPhoneDto>? ContactPhones { get; set; }
|
public List<UpdateContactPhoneDto>? ContactPhones { get; set; }
|
||||||
public List<UpdateContactEmailDto>? ContactEmails { get; set; }
|
public List<UpdateContactEmailDto>? ContactEmails { get; set; }
|
||||||
|
@ -13,7 +13,6 @@ namespace Marco.Pms.Model.Mapper
|
|||||||
|
|
||||||
return new Contact
|
return new Contact
|
||||||
{
|
{
|
||||||
ProjectId = createContactDto.ProjectId,
|
|
||||||
Name = createContactDto.Name ?? string.Empty,
|
Name = createContactDto.Name ?? string.Empty,
|
||||||
ContactCategoryId = createContactDto.ContactCategoryId,
|
ContactCategoryId = createContactDto.ContactCategoryId,
|
||||||
Description = createContactDto.Description ?? string.Empty,
|
Description = createContactDto.Description ?? string.Empty,
|
||||||
@ -30,7 +29,6 @@ namespace Marco.Pms.Model.Mapper
|
|||||||
return new Contact
|
return new Contact
|
||||||
{
|
{
|
||||||
Id = updateContactDto.Id,
|
Id = updateContactDto.Id,
|
||||||
ProjectId = updateContactDto.ProjectId,
|
|
||||||
Name = updateContactDto.Name ?? string.Empty,
|
Name = updateContactDto.Name ?? string.Empty,
|
||||||
ContactCategoryId = updateContactDto.ContactCategoryId,
|
ContactCategoryId = updateContactDto.ContactCategoryId,
|
||||||
Description = updateContactDto.Description ?? string.Empty,
|
Description = updateContactDto.Description ?? string.Empty,
|
||||||
@ -44,7 +42,6 @@ namespace Marco.Pms.Model.Mapper
|
|||||||
return new ContactVM
|
return new ContactVM
|
||||||
{
|
{
|
||||||
Id = contact.Id,
|
Id = contact.Id,
|
||||||
ProjectId = contact.ProjectId,
|
|
||||||
Name = contact.Name,
|
Name = contact.Name,
|
||||||
ContactCategory = contact.ContactCategory != null ? contact.ContactCategory.ToContactCategoryVMFromContactCategoryMaster() : null,
|
ContactCategory = contact.ContactCategory != null ? contact.ContactCategory.ToContactCategoryVMFromContactCategoryMaster() : null,
|
||||||
Description = contact.Description ?? string.Empty,
|
Description = contact.Description ?? string.Empty,
|
||||||
|
@ -5,11 +5,12 @@ namespace Marco.Pms.Model.ViewModels.Directory
|
|||||||
public class ContactVM
|
public class ContactVM
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public Guid? ProjectId { get; set; }
|
public List<Guid>? ProjectIds { get; set; }
|
||||||
public string? Name { get; set; }
|
public string? Name { get; set; }
|
||||||
public List<ContactPhoneVM>? ContactPhones { get; set; }
|
public List<ContactPhoneVM>? ContactPhones { get; set; }
|
||||||
public List<ContactEmailVM>? ContactEmails { get; set; }
|
public List<ContactEmailVM>? ContactEmails { get; set; }
|
||||||
public ContactCategoryVM? ContactCategory { get; set; }
|
public ContactCategoryVM? ContactCategory { get; set; }
|
||||||
|
public List<Guid>? BucketIds { get; set; }
|
||||||
public string? Description { get; set; }
|
public string? Description { get; set; }
|
||||||
public string? Organization { get; set; }
|
public string? Organization { get; set; }
|
||||||
public string? Address { get; set; }
|
public string? Address { get; set; }
|
||||||
|
@ -41,6 +41,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)
|
||||||
{
|
{
|
||||||
|
@ -1370,13 +1370,19 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
{
|
{
|
||||||
Guid tenantId = _userHelper.GetTenantId();
|
Guid tenantId = _userHelper.GetTenantId();
|
||||||
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||||
|
List<EmployeeBucketMapping>? employeeBuckets = await _context.EmployeeBucketMappings.Where(eb => eb.EmployeeId == LoggedInEmployee.Id).ToListAsync();
|
||||||
|
List<Guid> bucketIds = employeeBuckets.Select(c => c.BucketId).ToList();
|
||||||
|
|
||||||
|
List<ContactBucketMapping>? contactBuckets = await _context.ContactBucketMappings.Where(cb => bucketIds.Contains(cb.BucketId)).ToListAsync();
|
||||||
|
List<Guid> contactIds = contactBuckets.Select(cb => cb.ContactId).ToList();
|
||||||
|
var contacts = await _context.Contacts.Where(c => contactIds.Contains(c.Id) && c.TenantId == tenantId && c.IsActive).ToListAsync();
|
||||||
|
|
||||||
var contacts = await _context.Contacts.Where(c => c.TenantId == tenantId).ToListAsync();
|
|
||||||
List<Guid> contactIds = contacts.Select(c => c.Id).ToList();
|
|
||||||
|
|
||||||
var phoneNo = await _context.ContactsPhones.Where(p => contactIds.Contains(p.ContactId)).ToListAsync();
|
var phoneNo = await _context.ContactsPhones.Where(p => contactIds.Contains(p.ContactId)).ToListAsync();
|
||||||
var Emails = await _context.ContactsEmails.Where(E => contactIds.Contains(E.ContactId)).ToListAsync();
|
var Emails = await _context.ContactsEmails.Where(E => contactIds.Contains(E.ContactId)).ToListAsync();
|
||||||
var Tags = await _context.ContactTagMappings.Where(t => contactIds.Contains(t.ContactId)).ToListAsync();
|
var Tags = await _context.ContactTagMappings.Where(t => contactIds.Contains(t.ContactId)).ToListAsync();
|
||||||
|
var contactProjects = await _context.ContactProjectMappings.Where(p => contactIds.Contains(p.ContactId)).ToListAsync();
|
||||||
|
|
||||||
List<Guid> TagIds = Tags.Select(t => t.ContactTagtId).ToList();
|
List<Guid> TagIds = Tags.Select(t => t.ContactTagtId).ToList();
|
||||||
|
|
||||||
var TagList = await _context.ContactTagMasters.Where(t => TagIds.Contains(t.Id)).ToListAsync();
|
var TagList = await _context.ContactTagMasters.Where(t => TagIds.Contains(t.Id)).ToListAsync();
|
||||||
@ -1394,9 +1400,10 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
var phones = phoneNo.Where(p => p.ContactId == contact.Id).ToList();
|
var phones = phoneNo.Where(p => p.ContactId == contact.Id).ToList();
|
||||||
var emails = Emails.Where(e => e.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 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)
|
||||||
if (emails != null)
|
|
||||||
{
|
{
|
||||||
foreach (var email in emails)
|
foreach (var email in emails)
|
||||||
{
|
{
|
||||||
@ -1406,7 +1413,7 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (phones != null)
|
if (phones != null && phones.Count > 0)
|
||||||
{
|
{
|
||||||
foreach (var phone in phones)
|
foreach (var phone in phones)
|
||||||
{
|
{
|
||||||
@ -1416,7 +1423,7 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if (tagMappingss != null)
|
if (tagMappingss != null && tagMappingss.Count > 0)
|
||||||
{
|
{
|
||||||
foreach (var tagMapping in tagMappingss)
|
foreach (var tagMapping in tagMappingss)
|
||||||
{
|
{
|
||||||
@ -1429,9 +1436,17 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
contactVM = contact.ToContactVMFromContact();
|
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.ContactEmails = contactEmailVms;
|
||||||
contactVM.ContactPhones = contactPhoneVms;
|
contactVM.ContactPhones = contactPhoneVms;
|
||||||
contactVM.Tags = conatctTagVms;
|
contactVM.Tags = conatctTagVms;
|
||||||
@ -1443,6 +1458,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>> CreateContact(CreateContactDto createContact)
|
public async Task<ApiResponse<object>> CreateContact(CreateContactDto createContact)
|
||||||
{
|
{
|
||||||
Guid tenantId = _userHelper.GetTenantId();
|
Guid tenantId = _userHelper.GetTenantId();
|
||||||
@ -1462,6 +1573,8 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
|
|
||||||
var tags = await _context.ContactTagMasters.Where(t => t.TenantId == tenantId).ToListAsync();
|
var tags = await _context.ContactTagMasters.Where(t => t.TenantId == tenantId).ToListAsync();
|
||||||
var buckets = await _context.Buckets.Where(b => b.TenantId == tenantId).Select(b => b.Id).ToListAsync();
|
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)
|
if (createContact.ContactPhones != null)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -1484,6 +1597,7 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
_context.ContactsEmails.AddRange(emails);
|
_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);
|
_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)
|
if (createContact.BucketIds != null)
|
||||||
{
|
{
|
||||||
foreach (var bucket in createContact.BucketIds)
|
foreach (var bucket in createContact.BucketIds)
|
||||||
@ -1499,8 +1613,29 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
_context.ContactBucketMappings.AddRange(contactBucketMappings);
|
_context.ContactBucketMappings.AddRange(contactBucketMappings);
|
||||||
_logger.LogInfo("Contact with ID {ContactId} added to {count} number of buckets by employee with ID {LoggedEmployeeId}", contact.Id, LoggedInEmployee.Id);
|
_logger.LogInfo("Contact with ID {ContactId} added to {count} number of buckets by employee with ID {LoggedEmployeeId}", contact.Id, contactBucketMappings.Count, 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)
|
if (createContact.Tags != null)
|
||||||
{
|
{
|
||||||
foreach (var tag in createContact.Tags)
|
foreach (var tag in createContact.Tags)
|
||||||
@ -1518,7 +1653,8 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
{
|
{
|
||||||
var newtag = new ContactTagMaster
|
var newtag = new ContactTagMaster
|
||||||
{
|
{
|
||||||
Name = tag.Name
|
Name = tag.Name,
|
||||||
|
TenantId = tenantId
|
||||||
};
|
};
|
||||||
_context.ContactTagMasters.Add(newtag);
|
_context.ContactTagMasters.Add(newtag);
|
||||||
ContactTagMapping tagMapping = new ContactTagMapping
|
ContactTagMapping tagMapping = new ContactTagMapping
|
||||||
@ -1529,12 +1665,20 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
contactTagMappings.Add(tagMapping);
|
contactTagMappings.Add(tagMapping);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_context.ContactTagMappings.AddRange(contactTagMappings);
|
_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();
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
ContactVM contactVM = new ContactVM();
|
ContactVM contactVM = new ContactVM();
|
||||||
List<ContactPhoneVM> phoneVMs = new List<ContactPhoneVM>();
|
List<ContactPhoneVM> phoneVMs = new List<ContactPhoneVM>();
|
||||||
|
|
||||||
|
contact = await _context.Contacts.Include(c => c.ContactCategory).FirstOrDefaultAsync(c => c.Id == contact.Id) ?? new Contact();
|
||||||
|
var tagIds = contactTagMappings.Select(t => t.ContactTagtId).ToList();
|
||||||
|
tags = await _context.ContactTagMasters.Where(t => t.TenantId == tenantId && tagIds.Contains(t.Id)).ToListAsync();
|
||||||
|
List<ContactProjectMapping> contactProjects = await _context.ContactProjectMappings.Where(cp => cp.ContactId == contact.Id).ToListAsync();
|
||||||
|
List<ContactBucketMapping> bucketMappings = await _context.ContactBucketMappings.Where(cb => cb.ContactId == contact.Id).ToListAsync();
|
||||||
foreach (var phone in phones)
|
foreach (var phone in phones)
|
||||||
{
|
{
|
||||||
ContactPhoneVM phoneVM = phone.ToContactPhoneVMFromContactPhone();
|
ContactPhoneVM phoneVM = phone.ToContactPhoneVMFromContactPhone();
|
||||||
@ -1560,6 +1704,8 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
contactVM.ContactPhones = phoneVMs;
|
contactVM.ContactPhones = phoneVMs;
|
||||||
contactVM.ContactEmails = emailVMs;
|
contactVM.ContactEmails = emailVMs;
|
||||||
contactVM.Tags = tagVMs;
|
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);
|
return ApiResponse<object>.SuccessResponse(contactVM, "Contact Created Successfully", 200);
|
||||||
}
|
}
|
||||||
@ -1578,7 +1724,7 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended different ID in payload and path parameter", LoggedInEmployee.Id);
|
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended different ID in payload and path parameter", LoggedInEmployee.Id);
|
||||||
return ApiResponse<object>.ErrorResponse("Invalid data", "Invalid data", 400);
|
return ApiResponse<object>.ErrorResponse("Invalid data", "Invalid data", 400);
|
||||||
}
|
}
|
||||||
Contact? contact = await _context.Contacts.FirstOrDefaultAsync(c => c.Id == id && c.TenantId == tenantId);
|
Contact? contact = await _context.Contacts.FirstOrDefaultAsync(c => c.Id == id && c.IsActive && c.TenantId == tenantId);
|
||||||
if (contact == null)
|
if (contact == null)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} tries to update contact with ID {ContactId} is not found in database", LoggedInEmployee.Id);
|
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} tries to update contact with ID {ContactId} is not found in database", LoggedInEmployee.Id);
|
||||||
@ -1598,6 +1744,9 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
List<ContactTagMapping> contactTags = await _context.ContactTagMappings.AsNoTracking().Where(m => m.ContactId == contact.Id).ToListAsync();
|
List<ContactTagMapping> contactTags = await _context.ContactTagMappings.AsNoTracking().Where(m => m.ContactId == contact.Id).ToListAsync();
|
||||||
var tagIds = contactTags.Select(t => t.ContactTagtId).Distinct().ToList();
|
var tagIds = contactTags.Select(t => t.ContactTagtId).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> tags = await _context.ContactTagMasters.Where(t => tagIds.Contains(t.Id)).ToListAsync();
|
||||||
|
|
||||||
if (updateContact.ContactPhones != null)
|
if (updateContact.ContactPhones != null)
|
||||||
@ -1685,6 +1834,33 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
_context.ContactBucketMappings.RemoveRange(contactBuckets);
|
_context.ContactBucketMappings.RemoveRange(contactBuckets);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (updateContact.ProjectIds != null)
|
||||||
|
{
|
||||||
|
foreach (var ProjectId in updateContact.ProjectIds)
|
||||||
|
{
|
||||||
|
if (!projectIds.Contains(ProjectId))
|
||||||
|
{
|
||||||
|
_context.ContactProjectMappings.Add(new ContactProjectMapping
|
||||||
|
{
|
||||||
|
ProjectId = ProjectId,
|
||||||
|
ContactId = contact.Id
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var projectMapping in contactProjects)
|
||||||
|
{
|
||||||
|
if (!updateContact.ProjectIds.Contains(projectMapping.ProjectId))
|
||||||
|
{
|
||||||
|
_context.ContactProjectMappings.Remove(projectMapping);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (contactProjects != null)
|
||||||
|
{
|
||||||
|
_context.ContactProjectMappings.RemoveRange(contactProjects);
|
||||||
|
}
|
||||||
|
|
||||||
if (updateContact.Tags != null)
|
if (updateContact.Tags != null)
|
||||||
{
|
{
|
||||||
var updatedTagIds = updateContact.Tags.Select(t => t.Id).Distinct().ToList();
|
var updatedTagIds = updateContact.Tags.Select(t => t.Id).Distinct().ToList();
|
||||||
@ -1703,7 +1879,8 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
ContactTagMaster contactTag = new ContactTagMaster
|
ContactTagMaster contactTag = new ContactTagMaster
|
||||||
{
|
{
|
||||||
Name = tag.Name,
|
Name = tag.Name,
|
||||||
Description = ""
|
Description = "",
|
||||||
|
TenantId = tenantId
|
||||||
};
|
};
|
||||||
_context.ContactTagMasters.Add(contactTag);
|
_context.ContactTagMasters.Add(contactTag);
|
||||||
|
|
||||||
@ -1716,7 +1893,7 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
}
|
}
|
||||||
foreach (var contactTag in contactTags)
|
foreach (var contactTag in contactTags)
|
||||||
{
|
{
|
||||||
if (!updatedTagIds.Contains(contactTag.ContactTagtId))
|
if (!updatedTagIds.Contains(contactTag.Id))
|
||||||
{
|
{
|
||||||
_context.ContactTagMappings.Remove(contactTag);
|
_context.ContactTagMappings.Remove(contactTag);
|
||||||
}
|
}
|
||||||
@ -1738,6 +1915,10 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
phones = await _context.ContactsPhones.AsNoTracking().Where(p => p.ContactId == contact.Id).ToListAsync();
|
phones = await _context.ContactsPhones.AsNoTracking().Where(p => p.ContactId == contact.Id).ToListAsync();
|
||||||
emails = await _context.ContactsEmails.AsNoTracking().Where(p => p.ContactId == contact.Id).ToListAsync();
|
emails = await _context.ContactsEmails.AsNoTracking().Where(p => p.ContactId == contact.Id).ToListAsync();
|
||||||
contactTags = await _context.ContactTagMappings.AsNoTracking().Where(m => m.ContactId == contact.Id).ToListAsync();
|
contactTags = await _context.ContactTagMappings.AsNoTracking().Where(m => m.ContactId == contact.Id).ToListAsync();
|
||||||
|
contactBuckets = await _context.ContactBucketMappings.AsNoTracking().Where(cb => cb.ContactId == contact.Id).ToListAsync();
|
||||||
|
contactProjects = await _context.ContactProjectMappings.AsNoTracking().Where(cp => cp.ContactId == contact.Id).ToListAsync();
|
||||||
|
tagIds = contactTags.Select(t => t.ContactTagtId).Distinct().ToList();
|
||||||
|
tags = await _context.ContactTagMasters.Where(t => tagIds.Contains(t.Id)).ToListAsync();
|
||||||
|
|
||||||
ContactVM contactVM = new ContactVM();
|
ContactVM contactVM = new ContactVM();
|
||||||
List<ContactPhoneVM> phoneVMs = new List<ContactPhoneVM>();
|
List<ContactPhoneVM> phoneVMs = new List<ContactPhoneVM>();
|
||||||
@ -1766,6 +1947,9 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
contactVM.ContactPhones = phoneVMs;
|
contactVM.ContactPhones = phoneVMs;
|
||||||
contactVM.ContactEmails = emailVMs;
|
contactVM.ContactEmails = emailVMs;
|
||||||
contactVM.Tags = tagVMs;
|
contactVM.Tags = tagVMs;
|
||||||
|
contactVM.BucketIds = contactBuckets.Select(cb => cb.BucketId).ToList();
|
||||||
|
contactVM.ProjectIds = contactProjects.Select(cp => cp.ProjectId).ToList();
|
||||||
|
|
||||||
_logger.LogInfo("Conatct {ContactId} has been updated by employee {EmployeeId}", contact.Id, LoggedInEmployee.Id);
|
_logger.LogInfo("Conatct {ContactId} has been updated by employee {EmployeeId}", contact.Id, LoggedInEmployee.Id);
|
||||||
return ApiResponse<object>.SuccessResponse(contactVM, "Contact Updated Successfully", 200);
|
return ApiResponse<object>.SuccessResponse(contactVM, "Contact Updated Successfully", 200);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user