public async Task NotificationRule_Save_DoesSave_WRuleTypeCheckBox() { //Arrange Random rnd = new Random(); NotificationRule rule = new NotificationRule(); rule.Data = Guid.NewGuid().ToString(); rule.Subject = Guid.NewGuid().ToString(); rule.Text = Guid.NewGuid().ToString(); rule.CheckListId = rnd.Next(1, 255); rule.DataItemId = rnd.Next(1, 255); rule.AttachReport = rnd.NextDouble() >= 0.5; rule.RuleType = RuleType.CheckBox; // Act await rule.Create(DbContext).ConfigureAwait(false); NotificationRule dbNotificationRule = DbContext.Rules.AsNoTracking().First(); List <NotificationRule> notificationRuleList = DbContext.Rules.AsNoTracking().ToList(); //Assert Assert.NotNull(dbNotificationRule); Assert.AreEqual(1, notificationRuleList.Count); Assert.AreEqual(rule.Data, dbNotificationRule.Data); Assert.AreEqual(rule.Subject, dbNotificationRule.Subject); Assert.AreEqual(rule.Text, dbNotificationRule.Text); Assert.AreEqual(rule.CheckListId, dbNotificationRule.CheckListId); Assert.AreEqual(rule.DataItemId, dbNotificationRule.DataItemId); Assert.AreEqual(rule.AttachReport, dbNotificationRule.AttachReport); Assert.AreEqual(rule.RuleType, dbNotificationRule.RuleType); }
public async Task Recipient_Save_DoesSave() { //Arrange Random rnd = new Random(); NotificationRule rule = new NotificationRule(); rule.Data = Guid.NewGuid().ToString(); rule.Subject = Guid.NewGuid().ToString(); rule.Text = Guid.NewGuid().ToString(); rule.CheckListId = rnd.Next(1, 255); rule.DataItemId = rnd.Next(1, 255); rule.AttachReport = rnd.NextDouble() >= 0.5; rule.RuleType = RuleType.Number; await rule.Create(DbContext).ConfigureAwait(false); NotificationRule ruleForRecipient = DbContext.Rules.AsNoTracking().First(); Recipient recipient = new Recipient(); recipient.Email = Guid.NewGuid().ToString(); recipient.NotificationRuleId = ruleForRecipient.Id; //Act await recipient.Create(DbContext).ConfigureAwait(false); Recipient dbRecipient = DbContext.Recipients.AsNoTracking().First(); List <Recipient> recipientList = DbContext.Recipients.AsNoTracking().ToList(); //Assert Assert.NotNull(dbRecipient); Assert.AreEqual(1, recipientList.Count); Assert.AreEqual(recipient.Email, dbRecipient.Email); Assert.AreEqual(recipient.NotificationRuleId, dbRecipient.NotificationRuleId); }
public async Task NotificationRule_Update_DoesUpdate_WRuleTypeNumber() { //Arrange Random rnd = new Random(); NotificationRule rule = new NotificationRule(); rule.Data = Guid.NewGuid().ToString(); rule.Subject = Guid.NewGuid().ToString(); rule.Text = Guid.NewGuid().ToString(); rule.CheckListId = rnd.Next(1, 255); rule.DataItemId = rnd.Next(1, 255); rule.AttachReport = rnd.NextDouble() >= 0.5; rule.RuleType = RuleType.Select; await rule.Create(DbContext).ConfigureAwait(false); string newData = Guid.NewGuid().ToString(); string newSubject = Guid.NewGuid().ToString(); string newText = Guid.NewGuid().ToString(); int newChecklistId = rnd.Next(1, 255); int newDataItemId = rnd.Next(1, 255); bool newAttachReport = rnd.NextDouble() >= 0.5; // Act rule.Data = newData; rule.Subject = newSubject; rule.Text = newText; rule.CheckListId = newChecklistId; rule.DataItemId = newDataItemId; rule.AttachReport = newAttachReport; rule.RuleType = RuleType.Number; await rule.Update(DbContext).ConfigureAwait(false); NotificationRule dbNotificationRule = DbContext.Rules.AsNoTracking().First(); List <NotificationRule> notificationRuleList = DbContext.Rules.AsNoTracking().ToList(); List <NotificationRuleVersion> notificationRuleVersionList = DbContext.RuleVersions.AsNoTracking().ToList(); //Assert Assert.NotNull(dbNotificationRule); Assert.AreEqual(1, notificationRuleList.Count); Assert.AreEqual(2, notificationRuleVersionList.Count); Assert.AreEqual(newData, dbNotificationRule.Data); Assert.AreEqual(newSubject, dbNotificationRule.Subject); Assert.AreEqual(newText, dbNotificationRule.Text); Assert.AreEqual(newChecklistId, dbNotificationRule.CheckListId); Assert.AreEqual(newDataItemId, dbNotificationRule.DataItemId); Assert.AreEqual(newAttachReport, dbNotificationRule.AttachReport); Assert.AreEqual(RuleType.Number, dbNotificationRule.RuleType); }
public async Task NotificationRule_Delete_DoesDelete_WRuleTypeNumber() { //Arrange Random rnd = new Random(); NotificationRule rule = new NotificationRule(); rule.Data = Guid.NewGuid().ToString(); rule.Subject = Guid.NewGuid().ToString(); rule.Text = Guid.NewGuid().ToString(); rule.CheckListId = rnd.Next(1, 255); rule.DataItemId = rnd.Next(1, 255); rule.AttachReport = rnd.NextDouble() >= 0.5; rule.RuleType = RuleType.Number; await rule.Create(DbContext).ConfigureAwait(false); // Act await rule.Delete(DbContext).ConfigureAwait(false); NotificationRule dbNotificationRule = DbContext.Rules.AsNoTracking().First(); List <NotificationRule> notificationRuleList = DbContext.Rules.AsNoTracking().ToList(); List <NotificationRuleVersion> notificationRuleVersionList = DbContext.RuleVersions.AsNoTracking().ToList(); //Assert Assert.NotNull(dbNotificationRule); Assert.AreEqual(1, notificationRuleList.Count); Assert.AreEqual(2, notificationRuleVersionList.Count); Assert.AreEqual(rule.Data, dbNotificationRule.Data); Assert.AreEqual(rule.Subject, dbNotificationRule.Subject); Assert.AreEqual(rule.Text, dbNotificationRule.Text); Assert.AreEqual(rule.CheckListId, dbNotificationRule.CheckListId); Assert.AreEqual(rule.DataItemId, dbNotificationRule.DataItemId); Assert.AreEqual(rule.AttachReport, dbNotificationRule.AttachReport); Assert.AreEqual(rule.RuleType, dbNotificationRule.RuleType); Assert.AreEqual(Constants.WorkflowStates.Removed, dbNotificationRule.WorkflowState); }
public async Task <OperationResult> Create(NotificationRuleModel ruleModel) { using (var transaction = await _dbContext.Database.BeginTransactionAsync()) { try { var notificationRule = new NotificationRule() { Subject = ruleModel.Subject, Text = ruleModel.Text, AttachReport = ruleModel.AttachReport, AttachLink = ruleModel.AttachLink, IncludeValue = ruleModel.IncludeValue, DataItemId = ruleModel.DataItemId, CheckListId = ruleModel.CheckListId, RuleType = ruleModel.RuleType, CreatedByUserId = UserId, UpdatedByUserId = UserId, }; if (ruleModel.Data != null) { notificationRule.Data = ruleModel.Data?.ToString(); } await notificationRule.Create(_dbContext); foreach (var recipientModel in ruleModel.Recipients) { var recipient = new Recipient() { CreatedByUserId = UserId, UpdatedByUserId = UserId, Email = recipientModel.Email, NotificationRuleId = notificationRule.Id, }; await recipient.Create(_dbContext); } var deviceUsersGroupedIds = ruleModel.DeviceUsers .Where(x => x.Id != null) .GroupBy(x => x.Id) .Select(x => x.Key) .ToList(); foreach (var deviceUserId in deviceUsersGroupedIds) { if (deviceUserId != null) { var deviceUser = new DeviceUser() { CreatedByUserId = UserId, UpdatedByUserId = UserId, NotificationRuleId = notificationRule.Id, DeviceUserId = (int)deviceUserId, }; await deviceUser.Create(_dbContext); } } transaction.Commit(); return(new OperationResult( true, _localizationService.GetString("NotificationRuleCreatedSuccessfully"))); } catch (Exception e) { transaction.Rollback(); _logger.LogError(e.Message); return(new OperationResult( false, _localizationService.GetString("ErrorWhileCreatingNotificationRule"))); } } }