Added an API to update existing contact
This commit is contained in:
parent
b4505827bb
commit
0809d26517
8
Marco.Pms.Model/Dtos/Directory/ContactTagDto.cs
Normal file
8
Marco.Pms.Model/Dtos/Directory/ContactTagDto.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace Marco.Pms.Model.Dtos.Directory
|
||||
{
|
||||
public class ContactTagDto
|
||||
{
|
||||
public Guid? Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
@ -11,6 +11,6 @@
|
||||
public string? Description { get; set; }
|
||||
public string? Organization { get; set; }
|
||||
public string? Address { get; set; }
|
||||
public List<Guid>? TagIds { get; set; }
|
||||
public List<ContactTagDto>? Tags { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,6 @@
|
||||
public string? Description { get; set; }
|
||||
public string? Organization { get; set; }
|
||||
public string? Address { get; set; }
|
||||
public List<Guid>? TagIds { get; set; }
|
||||
public List<ContactTagDto>? Tags { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
public class UpdateContactEmailDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid? Id { get; set; }
|
||||
public string? Label { get; set; }
|
||||
public string? EmailAddress { get; set; }
|
||||
public Guid? ContactId { get; set; }
|
||||
|
@ -2,7 +2,7 @@
|
||||
{
|
||||
public class UpdateContactPhoneDto
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid? Id { get; set; }
|
||||
public string? Label { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public Guid? ContactId { get; set; }
|
||||
|
@ -68,7 +68,7 @@ namespace Marco.Pms.Model.Mapper
|
||||
{
|
||||
return new ContactPhone
|
||||
{
|
||||
Id = updateContactPhoneDto.Id,
|
||||
Id = updateContactPhoneDto.Id ?? Guid.Empty,
|
||||
Label = updateContactPhoneDto.Label ?? string.Empty,
|
||||
PhoneNumber = updateContactPhoneDto.PhoneNumber ?? string.Empty,
|
||||
ContactId = contactId,
|
||||
@ -101,7 +101,7 @@ namespace Marco.Pms.Model.Mapper
|
||||
{
|
||||
return new ContactEmail
|
||||
{
|
||||
Id = updateContactEmailDto.Id,
|
||||
Id = updateContactEmailDto.Id ?? Guid.Empty,
|
||||
Label = updateContactEmailDto.Label ?? string.Empty,
|
||||
EmailAddress = updateContactEmailDto.EmailAddress ?? string.Empty,
|
||||
ContactId = contactId,
|
||||
|
@ -64,7 +64,23 @@ namespace Marco.Pms.Services.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[HttpPut("{id}")]
|
||||
public async Task<IActionResult> UpdateContact(Guid id, [FromBody] UpdateContactDto updateContact)
|
||||
{
|
||||
var response = await _directoryHelper.UpdateContact(id, updateContact);
|
||||
if (response.StatusCode == 200)
|
||||
{
|
||||
return Ok(response);
|
||||
}
|
||||
else if (response.StatusCode == 404)
|
||||
{
|
||||
return NotFound(response);
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest(response);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -162,17 +162,32 @@ 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);
|
||||
}
|
||||
if (createContact.TagIds != null)
|
||||
if (createContact.Tags != null)
|
||||
{
|
||||
foreach (var tag in createContact.TagIds)
|
||||
foreach (var tag in createContact.Tags)
|
||||
{
|
||||
if (tags.Where(t => t.Id == tag) != null)
|
||||
if (tag.Id != null && tags.Where(t => t.Id == tag.Id) != null)
|
||||
{
|
||||
ContactTagMapping tagMapping = new ContactTagMapping
|
||||
{
|
||||
ContactTagtId = tag,
|
||||
ContactTagtId = tag.Id != null ? tag.Id.Value : Guid.Empty,
|
||||
ContactId = contact.Id
|
||||
};
|
||||
contactTagMappings.Add(tagMapping);
|
||||
}
|
||||
else if (tag.Id == null || tags.Where(t => t.Name == tag.Name) == null)
|
||||
{
|
||||
var newtag = new ContactTagMaster
|
||||
{
|
||||
Name = tag.Name
|
||||
};
|
||||
_context.ContactTagMasters.Add(newtag);
|
||||
ContactTagMapping tagMapping = new ContactTagMapping
|
||||
{
|
||||
ContactTagtId = newtag.Id,
|
||||
ContactId = contact.Id
|
||||
};
|
||||
contactTagMappings.Add(tagMapping);
|
||||
}
|
||||
}
|
||||
_context.ContactTagMappings.AddRange(contactTagMappings);
|
||||
@ -209,8 +224,214 @@ namespace Marco.Pms.Services.Helpers
|
||||
|
||||
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);
|
||||
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id);
|
||||
return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400);
|
||||
}
|
||||
|
||||
public async Task<ApiResponse<object>> UpdateContact(Guid id, UpdateContactDto updateContact)
|
||||
{
|
||||
Guid tenantId = _userHelper.GetTenantId();
|
||||
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||
if (updateContact != null)
|
||||
{
|
||||
if (updateContact.Id != 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);
|
||||
}
|
||||
Contact? contact = await _context.Contacts.FirstOrDefaultAsync(c => c.Id == id && c.TenantId == tenantId);
|
||||
if (contact == null)
|
||||
{
|
||||
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} tries to update contact with ID {ContactId} is not found in database", LoggedInEmployee.Id);
|
||||
return ApiResponse<object>.ErrorResponse("Contact not found", "Contact not found", 404);
|
||||
}
|
||||
|
||||
var newContact = updateContact.ToContactFromUpdateContactDto(tenantId);
|
||||
|
||||
List<ContactPhone> phones = await _context.ContactsPhones.AsNoTracking().Where(p => p.ContactId == contact.Id).ToListAsync();
|
||||
var phoneIds = phones.Select(p => p.Id).ToList();
|
||||
List<ContactEmail> emails = await _context.ContactsEmails.AsNoTracking().Where(p => p.ContactId == contact.Id).ToListAsync();
|
||||
var emailIds = emails.Select(p => p.Id).ToList();
|
||||
|
||||
List<ContactBucketMapping> contactBuckets = await _context.ContactBucketMappings.AsNoTracking().Where(m => m.ContactId == contact.Id).ToListAsync();
|
||||
var bucketIds = contactBuckets.Select(b => b.BucketId).Distinct().ToList();
|
||||
|
||||
List<ContactTagMapping> contactTags = await _context.ContactTagMappings.AsNoTracking().Where(m => m.ContactId == contact.Id).ToListAsync();
|
||||
var tagIds = contactTags.Select(t => t.ContactTagtId).Distinct().ToList();
|
||||
|
||||
List<ContactTagMaster> tags = await _context.ContactTagMasters.Where(t => tagIds.Contains(t.Id)).ToListAsync();
|
||||
|
||||
if (updateContact.ContactPhones != null)
|
||||
{
|
||||
var updatedPhoneIds = updateContact.ContactPhones.Select(p => p.Id).Distinct().ToList();
|
||||
foreach (var phoneDto in updateContact.ContactPhones)
|
||||
{
|
||||
var phone = phoneDto.ToContactPhoneFromUpdateContactPhoneDto(tenantId, contact.Id);
|
||||
if (phoneDto.Id != null && phoneDto.Id != Guid.Empty && phoneIds.Contains(phone.Id))
|
||||
{
|
||||
_context.ContactsPhones.Update(phone);
|
||||
}
|
||||
else
|
||||
{
|
||||
_context.ContactsPhones.Add(phone);
|
||||
}
|
||||
}
|
||||
foreach (var phone in phones)
|
||||
{
|
||||
|
||||
if (!updatedPhoneIds.Contains(phone.Id))
|
||||
{
|
||||
_context.ContactsPhones.Remove(phone);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (phones != null)
|
||||
{
|
||||
_context.ContactsPhones.RemoveRange(phones);
|
||||
}
|
||||
|
||||
if (updateContact.ContactEmails != null)
|
||||
{
|
||||
var updateEmailIds = updateContact.ContactEmails.Select(p => p.Id).Distinct().ToList();
|
||||
|
||||
foreach (var emailDto in updateContact.ContactEmails)
|
||||
{
|
||||
var email = emailDto.ToContactEmailFromUpdateContactEmailDto(tenantId, contact.Id);
|
||||
if (emailDto.Id != null && emailDto.Id != Guid.Empty && emailIds.Contains(email.Id))
|
||||
{
|
||||
_context.ContactsEmails.Update(email);
|
||||
}
|
||||
else
|
||||
{
|
||||
_context.ContactsEmails.Add(email);
|
||||
}
|
||||
}
|
||||
foreach (var email in emails)
|
||||
{
|
||||
|
||||
if (!updateEmailIds.Contains(email.Id))
|
||||
{
|
||||
_context.ContactsEmails.Remove(email);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (emails != null)
|
||||
{
|
||||
_context.ContactsEmails.RemoveRange(emails);
|
||||
}
|
||||
|
||||
if (updateContact.BucketIds != null)
|
||||
{
|
||||
foreach (var bucketId in updateContact.BucketIds)
|
||||
{
|
||||
if (!bucketIds.Contains(bucketId))
|
||||
{
|
||||
_context.ContactBucketMappings.Add(new ContactBucketMapping
|
||||
{
|
||||
BucketId = bucketId,
|
||||
ContactId = contact.Id
|
||||
});
|
||||
}
|
||||
}
|
||||
foreach (var bucketMapping in contactBuckets)
|
||||
{
|
||||
if (!updateContact.BucketIds.Contains(bucketMapping.BucketId))
|
||||
{
|
||||
_context.ContactBucketMappings.Remove(bucketMapping);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (contactBuckets != null)
|
||||
{
|
||||
_context.ContactBucketMappings.RemoveRange(contactBuckets);
|
||||
}
|
||||
|
||||
if (updateContact.Tags != null)
|
||||
{
|
||||
var updatedTagIds = updateContact.Tags.Select(t => t.Id).Distinct().ToList();
|
||||
foreach (var tag in updateContact.Tags)
|
||||
{
|
||||
if (tag.Id != null && !tagIds.Contains(tag.Id.Value))
|
||||
{
|
||||
_context.ContactTagMappings.Add(new ContactTagMapping
|
||||
{
|
||||
ContactId = contact.Id,
|
||||
ContactTagtId = tag.Id.Value
|
||||
});
|
||||
}
|
||||
else if (tag.Id == null || tag.Id == Guid.Empty)
|
||||
{
|
||||
ContactTagMaster contactTag = new ContactTagMaster
|
||||
{
|
||||
Name = tag.Name,
|
||||
Description = ""
|
||||
};
|
||||
_context.ContactTagMasters.Add(contactTag);
|
||||
|
||||
_context.ContactTagMappings.Add(new ContactTagMapping
|
||||
{
|
||||
ContactId = contact.Id,
|
||||
ContactTagtId = contactTag.Id
|
||||
});
|
||||
}
|
||||
}
|
||||
foreach (var contactTag in contactTags)
|
||||
{
|
||||
if (!updatedTagIds.Contains(contactTag.Id))
|
||||
{
|
||||
_context.ContactTagMappings.Remove(contactTag);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (contactTags != null)
|
||||
{
|
||||
_context.ContactTagMappings.RemoveRange(contactTags);
|
||||
}
|
||||
_context.DirectoryUpdateLogs.Add(new DirectoryUpdateLog
|
||||
{
|
||||
RefereanceId = contact.Id,
|
||||
UpdatedById = LoggedInEmployee.Id,
|
||||
UpdateAt = DateTime.UtcNow
|
||||
});
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
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();
|
||||
|
||||
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 contactTags)
|
||||
{
|
||||
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;
|
||||
_logger.LogInfo("Conatct {ContactId} has been updated by employee {EmployeeId}", contact.Id, LoggedInEmployee.Id);
|
||||
return ApiResponse<object>.SuccessResponse(contactVM, "Contact Updated Successfully", 200);
|
||||
}
|
||||
_logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id);
|
||||
return ApiResponse<object>.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user