Exemple #1
0
        /// <summary>
        /// Create a notification
        /// </summary>
        /// <param name="notification">NotificationCreationModel</param>
        /// <returns>NotificationModel</returns>
        public async Task <NotificationModel> CreateNotification(NotificationCreationModel notification)
        {
            var date = DateTime.UtcNow;

            NotificationDAO notificationDao = new NotificationDAO()
            {
                NotificationText = notification.NotificationText,
                CreationDate     = date,
                UpdateDate       = date,
                ResponseId       = notification.ResponseId.ToString(),
                Status           = notification.Status,
                Tags             = notification.Tags,
                Title            = notification.Title,
                User             = notification.User
            };

            notificationDao.Id = await _repoNotifications.CreateItemAsync(notificationDao);

            if (_repoNotifications.IsDocumentKeyNull(notificationDao))
            {
                throw new Exception($"An error occured when creating a new notification");
            }

            NotificationModel output = _mapper.Map <NotificationModel>(notificationDao);

            return(output);
        }
        public async Task <NotificationModel> SendNotification(NotificationCreationModel notificationReq)
        {
            RestRequest request = await PrepareQuery("Notifications", Method.POST);

            request.AddParameter("application/json", JsonConvert.SerializeObject(notificationReq), ParameterType.RequestBody);
            var queryResult = await _client.ExecuteTaskAsync <NotificationModel>(request);

            if (queryResult.IsSuccessful)
            {
                return(queryResult.Data);
            }
            else
            {
                _logger.LogError($"SendNotification: Error: {queryResult.StatusCode}");
            }
            return(null);
        }
        public async Task Consume(ConsumeContext <INotificationSendEvent> context)
        {
            try
            {
                if (context.Message != null && context.Message as INotificationSendEvent != null && context.Message.Notification != null)
                {
                    NotificationCreationModel notification = context.Message.Notification;
                    _logger.LogDebug($"NotificationSendEventConsumer: Text: '{notification.NotificationText}'.");
                    _logger.LogDebug($"NotificationSendEventConsumer: Status: '{notification.Status}'.");
                    _logger.LogDebug($"NotificationSendEventConsumer: Title: '{notification.Title}'.");
                    _logger.LogDebug($"NotificationSendEventConsumer: User: '******'.");
                    DateTime date = DateTime.UtcNow;
                    notification.ResponseId = context.Message.ResponseId;
                    NotificationModel result = await _notificationRestService.SendNotification(notification);

                    if (result != null)
                    {
                        await context.Publish(new EventSagaReceiveResponseActionCallback()
                        {
                            IsCloseAction = context.Message.IsCloseAction,
                            ResponseId    = context.Message.ResponseId,
                            ActionId      = context.Message.ActionId,
                            Status        = ActionStatus.Success
                        });

                        _logger.LogDebug($"NotificationSendEventConsumer: NotificationId: '{result.NotificationId}' published.");
                        return;
                    }
                }
                _logger.LogError("NotificationSendEventConsumer: Invalid Null or Empty Action Notification");
                throw new Exception("Invalid or Null Action Notification");
            }
            catch (Exception e)
            {
                await context.Publish(new EventSagaReceiveResponseActionCallback()
                {
                    IsCloseAction = context.Message.IsCloseAction,
                    ResponseId    = context.Message.ResponseId,
                    ActionId      = context.Message.ActionId,
                    Status        = ActionStatus.Error
                });

                _logger.LogError($"NotificationSendEventConsumer: {e.Message}");
                throw e;
            }
        }
        public async Task <IActionResult> SendNotification([FromBody] NotificationCreationModel notificationReq)
        {
            try
            {
                var jsonNotification = JsonConvert.SerializeObject(notificationReq);

                NotificationOutcome response = (notificationReq.Tags != null && notificationReq.Tags.Count > 0) ?
                                               await _hubClient.SendGcmNativeNotificationAsync("{\"data\":{\"message\":" + jsonNotification + "}}", notificationReq.Tags) :
                                               await _hubClient.SendGcmNativeNotificationAsync("{\"data\":{\"message\":" + jsonNotification + "}}");

                NotificationOutcome responseApple = (notificationReq.Tags != null && notificationReq.Tags.Count > 0) ?
                                                    await _hubClient.SendAppleNativeNotificationAsync("{\"aps\":{\"alert\":\"" + notificationReq.Title + ": " + notificationReq.NotificationText + "\"}}", notificationReq.Tags) :
                                                    await _hubClient.SendAppleNativeNotificationAsync("{\"aps\":{\"alert\":\"" + notificationReq.Title + ": " + notificationReq.NotificationText + "\"}}");

                var outcomes = new List <NotificationOutcome>()
                {
                    response, responseApple
                };
                NotificationModel model = null;

                if (response.Results != null && responseApple.Results != null)
                {
                    notificationReq.Status = 1;
                    model = await _notificationDataManager.CreateNotification(notificationReq);

                    return(Ok(model));
                }
                else
                {
                    notificationReq.Status = 0;
                    model = await _notificationDataManager.CreateNotification(notificationReq);

                    return(Ok(model));
                }
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status502BadGateway, e.Message));
            }
        }