public JsonResult Create(ContactModel model) { if (!HasContextRole(new string[] { "CRO", "CEO", "DBD", "AVP" })) { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "Unauthorized." })); } if (ModelState.IsValid) { if (model.ReferralSources == null || model.ReferralSources.Count() <= 0) { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "The contact must be assigned to one or more referral source." })); } using (var transaction = _dataContext.Database.BeginTransaction()) { if (_dataContext.Contacts.Where(p => p.Email == model.Email && p.Deleted == false).Count() <= 0) { Contact entity = new Contact() { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, Phone = model.Phone, Mobile = model.Mobile }; _dataContext.Contacts.Add(entity); _dataContext.SaveChanges(); var contactAttribute = new ContactAttribute() { CategoryTypeId = model.CategoryTypeId, Contact = entity }; contactAttribute.Location = model.Location; contactAttribute.Note = model.Note; contactAttribute.ContactRoleTypeId = _dataContext.ContactRoleTypes.Where(p => p.Name == model.ContactRoleTypeName && p.Deleted == false).Single().ContactRoleTypeId; if (model.ContactRoleTypeName == "Physician" || model.ContactRoleTypeName == "Nurse Practitioner") { if (model.SpecialityTypeId == null || model.SpecialityTypeId == 0) { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "Please select the speciality for the contact role." })); } contactAttribute.SpecialityTypeId = model.SpecialityTypeId; } else { contactAttribute.SpecialityTypeId = null; } _dataContext.ContactAttributes.Add(contactAttribute); _dataContext.SaveChanges(); if (model.ReferralSources != null) { foreach (var item in model.ReferralSources) { ContactReferralSource contactReferralSource = new ContactReferralSource() { Contact = entity, ReferralSourceId = item.ReferralSourceId }; _dataContext.ContactReferralSources.Add(contactReferralSource); } } _dataContext.SaveChanges(); ApprovalProcess(entity.ContactId); _dataContext.SaveChanges(); transaction.Commit(); return(Json(new { Status = Constant.RESPONSE_OK, Description = "Contact created successfully." })); } else { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "The contact with the same email already exists. Please enter a different email." })); } } } return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "One or more fields failed validation." })); }
public JsonResult Edit(long?id, ContactModel model) { if (!HasContextRole(new string[] { "CRO" })) { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "Unauthorized." })); } if (id == null || id == 0) { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "Invalid ID Specification." })); } var query = (from c in _dataContext.Contacts.Where(p => p.Deleted == false).Except(from c in _dataContext.Users select c.Contact) select c); query = query.Where(p => p.IsApproved == true); var query2 = (from c in query join d in _dataContext.ContactReferralSources on c.ContactId equals d.ContactId where d.Deleted == false select new { Contact = c, ReferralSource = d.ReferralSource }); var query3 = (from c in _dataContext.CHGSiteReferralSources join d in query2 on c.ReferralSourceId equals d.ReferralSource.ReferralSourceId where c.Deleted == false && c.Deleted == false select new { ReferralSource = d, CHGSite = c.CHGSite, Contact = d.Contact }).ToList(); var results = (from c in query3 join d in _dataContext.GetUserSites(_dataContext.UserId.Value) on c.CHGSite.CHGSiteId equals d.CHGSiteId where c.Contact.ContactId == id.Value select c.Contact).ToList(); if (results.Count() <= 0) { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "Unauthorized." })); } if (model.ReferralSources == null || model.ReferralSources.Count() <= 0) { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "The contact must be assigned to one or more referral source." })); } if (ModelState.IsValid) { using (var transaction = _dataContext.Database.BeginTransaction()) { if (_dataContext.Contacts.Where(p => p.Email == model.Email && p.ContactId != id && p.Deleted == false).Count() <= 0) { var entity = _dataContext.Contacts.Where(p => p.ContactId == id && p.Deleted == false).SingleOrDefault(); if (entity == null) { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "Contact not found. Invalid contact." })); } entity.FirstName = model.FirstName; entity.LastName = model.LastName; entity.Email = model.Email; entity.Mobile = model.Mobile; entity.Phone = model.Phone; _dataContext.SaveChanges(); var contactAttribute = _dataContext.ContactAttributes.Include("CategoryType").Where(p => p.ContactId == id.Value && p.Deleted == false).SingleOrDefault(); if (contactAttribute == null) { contactAttribute = new ContactAttribute(); contactAttribute.ContactId = entity.ContactId; contactAttribute.CategoryTypeId = model.CategoryTypeId; _dataContext.ContactAttributes.Add(contactAttribute); } contactAttribute.Location = model.Location; contactAttribute.Note = model.Note; contactAttribute.ContactRoleTypeId = _dataContext.ContactRoleTypes.Where(p => p.Name == model.ContactRoleTypeName && p.Deleted == false).Single().ContactRoleTypeId; contactAttribute.CategoryTypeId = model.CategoryTypeId; if (model.ContactRoleTypeName == "Physician" || model.ContactRoleTypeName == "Nurse Practitioner") { if (model.SpecialityTypeId == null || model.SpecialityTypeId == 0) { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "Please select the speciality for the contact role." })); } contactAttribute.SpecialityTypeId = model.SpecialityTypeId; } else { contactAttribute.SpecialityTypeId = null; } _dataContext.SaveChanges(); _dataContext.ContactReferralSources.RemoveRange(_dataContext.ContactReferralSources.Where(p => p.ContactId == id.Value)); _dataContext.SaveChanges(); if (model.ReferralSources != null) { foreach (var item in model.ReferralSources) { ContactReferralSource contactReferralSource = new ContactReferralSource() { Contact = entity, ReferralSourceId = item.ReferralSourceId }; _dataContext.ContactReferralSources.Add(contactReferralSource); } } _dataContext.SaveChanges(); transaction.Commit(); return(Json(new { Status = Constant.RESPONSE_OK, Description = "Contact saved successfully." })); } else { return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "The contact with the same email already exists. Please enter a different email." })); } } } return(Json(new { Status = Constant.RESPONSE_ERROR, Description = "One or more fields failed validation." })); }