public async Task <Result> Send(DataWithCompanyInfo messageData, NotificationTypes notificationType) { return(await GetRecipients(notificationType) .Bind(GetNotificationOptions) .Bind(BuildSettings) .Tap(AddNotifications)); async Task <Result <Dictionary <int, string> > > GetRecipients(NotificationTypes notificationType) { var roles = await _context.AdministratorRoles.ToListAsync(); var roleIds = roles.Where(r => r.NotificationTypes?.Contains(notificationType) ?? false) .Select(r => r.Id) .ToList(); var recipients = new Dictionary <int, string>(); foreach (var roleId in roleIds) { var addedRecipients = await _context.Administrators.Where(a => a.AdministratorRoleIds.Contains(roleId)) .ToDictionaryAsync(a => a.Id, a => a.Email); recipients = recipients.Union(addedRecipients) .ToDictionary(r => r.Key, r => r.Value); } return(recipients); } async Task <Result <List <RecipientWithNotificationOptions> > > GetNotificationOptions(Dictionary <int, string> recipients) => await _notificationOptionsService.GetNotificationOptions(recipients, notificationType);
public async Task AddAgentNotification(SlimAgentContext agent, DataWithCompanyInfo messageData, NotificationTypes notificationType, Dictionary <ProtocolTypes, object> sendingSettings) { var notification = new Notification { Receiver = ReceiverTypes.AgentApp, UserId = agent.AgentId, AgencyId = agent.AgencyId, Message = JsonDocument.Parse(JsonSerializer.SerializeToUtf8Bytes((object)messageData, new JsonSerializerOptions(JsonSerializerDefaults.Web))), Type = notificationType, SendingSettings = sendingSettings }; await SaveAndSend(notification, messageData); }
public Task <Result> Send(string templateId, string recipientAddress, DataWithCompanyInfo messageData) { return(Validate() .Bind(SendEmail)); Result Validate() { return(GenericValidator <string> .Validate(v => { v.RuleFor(e => e).NotEmpty().EmailAddress(); }, recipientAddress)); } Task <Result> SendEmail() => Send(templateId, new[] { recipientAddress }, messageData); }
public async Task AddAdminNotifications(DataWithCompanyInfo messageData, NotificationTypes notificationType, List <RecipientWithSendingSettings> recipientsWithSendingSettings) { foreach (var recipient in recipientsWithSendingSettings) { var notification = new Notification { Receiver = ReceiverTypes.AdminPanel, UserId = recipient.RecipientId, AgencyId = null, Message = JsonDocument.Parse(JsonSerializer.SerializeToUtf8Bytes((object)messageData, new JsonSerializerOptions(JsonSerializerDefaults.Web))), Type = notificationType, SendingSettings = recipient.SendingSettings }; await SaveAndSend(notification, messageData); } }
private async Task Send(Notification notification, int notificationId, DataWithCompanyInfo messageData) { var tasks = new List <Task>(); foreach (var(protocol, settings) in notification.SendingSettings) { var task = protocol switch { ProtocolTypes.Email when settings is EmailSettings emailSettings && messageData is not null => SendEmail(emailSettings, messageData), ProtocolTypes.WebSocket when settings is WebSocketSettings webSocketSettings => notification.Receiver switch { ReceiverTypes.AgentApp => SendMessageToAgent(notification.UserId, notification.AgencyId, notificationId, notification.Type, notification.Message), ReceiverTypes.AdminPanel => SendMessageToAdmin(notification.UserId, notificationId, notification.Type, notification.Message), _ => throw new ArgumentException($"Unsupported receiver '{notification.Receiver}' for notification") },
public async Task <Result> Send(ApiCaller apiCaller, DataWithCompanyInfo messageData, NotificationTypes notificationType, List <string> emails) { if (apiCaller.Type == ApiCallerTypes.Agent) { var agent = await _agentContextService.GetAgent(); return(await Send(new SlimAgentContext(agent.AgentId, agent.AgencyId), messageData, notificationType, emails)); } else if (apiCaller.Type == ApiCallerTypes.Admin) { return(await Send(new SlimAdminContext(apiCaller.Id), messageData, notificationType, emails)); } else if (apiCaller.Type == ApiCallerTypes.PropertyOwner) { return(await _notificationOptionsService.GetNotificationOptions(NoUserId, apiCaller.Type, NoAgencyId, notificationType) .Map(notificationOptions => BuildSettings(notificationOptions, emails)) .Tap(sendingSettings => _internalNotificationService.AddPropertyOwnerNotification(messageData, notificationType, sendingSettings))); } else { return(Result.Success()); } }
public async Task <Result> Send(ApiCaller apiCaller, DataWithCompanyInfo messageData, NotificationTypes notificationType, string email) => await Send(apiCaller, messageData, notificationType, new List <string> { email });
public async Task <Result> Send(DataWithCompanyInfo messageData, NotificationTypes notificationType, List <string> emails) => await Send(new SlimAdminContext(adminId : 0), messageData, notificationType, emails);
public async Task <Result> Send(DataWithCompanyInfo messageData, NotificationTypes notificationType, string email) => await Send(new SlimAgentContext(agentId : 0, agencyId : 0), messageData, notificationType, new List <string> { email });
public async Task <Result> Send(SlimAgentContext agent, DataWithCompanyInfo messageData, NotificationTypes notificationType, List <string> emails) => await _notificationOptionsService.GetNotificationOptions(agent.AgentId, ApiCallerTypes.Agent, agent.AgencyId, notificationType) .Map(notificationOptions => BuildSettings(notificationOptions, emails)) .Tap(sendingSettings => _internalNotificationService.AddAgentNotification(agent, messageData, notificationType, sendingSettings));
public Task <Result> Send(SlimAgentContext agent, DataWithCompanyInfo messageData, NotificationTypes notificationType, List <string> emails) => Task.FromResult(Result.Success());
public Task <Result> Send(string templateId, string recipientAddress, DataWithCompanyInfo messageData) => Send(templateId, new[] { recipientAddress }, messageData);
private async Task SaveAndSend(Notification notification, DataWithCompanyInfo messageData) { var notificationId = await Save(notification); await Send(notification, notificationId, messageData); }
public Task <Result> Send(DataWithCompanyInfo messageData, NotificationTypes notificationType, string email) => Task.FromResult(Result.Success());
public async Task <Result> Send(SlimAdminContext admin, DataWithCompanyInfo messageData, NotificationTypes notificationType, string email) => await Send(admin, messageData, notificationType, new List <string> { email });
public async Task <Result> Send(string templateId, IEnumerable <string> recipientAddresses, DataWithCompanyInfo messageData) { var(_, isFailure, companyInfo, error) = await _companyService.Get(); messageData.CompanyInfo = isFailure ? new CompanyInfo() : companyInfo; return(await _mailSender.Send(templateId, recipientAddresses, messageData)); }
public async Task <Result> Send(SlimAdminContext admin, DataWithCompanyInfo messageData, NotificationTypes notificationType, List <string> emails) { return(await _notificationOptionsService.GetNotificationOptions(admin.AdminId, ApiCallerTypes.Admin, NoAgencyId, notificationType) .Map(notificationOptions => BuildSettings(notificationOptions, emails)) .Tap(sendingSettings => _internalNotificationService.AddAdminNotification(admin, messageData, notificationType, sendingSettings))); }
public Task <Result> Send(ApiCaller apiCaller, DataWithCompanyInfo messageData, NotificationTypes notificationType, List <string> emails) => Task.FromResult(Result.Success());