Merge pull request 'Ashutosh_Task#226_Create_Contact' (#49) from Ashutosh_Task#226_Create_Contact into Feature_Directory
Reviewed-on: #49
This commit is contained in:
commit
481b01a53f
@ -4,6 +4,6 @@
|
|||||||
{
|
{
|
||||||
public string? Label { get; set; }
|
public string? Label { get; set; }
|
||||||
public string? EmailAddress { get; set; }
|
public string? EmailAddress { get; set; }
|
||||||
public Guid? ContactId { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,5 @@
|
|||||||
{
|
{
|
||||||
public string? Label { get; set; }
|
public string? Label { get; set; }
|
||||||
public string? PhoneNumber { get; set; }
|
public string? PhoneNumber { get; set; }
|
||||||
public Guid? ContactId { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ namespace Marco.Pms.Model.Mapper
|
|||||||
Id = contact.Id,
|
Id = contact.Id,
|
||||||
ProjectId = contact.ProjectId,
|
ProjectId = contact.ProjectId,
|
||||||
Name = contact.Name,
|
Name = contact.Name,
|
||||||
ContactCategory = contact.ContactCategory != null ? contact.ContactCategory.ToContactCategoryVMFromContactCategoryMaster() : new ContactCategoryVM(),
|
ContactCategory = contact.ContactCategory != null ? contact.ContactCategory.ToContactCategoryVMFromContactCategoryMaster() : null,
|
||||||
Description = contact.Description ?? string.Empty,
|
Description = contact.Description ?? string.Empty,
|
||||||
Organization = contact.Organization ?? string.Empty,
|
Organization = contact.Organization ?? string.Empty,
|
||||||
Address = contact.Address ?? string.Empty
|
Address = contact.Address ?? string.Empty
|
||||||
|
@ -2,12 +2,14 @@
|
|||||||
using Marco.Pms.Model.Utilities;
|
using Marco.Pms.Model.Utilities;
|
||||||
using Marco.Pms.Services.Helpers;
|
using Marco.Pms.Services.Helpers;
|
||||||
using MarcoBMS.Services.Service;
|
using MarcoBMS.Services.Service;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace Marco.Pms.Services.Controllers
|
namespace Marco.Pms.Services.Controllers
|
||||||
{
|
{
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
|
[Authorize]
|
||||||
|
|
||||||
public class DirectoryController : ControllerBase
|
public class DirectoryController : ControllerBase
|
||||||
{
|
{
|
||||||
@ -51,8 +53,15 @@ namespace Marco.Pms.Services.Controllers
|
|||||||
_logger.LogError("User sent Invalid Date while marking attendance");
|
_logger.LogError("User sent Invalid Date while marking attendance");
|
||||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
|
||||||
}
|
}
|
||||||
|
var response = await _directoryHelper.CreateContact(createContact);
|
||||||
return Ok();
|
if (response.StatusCode == 200)
|
||||||
|
{
|
||||||
|
return Ok(response);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return BadRequest(response);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using Marco.Pms.DataAccess.Data;
|
using Marco.Pms.DataAccess.Data;
|
||||||
using Marco.Pms.Model.Directory;
|
using Marco.Pms.Model.Directory;
|
||||||
|
using Marco.Pms.Model.Dtos.Directory;
|
||||||
using Marco.Pms.Model.Mapper;
|
using Marco.Pms.Model.Mapper;
|
||||||
using Marco.Pms.Model.Utilities;
|
using Marco.Pms.Model.Utilities;
|
||||||
using Marco.Pms.Model.ViewModels.Directory;
|
using Marco.Pms.Model.ViewModels.Directory;
|
||||||
@ -29,18 +30,14 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
public async Task<ApiResponse<object>> GetListOfContacts()
|
public async Task<ApiResponse<object>> GetListOfContacts()
|
||||||
{
|
{
|
||||||
Guid tenantId = _userHelper.GetTenantId();
|
Guid tenantId = _userHelper.GetTenantId();
|
||||||
|
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||||
|
|
||||||
var contacts = await _context.Contacts.Where(c => c.TenantId == tenantId).ToListAsync();
|
var contacts = await _context.Contacts.Where(c => c.TenantId == tenantId).ToListAsync();
|
||||||
|
|
||||||
List<Guid> contactIds = contacts.Select(c => c.Id).ToList();
|
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();
|
||||||
|
|
||||||
|
|
||||||
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();
|
||||||
@ -80,14 +77,14 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
if (tagMappingss != null)
|
||||||
if (tagMappingss != null) {
|
{
|
||||||
foreach (var tagMapping in tagMappingss)
|
foreach (var tagMapping in tagMappingss)
|
||||||
{
|
{
|
||||||
ContactTagVM tagVM = new ContactTagVM(); ;
|
ContactTagVM tagVM = new ContactTagVM(); ;
|
||||||
var tag = TagList.Find(t => t.Id == tagMapping.ContactTagtId);
|
var tag = TagList.Find(t => t.Id == tagMapping.ContactTagtId);
|
||||||
|
|
||||||
tagVM = tag.ToContactTagVMFromContactTagMaster();
|
tagVM = tag != null ? tag.ToContactTagVMFromContactTagMaster() : new ContactTagVM();
|
||||||
conatctTagVms.Add(tagVM);
|
conatctTagVms.Add(tagVM);
|
||||||
|
|
||||||
|
|
||||||
@ -96,16 +93,124 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
|
|
||||||
|
|
||||||
contactVM = contact.ToContactVMFromContact();
|
contactVM = contact.ToContactVMFromContact();
|
||||||
|
|
||||||
contactVM.ContactEmails = contactEmailVms;
|
contactVM.ContactEmails = contactEmailVms;
|
||||||
contactVM.ContactPhones = contactPhoneVms;
|
contactVM.ContactPhones = contactPhoneVms;
|
||||||
contactVM.Tags = conatctTagVms;
|
contactVM.Tags = conatctTagVms;
|
||||||
|
|
||||||
list.Add(contactVM);
|
list.Add(contactVM);
|
||||||
}
|
}
|
||||||
|
_logger.LogInfo("{count} contacts are fetched by Employee with ID {LoggedInEmployeeId}", list.Count, LoggedInEmployee.Id);
|
||||||
return ApiResponse<object>.SuccessResponse(list, "Contact List !",200);
|
return ApiResponse<object>.SuccessResponse(list, System.String.Format("{0} contacts fetched successfully", list.Count), 200);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<ApiResponse<object>> CreateContact(CreateContactDto createContact)
|
||||||
|
{
|
||||||
|
Guid tenantId = _userHelper.GetTenantId();
|
||||||
|
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||||
|
if (createContact != null)
|
||||||
|
{
|
||||||
|
List<ContactPhone> phones = new List<ContactPhone>();
|
||||||
|
List<ContactEmail> emails = new List<ContactEmail>();
|
||||||
|
List<ContactBucketMapping> contactBucketMappings = new List<ContactBucketMapping>();
|
||||||
|
List<ContactTagMapping> contactTagMappings = new List<ContactTagMapping>();
|
||||||
|
|
||||||
|
Contact? contact = createContact.ToContactFromCreateContactDto(tenantId, LoggedInEmployee.Id);
|
||||||
|
|
||||||
|
_context.Contacts.Add(contact);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
_logger.LogInfo("Contact with ID {ContactId} created by Employee with ID {LoggedInEmployeeId}", contact.Id, LoggedInEmployee.Id);
|
||||||
|
|
||||||
|
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();
|
||||||
|
if (createContact.ContactPhones != null)
|
||||||
|
{
|
||||||
|
|
||||||
|
foreach (var contactPhone in createContact.ContactPhones)
|
||||||
|
{
|
||||||
|
ContactPhone phone = contactPhone.ToContactPhoneFromCreateContactPhoneDto(tenantId, contact.Id);
|
||||||
|
phones.Add(phone);
|
||||||
|
}
|
||||||
|
_context.ContactsPhones.AddRange(phones);
|
||||||
|
_logger.LogInfo("{count} phone number are saved in contact with ID {ContactId} by employee with ID {LoggedEmployeeId}", phones.Count, contact.Id, LoggedInEmployee.Id);
|
||||||
|
}
|
||||||
|
if (createContact.ContactEmails != null)
|
||||||
|
{
|
||||||
|
|
||||||
|
foreach (var contactEmail in createContact.ContactEmails)
|
||||||
|
{
|
||||||
|
ContactEmail email = contactEmail.ToContactEmailFromCreateContactEmailDto(tenantId, contact.Id);
|
||||||
|
emails.Add(email);
|
||||||
|
}
|
||||||
|
_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)
|
||||||
|
{
|
||||||
|
if (buckets.Contains(bucket))
|
||||||
|
{
|
||||||
|
ContactBucketMapping bucketMapping = new ContactBucketMapping
|
||||||
|
{
|
||||||
|
BucketId = bucket,
|
||||||
|
ContactId = contact.Id
|
||||||
|
};
|
||||||
|
contactBucketMappings.Add(bucketMapping);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_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);
|
||||||
|
}
|
||||||
|
if (createContact.TagIds != null)
|
||||||
|
{
|
||||||
|
foreach (var tag in createContact.TagIds)
|
||||||
|
{
|
||||||
|
if (tags.Where(t => t.Id == tag) != null)
|
||||||
|
{
|
||||||
|
ContactTagMapping tagMapping = new ContactTagMapping
|
||||||
|
{
|
||||||
|
ContactTagtId = tag,
|
||||||
|
ContactId = contact.Id
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_context.ContactTagMappings.AddRange(contactTagMappings);
|
||||||
|
}
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
ContactVM contactVM = new ContactVM();
|
||||||
|
List<ContactPhoneVM> phoneVMs = new List<ContactPhoneVM>();
|
||||||
|
foreach (var phone in phones)
|
||||||
|
{
|
||||||
|
ContactPhoneVM phoneVM = phone.ToContactPhoneVMFromContactPhone();
|
||||||
|
phoneVMs.Add(phoneVM);
|
||||||
|
}
|
||||||
|
List<ContactEmailVM> emailVMs = new List<ContactEmailVM>();
|
||||||
|
foreach (var email in emails)
|
||||||
|
{
|
||||||
|
ContactEmailVM emailVM = email.ToContactEmailVMFromContactEmail();
|
||||||
|
emailVMs.Add(emailVM);
|
||||||
|
}
|
||||||
|
List<ContactTagVM> tagVMs = new List<ContactTagVM>();
|
||||||
|
foreach (var contactTagMapping in contactTagMappings)
|
||||||
|
{
|
||||||
|
ContactTagVM tagVM = new ContactTagVM();
|
||||||
|
var tag = tags.Find(t => t.Id == contactTagMapping.ContactTagtId);
|
||||||
|
tagVM = tag != null ? tag.ToContactTagVMFromContactTagMaster() : new ContactTagVM();
|
||||||
|
tagVMs.Add(tagVM);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
contactVM = contact.ToContactVMFromContact();
|
||||||
|
contactVM.ContactPhones = phoneVMs;
|
||||||
|
contactVM.ContactEmails = emailVMs;
|
||||||
|
contactVM.Tags = tagVMs;
|
||||||
|
|
||||||
|
return ApiResponse<object>.SuccessResponse(contactVM, "Contact Created Successfully", 200);
|
||||||
|
}
|
||||||
|
_logger.LogWarning("User send empty payload");
|
||||||
|
return ApiResponse<object>.ErrorResponse("User send empty data", "User send empty data", 400);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
//"DefaultConnectionString": "Server=localhost;port=3306;User ID=root;Password=root;Database=MarcoBMS2"
|
//"DefaultConnectionString": "Server=localhost;port=3306;User ID=root;Password=root;Database=MarcoBMS2"
|
||||||
"DefaultConnectionString": "Server=147.93.98.152;User ID=devuser;Password=AppUser@123$;Database=MarcoBMS10"
|
"DefaultConnectionString": "Server=147.93.98.152;User ID=devuser;Password=AppUser@123$;Database=MarcoBMSDev"
|
||||||
},
|
},
|
||||||
"MongoDB": {
|
"MongoDB": {
|
||||||
"SerilogDatabaseUrl": "mongodb://localhost:27017/DotNetLogs"
|
"SerilogDatabaseUrl": "mongodb://localhost:27017/DotNetLogs"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user