protected override async Task CreateLogic(UserData data, UserEntity userToBeUpdated, CrudResult <UserData> result) { userToBeUpdated.UserId = Guid.NewGuid(); userToBeUpdated.SecurityUserAccessRoles = new List <SecurityUserAccessRoleEntity>(); userToBeUpdated.UserCarrierScacs = new List <UserCarrierScacEntity>(); userToBeUpdated.UserShippers = new List <UserShipperEntity>(); var updateAndCreateTask = UpdateAndCreateLogic(data, userToBeUpdated); var identityUserTask = GetIdentityUser(data.Username); await Task.WhenAll(updateAndCreateTask, identityUserTask); var identityUser = identityUserTask.Result; if (identityUser != null && !string.IsNullOrWhiteSpace(identityUser.Email)) { var newUserNotification = new UserNotificationEntity(); newUserNotification.MessageTypeId = MessageTypeConstants.Email; newUserNotification.NotificationValue = identityUser.Email; newUserNotification.User = userToBeUpdated; Context.UserNotifications.Add(newUserNotification); } if (identityUser != null && !string.IsNullOrWhiteSpace(identityUser.PhoneNumber)) { var newUserNotification = new UserNotificationEntity(); newUserNotification.MessageTypeId = MessageTypeConstants.CellPhone; newUserNotification.NotificationValue = identityUser.PhoneNumber; newUserNotification.User = userToBeUpdated; Context.UserNotifications.Add(newUserNotification); } }
public UserNotification AddNotification(NotificationModel notificationModel) { // todo add coherent error handling var notificationType = notificationModel.Type; var template = _dbContext.Set <TemplateEntity>().FirstOrDefault(x => x.EventType == notificationType); if (template != null) { var entity = new UserNotificationEntity() { Body = FillTemplate(notificationModel.Data as JObject, template.Body), Title = FillTemplate(notificationModel.Data as JObject, template.Title), EventType = notificationModel.Type, UserId = notificationModel.UserId }; _dbContext.Set <UserNotificationEntity>().Add(entity); _dbContext.SaveChanges(); return(Map <UserNotificationEntity, UserNotification>(entity)); } //something is wrong ?!? throw new Exception("Unhandled case ..."); }
private async Task UpdateAndCreateLogic(UserData data, UserEntity userToBeUpdated) { _userContext.UserId.NullArgumentCheck(nameof(_userContext.UserId)); var updatingUserEntity = await LoadUserByIdentId(_userContext.UserId.Value); if (!(await CanUpdateUser(updatingUserEntity, userToBeUpdated))) { throw new UnauthorizedAccessException($"{updatingUserEntity.Username} cannot update user {userToBeUpdated.Username}"); } //Map Allowed Security Roles var allowedRoleIds = await FilterToAuthorizedRoleIds(updatingUserEntity, data.SecurityRoleIds); Context.SecurityUserAccessRoles.RemoveRange(userToBeUpdated.SecurityUserAccessRoles); userToBeUpdated.SecurityUserAccessRoles.Clear(); userToBeUpdated.SecurityUserAccessRoles.AddRange(allowedRoleIds.Select(roleId => new SecurityUserAccessRoleEntity() { AccessRoleId = roleId, UserId = userToBeUpdated.UserId })); if (await _securityService.UserHasActionAsync(SecurityActions.Loadshop_Ui_System_Carrier_User_Add_Edit)) { //Map Carriers/Carrier Scacs var carrierScacs = await BuildUserCarrierScacs(userToBeUpdated.UserId, data.CarrierScacs); await GuardCarrier(updatingUserEntity, carrierScacs); Context.UserCarrierScacs.RemoveRange(userToBeUpdated.UserCarrierScacs); userToBeUpdated.UserCarrierScacs.Clear(); userToBeUpdated.UserCarrierScacs.AddRange(carrierScacs); } if (await _securityService.UserHasActionAsync(SecurityActions.Loadshop_Ui_System_Shipper_User_Add_Edit)) { //Map Shippers await GuardCustomer(data.ShipperIds); Context.UserShippers.RemoveRange(userToBeUpdated.UserShippers); userToBeUpdated.UserShippers.Clear(); userToBeUpdated.UserShippers.AddRange(data.ShipperIds.Select(shipperId => new UserShipperEntity() { UserId = userToBeUpdated.UserId, CustomerId = shipperId })); } //Reset Primary Customer if not part of user's security anymore if (userToBeUpdated.PrimaryCustomerId != null && !userToBeUpdated.UserShippers .Select(userShipper => userShipper.CustomerId) .Contains(userToBeUpdated.PrimaryCustomerId.Value)) { userToBeUpdated.PrimaryCustomerId = null; } //Reset Primary Carrier Scac if not part of user's security anymore if (userToBeUpdated.PrimaryScac != null && !data.CarrierScacs.Contains(userToBeUpdated.PrimaryScac)) { userToBeUpdated.PrimaryScac = null; } //Set Focus Entity if not set yet if (userToBeUpdated.PrimaryScac == null && userToBeUpdated.PrimaryCustomerId == null) { if (data.CarrierScacs.Count() > 0) { userToBeUpdated.PrimaryScac = data.CarrierScacs.First(); } else if (userToBeUpdated.UserShippers.Count() > 0) { userToBeUpdated.PrimaryCustomerId = userToBeUpdated.UserShippers.First().CustomerId; } } if (data.UserNotifications != null && data.UserNotifications.Any()) { if (userToBeUpdated.UserNotifications == null) { userToBeUpdated.UserNotifications = new List <UserNotificationEntity>(); } // remove notification user deleted userToBeUpdated.UserNotifications = userToBeUpdated.UserNotifications.Where(x => data.UserNotifications.Any(y => y.UserNotificationId == x.UserNotificationId)).ToList(); // add / update existing notifications foreach (var notification in data.UserNotifications) { var dbNotification = userToBeUpdated.UserNotifications.SingleOrDefault(x => x.UserNotificationId == notification.UserNotificationId); if (dbNotification == null) { dbNotification = new UserNotificationEntity() { MessageTypeId = notification.MessageTypeId, UserId = userToBeUpdated.UserId }; userToBeUpdated.UserNotifications.Add(dbNotification); } if (notification.MessageTypeId == MessageTypeConstants.CellPhone) { dbNotification.MessageTypeId = MessageTypeConstants.CellPhone; } else if (notification.MessageTypeId == MessageTypeConstants.Phone) { dbNotification.MessageTypeId = MessageTypeConstants.Phone; } else if (notification.MessageTypeId == MessageTypeConstants.Email) { dbNotification.MessageTypeId = MessageTypeConstants.Email; } dbNotification.NotificationEnabled = notification.NotificationEnabled; dbNotification.IsDefault = notification.IsDefault; dbNotification.NotificationValue = notification.NotificationValue ?? string.Empty; } } }
public MockDbBuilder WithUserNotification(UserNotificationEntity item) { _userNotifications.Add(item); return(this); }
public async Task <UserProfileData> GetUserProfileAsync(Guid identUserId) { var user = await _context.Users .Include(u => u.PrimaryScacEntity.Carrier) .Include(x => x.UserNotifications) .ThenInclude(x => x.MessageType) .Include(u => u.UserShippers) .ThenInclude(userShipper => userShipper.Customer) .Include(u => u.PrimaryCustomer) .SingleOrDefaultAsync(x => x.IdentUserId == identUserId); if (user == null) { // Throw exception so we can catch it and add the user throw new EntityNotFoundException($"UserProfile not found for id {identUserId}"); } var userProfile = _mapper.Map <UserProfileData>(user); // Add missing notification types the user does not have set yet var messageTypes = await _context.MessageTypes.Where(x => x.MessageTypeId != MessageTypeConstants.Email_SingleCarrierScac).ToListAsync(); foreach (var messageType in messageTypes) { var userMessageyTypeExists = userProfile.UserNotifications.Any(x => x.MessageTypeId.Equals(messageType.MessageTypeId, StringComparison.OrdinalIgnoreCase)); if (userMessageyTypeExists) { continue; } if (messageType.MessageTypeId.Equals(MessageTypeConstants.Email)) { // add a record using the user's identity email as a default var entity = new UserNotificationEntity() { MessageTypeId = MessageTypeConstants.Email, UserId = user.UserId, IsDefault = true, NotificationValue = _userContext.Email, CreateBy = user.Username, CreateDtTm = DateTime.Now, LastChgBy = user.Username, LastChgDtTm = DateTime.Now, NotificationEnabled = true }; _context.UserNotifications.Add(entity); await _context.SaveChangesAsync(); userProfile.UserNotifications.Add(_mapper.Map <UserNotificationData>(entity)); } else { userProfile.UserNotifications.Add(_mapper.Map <UserNotificationData>(messageType)); } } // check if the user has agreed to the latest terms and privacy userProfile.HasAgreedToTerms = await _agreementDocumentService.HasUserAgreedToLatestTermsAndPrivacy(user.UserId); SetSecurityProperties(user, userProfile); userProfile.CarrierVisibilityTypes = _commonService.GetCarrierVisibilityTypes(user.Username, user.PrimaryScacEntity?.CarrierId); return(userProfile); }
public async Task <SaveUserProfileResponse> SaveUserProfileAsync(UserProfileData userProfile, string username) { var response = new SaveUserProfileResponse(); var user = await _context.Users .Include(x => x.UserNotifications) .SingleOrDefaultAsync(x => x.UserId == userProfile.UserId); if (user == null) { throw new EntityNotFoundException($"UserProfile not found for id {userProfile.UserId}"); } var validationErrorMessage = ValidateUserProfile(userProfile); if (!string.IsNullOrWhiteSpace(validationErrorMessage)) { response.ModelState.AddModelError($"{ErrorPrefix}", validationErrorMessage); return(response); } //This should be removed as we are moving away from the Scac Field if (user.PrimaryScac == null) { user.PrimaryScac = userProfile.PrimaryScac; } // allow the commodity to be a nullable user.DefaultCommodity = userProfile.DefaultCommodity; user.IsNotificationsEnabled = userProfile.IsNotificationsEnabled; // remove notification user deleted user.UserNotifications = user.UserNotifications.Where(x => userProfile.UserNotifications.Any(y => y.UserNotificationId == x.UserNotificationId)).ToList(); // add / update existing notifications foreach (var notification in userProfile.UserNotifications) { var dbNotification = user.UserNotifications.SingleOrDefault(x => x.UserNotificationId == notification.UserNotificationId); if (dbNotification == null) { dbNotification = new UserNotificationEntity() { MessageTypeId = notification.MessageTypeId, UserId = user.UserId }; _context.UserNotifications.Add(dbNotification); } if (notification.MessageTypeId == MessageTypeConstants.CellPhone) { dbNotification.MessageTypeId = MessageTypeConstants.CellPhone; } else if (notification.MessageTypeId == MessageTypeConstants.Phone) { dbNotification.MessageTypeId = MessageTypeConstants.Phone; } else if (notification.MessageTypeId == MessageTypeConstants.Email) { dbNotification.MessageTypeId = MessageTypeConstants.Email; } dbNotification.NotificationEnabled = notification.NotificationEnabled; dbNotification.IsDefault = notification.IsDefault; dbNotification.NotificationValue = notification.NotificationValue ?? string.Empty; } await _context.SaveChangesAsync(username); if (user.PrimaryScac != null) { var primaryCarrierScac = _context.CarrierScacs.Find(user.PrimaryScac); userProfile.AvailableCarrierScacs = GetAuthorizedScas(primaryCarrierScac?.CarrierId); } SetSecurityProperties(user, userProfile); response.UserProfile = await GetUserProfileAsync(userProfile.IdentUserId); return(response); }