Added an API to create contact and populate related tables as well
This commit is contained in:
parent
e3e19a938a
commit
34f32bdd33
@ -786,5 +786,114 @@ namespace Marco.Pms.Services.Helpers
|
|||||||
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id);
|
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id);
|
||||||
return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400);
|
return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user