public static bool UpdateUsersToPreparers(Dictionary <string, string> userToPreparer, int year) { using (var db = new WorktripEntities()) { foreach (var kvPair in userToPreparer) { var link = db.UserToPreparers.FirstOrDefault(up => up.UserId == kvPair.Key && up.Year == year); if (link == null) { var checkUserFields = UserInfoViewModel.CheckUserFields(kvPair.Key); link = new UserToPreparer { UserId = kvPair.Key, Year = year, StatusId = checkUserFields ? 3 : 2 }; db.UserToPreparers.Add(link); } link.PreparerId = kvPair.Value; db.SaveChanges(); } return(true); } }
public static UserMiscTaxInfo GetOrCreateUserInfo(WorktripEntities dbContext, string userId, int taxYear, string promoCode = null, bool notifyAdminsIfExistingCustomer = true) { var allTaxInfos = dbContext.UserMiscTaxInfoes.Where(i => i.UserId == userId).ToList(); var taxInfo = allTaxInfos.FirstOrDefault(i => i.TaxYear == taxYear); bool createdNewTaxInfo = false; if (taxInfo == null)//New Signup { taxInfo = new UserMiscTaxInfo() { UserId = userId, TaxYear = taxYear }; dbContext.UserMiscTaxInfoes.Add(taxInfo); dbContext.SaveChanges(); createdNewTaxInfo = true; } var upLink = dbContext.UserToPreparers.FirstOrDefault(up => up.Year == taxYear && up.UserId == userId); var newUser = dbContext.UserToPreparers.FirstOrDefault(up => up.UserId == userId); if (upLink == null) { upLink = new UserToPreparer() { UserId = userId, StatusId = 1, Year = taxYear, PromoCode = promoCode }; if (newUser == null) { upLink.ColorCode = "#fc58a1"; //New Signup } else { upLink.ColorCode = "#58c4fc"; //Existing Signup } dbContext.UserToPreparers.Add(upLink); dbContext.SaveChanges(); } // Notify admins if this is an existing user (from previous year), and they have not been assigned yet for this year if (notifyAdminsIfExistingCustomer && allTaxInfos.Count > 0 && createdNewTaxInfo && upLink.PreparerId == null) { var smsService = new SmsService(); var preparers = dbContext.Users.Include(u => u.Roles).Where(u => u.Roles.Any(r => r.Name == "Preparer")).ToList(); var user = dbContext.Users.FirstOrDefault(u => u.Id == userId); foreach (var p in preparers) { smsService.SendAsync(new Microsoft.AspNet.Identity.IdentityMessage { Destination = p.PhoneNumber, Body = String.Format("Existing customer {0} ({1}) has began filling out their {2} tax info", user.FirstName + " " + user.LastName, user.PhoneNumber, taxYear) }); } } return(taxInfo); }