Exemplo n.º 1
0
        public async Task <IActionResult> SendTestEmail([FromServices] IMoongladeNotification notification)
        {
            var response = await notification.SendTestNotificationAsync();

            if (!response.IsSuccess)
            {
                Response.StatusCode = StatusCodes.Status500InternalServerError;
            }
            return(Json(response));
        }
        public async Task <Response> Post(NotificationRequest request, CancellationToken ct)
        {
            T GetModelFromPayload <T>() where T : class
            {
                var json = request.Payload.ToString();

                return(JsonSerializer.Deserialize <T>(json));
            }

            try
            {
                if (!Settings.EnableEmailSending)
                {
                    return(new FailedResponse((int)ResponseFailureCode.EmailSendingDisabled, "Email Sending is disabled."));
                }

                _notification.AdminEmail       = request.AdminEmail;
                _notification.EmailDisplayName = request.EmailDisplayName;
                switch (request.MessageType)
                {
                case MailMesageTypes.TestMail:
                    await _notification.SendTestNotificationAsync();

                    return(new SuccessResponse());

                case MailMesageTypes.NewCommentNotification:
                    var commentPayload = GetModelFromPayload <NewCommentPayload>();
                    _ = Task.Run(async() => await _notification.SendNewCommentNotificationAsync(commentPayload), ct);
                    return(new SuccessResponse());

                case MailMesageTypes.AdminReplyNotification:
                    var replyPayload = GetModelFromPayload <CommentReplyPayload>();
                    _ = Task.Run(async() => await _notification.SendCommentReplyNotificationAsync(replyPayload), ct);
                    return(new SuccessResponse());

                case MailMesageTypes.BeingPinged:
                    var pingPayload = GetModelFromPayload <PingPayload>();
                    _ = Task.Run(async() => await _notification.SendPingNotificationAsync(pingPayload), ct);
                    return(new SuccessResponse());

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"Error sending notification for type '{request.MessageType}'. Requested by '{User.Identity.Name}'");
                Response.StatusCode = StatusCodes.Status500InternalServerError;
                return(new FailedResponse((int)ResponseFailureCode.GeneralException, e.Message));
            }
        }