Exemplo n.º 1
0
        public async Task <IncidentBasePayload> AddIncidentAsync(AddIncidentInput input,
                                                                 [ScopedService] ApplicationDbContext context, [FromServices] IFluentEmail email, CancellationToken cancellationToken)
        {
            DateTime?endedAt = null;

            if (input.EndedAt.HasValue)
            {
                endedAt = input.EndedAt;
            }

            var incident = new Incident
            {
                Title           = input.Title,
                Description     = input.Description !,
                DescriptionHtml = MarkdownHelper.ToHtml(input.Description),
                Service         = await context.Service.DeferredFirst(x => x.Id == input.ServiceId).FromCacheAsync(cancellationToken),
                StartedAt       = input.StartedAt,
                EndedAt         = endedAt,
                Author          = await context.User.DeferredFirst(x => x.Id == Int32.Parse(_httpContextAccessor.HttpContext !.User.FindFirst(ClaimTypes.Name) !.Value)).FromCacheAsync(cancellationToken)
            };
            await context.Incident.AddAsync(incident, cancellationToken);

            await context.SaveChangesAsync(cancellationToken);

            var template = await context.Settings.Where(x => x.Key == "backend.email.template.incident.create").DeferredFirst().FromCacheAsync(cancellationToken);

            var emailDomain = await context.Settings.Where(x => x.Key == "backend.email.domain").DeferredFirst().FromCacheAsync(cancellationToken);

            try
            {
                await context.Subscriber.ForEachAsync((subscriber) =>
                {
                    if (!template.Value.IsNullOrEmpty())
                    {
                        email.To(subscriber.Email).Subject($"Incident Created: {incident.Service.Name}").UsingTemplate(template.Value, new
                        {
                            Title        = incident.Title,
                            Description  = incident.Description,
                            ServiceName  = incident.Service.Name,
                            StartedAd    = incident.StartedAt.ToString(CultureInfo.InvariantCulture),
                            EndedAt      = incident.EndedAt?.ToString(CultureInfo.InvariantCulture),
                            Attachements = incident.Files
                        });
                        // We set the message ID so we can reference it in status updates and what not.
                        email.Header("Message-ID", $"<{incident.Id}@{emailDomain.Value}>");
                        email.Send();
                    }
                }, cancellationToken);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            QueryCacheManager.ExpireType <Incident>();
            return(new IncidentBasePayload(incident));
        }
        public async Task <IncidentMessageBasePayload> AddIncidentMessage(AddIncidentMessageInput input,
                                                                          [ScopedService] ApplicationDbContext context, [FromServices] IFluentEmail email,
                                                                          CancellationToken cancellationToken)
        {
            var incident = await context.Incident.IncludeOptimized(x => x.Service).IncludeOptimized(x => x.Messages)
                           .DeferredFirst(x => x.Id == input.IncidentId).FromCacheAsync(cancellationToken);

            var status =
                await context.Status.DeferredFirst(x => x.Id == input.StatusId).FromCacheAsync(cancellationToken);

            var incidentMessage = new IncidentMessage
            {
                Message     = input.Message,
                MessageHtml = MarkdownHelper.ToHtml(input.Message),
                Author      = await context.User.DeferredFirst(
                    x => x.Id == Int32.Parse(_httpContextAccessor.HttpContext !.User.FindFirst(ClaimTypes.Name) !.Value)).FromCacheAsync(cancellationToken),
                Incident = incident,
                Status   = status
            };

            if (input.AttachedFilesIds.HasValue)
            {
                incidentMessage.Attachments = await addAttachments(input.AttachedFilesIds.Value, context, cancellationToken);;
            }

            incident.Service.Status = status;

            await context.IncidentMessage.AddAsync(incidentMessage, cancellationToken);

            await context.SaveChangesAsync(cancellationToken);

            var emailDomain = await context.Settings.DeferredFirst(x => x.Key == "backend.email.domain").FromCacheAsync(cancellationToken);

            var template = await context.Settings.DeferredFirst(x => x.Key == "backend.email.template.incidentmessage").FromCacheAsync(cancellationToken);

            try
            {
                await context.Subscriber.ForEachAsync((subscriber) =>
                {
                    if (!template.Value.IsNullOrEmpty())
                    {
                        email.To(subscriber.Email).Subject($"Incident Created: {incident.Service.Name}").UsingTemplate(template.Value, new
                        {
                            Title        = incident.Title,
                            Description  = incident.Description,
                            ServiceName  = incident.Service.Name,
                            StartedAd    = incident.StartedAt.ToString(CultureInfo.InvariantCulture),
                            EndedAt      = incident.EndedAt?.ToString(CultureInfo.InvariantCulture),
                            Attachements = incident.Files
                        });
                        // Build references list from previous incident messages, newest first, oldest last
                        var references = "";
                        incident.Messages.OrderByDescending(x => x.CreatedAt).ForEach((message) =>
                        {
                            references += $"<{message.Id}+{incident.Id}@{emailDomain.Value}> ";
                        });
                        // We set the message ID so we can reference it in status updates and what not.
                        email.Header("Message-ID", $"<{incidentMessage.Id}+{incident.Id}@{emailDomain.Value}>");
                        // Build references string for header
                        email.Header("References", $"<{incidentMessage.Id}+{incident.Id}@{emailDomain.Value}> {references}<{incident.Id}@{emailDomain.Value}>");
                        email.Send();
                    }
                }, cancellationToken);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            QueryCacheManager.ExpireType <IncidentMessage>();
            return(new IncidentMessageBasePayload(incidentMessage));
        }