Feature_Directory #90
@ -390,9 +390,6 @@ namespace Marco.Pms.DataAccess.Migrations
|
||||
.IsRequired()
|
||||
.HasColumnType("longtext");
|
||||
|
||||
b.Property<Guid?>("ProjectId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<Guid>("TenantId")
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
@ -548,6 +545,32 @@ 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")
|
||||
@ -2678,6 +2701,33 @@ 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")
|
||||
|
@ -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;
|
||||
|
20
Marco.Pms.Model/Directory/ContactProjectMapping.cs
Normal file
20
Marco.Pms.Model/Directory/ContactProjectMapping.cs
Normal file
@ -0,0 +1,20 @@
|
||||
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; }
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
public class CreateContactDto
|
||||
{
|
||||
public Guid? ProjectId { get; set; }
|
||||
public List<Guid>? ProjectIds { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public List<CreateContactPhoneDto>? ContactPhones { get; set; }
|
||||
public List<CreateContactEmailDto>? ContactEmails { get; set; }
|
||||
|
@ -3,7 +3,7 @@
|
||||
public class UpdateContactDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid? ProjectId { get; set; }
|
||||
public List<Guid>? ProjectIds { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public List<UpdateContactPhoneDto>? ContactPhones { get; set; }
|
||||
public List<UpdateContactEmailDto>? ContactEmails { get; set; }
|
||||
|
@ -13,7 +13,6 @@ namespace Marco.Pms.Model.Mapper
|
||||
|
||||
return new Contact
|
||||
{
|
||||
ProjectId = createContactDto.ProjectId,
|
||||
Name = createContactDto.Name ?? string.Empty,
|
||||
ContactCategoryId = createContactDto.ContactCategoryId,
|
||||
Description = createContactDto.Description ?? string.Empty,
|
||||
@ -30,7 +29,6 @@ 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,
|
||||
@ -46,7 +44,6 @@ 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,
|
||||
|
@ -5,11 +5,12 @@ namespace Marco.Pms.Model.ViewModels.Directory
|
||||
public class ContactVM
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid? ProjectId { get; set; }
|
||||
public List<Guid>? ProjectIds { 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; }
|
||||
|
@ -1370,14 +1370,19 @@ namespace Marco.Pms.Services.Helpers
|
||||
{
|
||||
Guid tenantId = _userHelper.GetTenantId();
|
||||
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.Include(c => c.ContactCategory).Where(c => contactIds.Contains(c.Id) && c.TenantId == tenantId && c.IsActive).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 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();
|
||||
|
||||
var TagList = await _context.ContactTagMasters.Where(t => TagIds.Contains(t.Id)).ToListAsync();
|
||||
@ -1395,9 +1400,10 @@ 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)
|
||||
if (emails != null && emails.Count > 0)
|
||||
{
|
||||
foreach (var email in emails)
|
||||
{
|
||||
@ -1407,7 +1413,7 @@ namespace Marco.Pms.Services.Helpers
|
||||
}
|
||||
}
|
||||
|
||||
if (phones != null)
|
||||
if (phones != null && phones.Count > 0)
|
||||
{
|
||||
foreach (var phone in phones)
|
||||
{
|
||||
@ -1417,7 +1423,7 @@ namespace Marco.Pms.Services.Helpers
|
||||
}
|
||||
|
||||
}
|
||||
if (tagMappingss != null)
|
||||
if (tagMappingss != null && tagMappingss.Count > 0)
|
||||
{
|
||||
foreach (var tagMapping in tagMappingss)
|
||||
{
|
||||
@ -1430,9 +1436,17 @@ 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;
|
||||
@ -1559,6 +1573,8 @@ 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)
|
||||
{
|
||||
|
||||
@ -1581,6 +1597,7 @@ 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)
|
||||
@ -1596,8 +1613,29 @@ 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, 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)
|
||||
{
|
||||
foreach (var tag in createContact.Tags)
|
||||
@ -1615,7 +1653,8 @@ namespace Marco.Pms.Services.Helpers
|
||||
{
|
||||
var newtag = new ContactTagMaster
|
||||
{
|
||||
Name = tag.Name
|
||||
Name = tag.Name,
|
||||
TenantId = tenantId
|
||||
};
|
||||
_context.ContactTagMasters.Add(newtag);
|
||||
ContactTagMapping tagMapping = new ContactTagMapping
|
||||
@ -1626,12 +1665,20 @@ 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();
|
||||
|
||||
ContactVM contactVM = new ContactVM();
|
||||
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)
|
||||
{
|
||||
ContactPhoneVM phoneVM = phone.ToContactPhoneVMFromContactPhone();
|
||||
@ -1657,6 +1704,8 @@ 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);
|
||||
}
|
||||
@ -1835,7 +1884,8 @@ namespace Marco.Pms.Services.Helpers
|
||||
ContactTagMaster contactTag = new ContactTagMaster
|
||||
{
|
||||
Name = tag.Name,
|
||||
Description = ""
|
||||
Description = "",
|
||||
TenantId = tenantId
|
||||
};
|
||||
_context.ContactTagMasters.Add(contactTag);
|
||||
|
||||
@ -1848,7 +1898,7 @@ namespace Marco.Pms.Services.Helpers
|
||||
}
|
||||
foreach (var contactTag in contactTags)
|
||||
{
|
||||
if (!updatedTagIds.Contains(contactTag.ContactTagtId))
|
||||
if (!updatedTagIds.Contains(contactTag.Id))
|
||||
{
|
||||
_context.ContactTagMappings.Remove(contactTag);
|
||||
}
|
||||
@ -1871,6 +1921,10 @@ namespace Marco.Pms.Services.Helpers
|
||||
phones = await _context.ContactsPhones.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();
|
||||
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();
|
||||
List<ContactPhoneVM> phoneVMs = new List<ContactPhoneVM>();
|
||||
@ -1899,6 +1953,9 @@ namespace Marco.Pms.Services.Helpers
|
||||
contactVM.ContactPhones = phoneVMs;
|
||||
contactVM.ContactEmails = emailVMs;
|
||||
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);
|
||||
return ApiResponse<object>.SuccessResponse(contactVM, "Contact Updated Successfully", 200);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user