diff --git a/Marco.Pms.Model/Dtos/Directory/CreateContactEmailDto.cs b/Marco.Pms.Model/Dtos/Directory/CreateContactEmailDto.cs index 68bb2b2..654890a 100644 --- a/Marco.Pms.Model/Dtos/Directory/CreateContactEmailDto.cs +++ b/Marco.Pms.Model/Dtos/Directory/CreateContactEmailDto.cs @@ -4,6 +4,6 @@ { public string? Label { get; set; } public string? EmailAddress { get; set; } - public Guid? ContactId { get; set; } } } + diff --git a/Marco.Pms.Model/Dtos/Directory/CreateContactPhoneDto.cs b/Marco.Pms.Model/Dtos/Directory/CreateContactPhoneDto.cs index dc97881..a72ac3d 100644 --- a/Marco.Pms.Model/Dtos/Directory/CreateContactPhoneDto.cs +++ b/Marco.Pms.Model/Dtos/Directory/CreateContactPhoneDto.cs @@ -4,6 +4,5 @@ { public string? Label { get; set; } public string? PhoneNumber { get; set; } - public Guid? ContactId { get; set; } } } diff --git a/Marco.Pms.Services/Controllers/DirectoryController.cs b/Marco.Pms.Services/Controllers/DirectoryController.cs index efa76c3..afcb061 100644 --- a/Marco.Pms.Services/Controllers/DirectoryController.cs +++ b/Marco.Pms.Services/Controllers/DirectoryController.cs @@ -51,8 +51,15 @@ namespace Marco.Pms.Services.Controllers _logger.LogError("User sent Invalid Date while marking attendance"); return BadRequest(ApiResponse.ErrorResponse("Invalid data", errors, 400)); } - - return Ok(); + var response = await _directoryHelper.CreateContact(createContact); + if (response.StatusCode == 200) + { + return Ok(response); + } + else + { + return BadRequest(response); + } } diff --git a/Marco.Pms.Services/Helpers/DirectoryHelper.cs b/Marco.Pms.Services/Helpers/DirectoryHelper.cs index 5e11587..b1f6e9c 100644 --- a/Marco.Pms.Services/Helpers/DirectoryHelper.cs +++ b/Marco.Pms.Services/Helpers/DirectoryHelper.cs @@ -1,5 +1,6 @@ using Marco.Pms.DataAccess.Data; using Marco.Pms.Model.Directory; +using Marco.Pms.Model.Dtos.Directory; using Marco.Pms.Model.Mapper; using Marco.Pms.Model.Utilities; using Marco.Pms.Model.ViewModels.Directory; @@ -107,5 +108,114 @@ namespace Marco.Pms.Services.Helpers return ApiResponse.SuccessResponse(list, "Contact List !",200); } + + public async Task> CreateContact(CreateContactDto createContact) + { + Guid tenantId = _userHelper.GetTenantId(); + var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); + if (createContact != null) + { + List phones = new List(); + List emails = new List(); + List contactBucketMappings = new List(); + List contactTagMappings = new List(); + + 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 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 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.SuccessResponse(contactVM, "Contact Created Successfully", 200); + } + _logger.LogWarning("User send empty payload"); + return ApiResponse.ErrorResponse("User send empty data", "User send empty data", 400); + } } } diff --git a/Marco.Pms.Services/appsettings.Development.json b/Marco.Pms.Services/appsettings.Development.json index 91af3c4..1522ad9 100644 --- a/Marco.Pms.Services/appsettings.Development.json +++ b/Marco.Pms.Services/appsettings.Development.json @@ -12,7 +12,7 @@ }, "ConnectionStrings": { //"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": { "SerilogDatabaseUrl": "mongodb://localhost:27017/DotNetLogs"