public async Task Execute(CreateNotificationInputModel input) { // validations await ValidateInput(input); // creating notification var notification = new Notification { Id = new Guid(), Message = input.Message, NextScheduledNotificatinoDateTime = input.DateTime.ToUniversalTime(), IsForEveryOne = input.IsForEveryOne, TargetUserId = input.TargetUserId, DepartmentId = userInfoProvider.CurrentDepartmentId, Repeat = input.Repeat }; notificationRepository.Add(notification); await notificationRepository.SaveChangesAsync(); foreach (var departmentId in input.TargetDepartments ?? new System.Collections.Generic.List <Guid>()) { departmentNotificationRepository.Add(new DepartmentNotification { DepartmentId = departmentId, NotificationId = notification.Id }); } await departmentNotificationRepository.SaveChangesAsync(); }
public async Task <bool> CreateNotification(CreateNotificationInputModel input) { var response = await client.SendAsync(HttpMethod.Post, $"{CreateNotificationUrl}?input={JsonConvert.SerializeObject(input)}"); if (!string.IsNullOrEmpty(response.ErrorMessage)) { notificationService.ShowError(response.ErrorMessage); return(false); } return(true); }
private async Task ValidateInput(CreateNotificationInputModel input) { if (input == null) { throw new ArgumentNullException(nameof(input)); } if (string.IsNullOrEmpty(input.Message)) { throw new ArgumentNullException(nameof(input.Message)); } if ((input.IsForEveryOne && (input.TargetDepartments != null || input.TargetUserId != null)) || !input.IsForEveryOne && input.TargetDepartments != null && input.TargetUserId != null) { throw new InvalidOperationException("Notification target must be set once"); } if (input.Repeat == RepeatInterval.Never && input.DateTime.ToUniversalTime() < DateTime.UtcNow.AddMinutes(-10)) { throw new InvalidOperationException("Invalid notification date"); } if (input.TargetUserId != null) { if (!await userRepository.AnyAsync(u => u.Id == input.TargetUserId)) { throw new BusinessException("Target user not found"); } } else { foreach (var departamentId in input.TargetDepartments ?? new System.Collections.Generic.List <Guid>()) { if (!await departamentRepository.AnyAsync(d => d.Id == departamentId)) { throw new BusinessException("One of target departments not found"); } } } }