Exemplo n.º 1
0
        public async Task NotifyUserAsync(string userId)
        {
            try
            {
                var user = await _subscribesService.FindUserByIdAsync(userId);

                if (user == null)
                {
                    throw new UserNotFoundException(userId);
                }

                //create private notice
                string content = GetMailTemplate(_environment, _appSettings, "subscribe");
                var    notice  = new Notice
                {
                    Title   = "您的訂閱會員已經生效",
                    Content = content
                };
                await _noticesService.CreateUserNotificationAsync(notice, new List <string> {
                    userId
                });

                // send email
                string mailSubject  = notice.Title;
                string mailTemplate = GetMailTemplate(_environment, _appSettings);
                string mailContent  = mailTemplate.Replace(ApplicationCore.Consts.MAILCONTENT, content);

                await _mailService.SendAsync(user.Email, notice.Title, mailContent);


                // send hub notification
                var connections = _userConnectionManager.GetUserConnections(userId);
                if (connections.HasItems())
                {
                    foreach (var connectionId in connections)
                    {
                        await _notificationHubContext.Clients.Client(connectionId).SendAsync(ApplicationCore.Consts.Notifications, userId);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
            }
        }