diff --git a/Marco.Pms.Model/Dtos/Directory/CreateContactDto.cs b/Marco.Pms.Model/Dtos/Directory/CreateContactDto.cs index 6e962a4..cadd7e2 100644 --- a/Marco.Pms.Model/Dtos/Directory/CreateContactDto.cs +++ b/Marco.Pms.Model/Dtos/Directory/CreateContactDto.cs @@ -11,6 +11,6 @@ public string? Description { get; set; } public string? Organization { get; set; } public string? Address { get; set; } - public List? TagIds { get; set; } + public List? Tags { get; set; } } } diff --git a/Marco.Pms.Model/Dtos/Directory/UpdateContactDto.cs b/Marco.Pms.Model/Dtos/Directory/UpdateContactDto.cs index 7c93cce..ccf5345 100644 --- a/Marco.Pms.Model/Dtos/Directory/UpdateContactDto.cs +++ b/Marco.Pms.Model/Dtos/Directory/UpdateContactDto.cs @@ -12,6 +12,6 @@ public string? Description { get; set; } public string? Organization { get; set; } public string? Address { get; set; } - public List? TagIds { get; set; } + public List? Tags { get; set; } } } diff --git a/Marco.Pms.Model/Dtos/Directory/UpdateContactEmailDto.cs b/Marco.Pms.Model/Dtos/Directory/UpdateContactEmailDto.cs index 8ece036..186f5b3 100644 --- a/Marco.Pms.Model/Dtos/Directory/UpdateContactEmailDto.cs +++ b/Marco.Pms.Model/Dtos/Directory/UpdateContactEmailDto.cs @@ -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; } diff --git a/Marco.Pms.Model/Dtos/Directory/UpdateContactPhoneDto.cs b/Marco.Pms.Model/Dtos/Directory/UpdateContactPhoneDto.cs index 1ddfb14..ff0ec23 100644 --- a/Marco.Pms.Model/Dtos/Directory/UpdateContactPhoneDto.cs +++ b/Marco.Pms.Model/Dtos/Directory/UpdateContactPhoneDto.cs @@ -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; } diff --git a/Marco.Pms.Model/Mapper/DirectoryMapper.cs b/Marco.Pms.Model/Mapper/DirectoryMapper.cs index 82e703f..9b5c835 100644 --- a/Marco.Pms.Model/Mapper/DirectoryMapper.cs +++ b/Marco.Pms.Model/Mapper/DirectoryMapper.cs @@ -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, diff --git a/Marco.Pms.Services/Controllers/DirectoryController.cs b/Marco.Pms.Services/Controllers/DirectoryController.cs index b5ac7e5..fe82bce 100644 --- a/Marco.Pms.Services/Controllers/DirectoryController.cs +++ b/Marco.Pms.Services/Controllers/DirectoryController.cs @@ -64,7 +64,23 @@ namespace Marco.Pms.Services.Controllers } } - + [HttpPut("{id}")] + public async Task 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); + } + } } diff --git a/Marco.Pms.Services/Helpers/DirectoryHelper.cs b/Marco.Pms.Services/Helpers/DirectoryHelper.cs index 7fa52eb..fb68e67 100644 --- a/Marco.Pms.Services/Helpers/DirectoryHelper.cs +++ b/Marco.Pms.Services/Helpers/DirectoryHelper.cs @@ -1501,17 +1501,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); @@ -1548,8 +1563,214 @@ namespace Marco.Pms.Services.Helpers return ApiResponse.SuccessResponse(contactVM, "Contact Created Successfully", 200); } - _logger.LogWarning("User send empty payload"); - return ApiResponse.ErrorResponse("User send empty data", "User send empty data", 400); + _logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id); + return ApiResponse.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400); + } + + public async Task> 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.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.ErrorResponse("Contact not found", "Contact not found", 404); + } + + var newContact = updateContact.ToContactFromUpdateContactDto(tenantId); + + List phones = await _context.ContactsPhones.AsNoTracking().Where(p => p.ContactId == contact.Id).ToListAsync(); + var phoneIds = phones.Select(p => p.Id).ToList(); + List emails = await _context.ContactsEmails.AsNoTracking().Where(p => p.ContactId == contact.Id).ToListAsync(); + var emailIds = emails.Select(p => p.Id).ToList(); + + List contactBuckets = await _context.ContactBucketMappings.AsNoTracking().Where(m => m.ContactId == contact.Id).ToListAsync(); + var bucketIds = contactBuckets.Select(b => b.BucketId).Distinct().ToList(); + + List contactTags = await _context.ContactTagMappings.AsNoTracking().Where(m => m.ContactId == contact.Id).ToListAsync(); + var tagIds = contactTags.Select(t => t.ContactTagtId).Distinct().ToList(); + + List 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 phoneVMs = new List(); + foreach (var phone in phones) + { + ContactPhoneVM phoneVM = phone.ToContactPhoneVMFromContactPhone(); + phoneVMs.Add(phoneVM); + } + List emailVMs = new List(); + foreach (var email in emails) + { + ContactEmailVM emailVM = email.ToContactEmailVMFromContactEmail(); + emailVMs.Add(emailVM); + } + List tagVMs = new List(); + 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.SuccessResponse(contactVM, "Contact Updated Successfully", 200); + } + _logger.LogWarning("Employee with ID {LoggedInEmployeeId} sended empty payload", LoggedInEmployee.Id); + return ApiResponse.ErrorResponse("User Send empty Payload", "User Send empty Payload", 400); } } }