Exemplo n.º 1
0
        // Shop Report Notification Template.
        internal async Task SendShopReportNotification(int requestId)
        {
            // Load Repair to Populate Template.
            var request = await _connection.QuerySingleAsync <RequestNotificationModel>("EXEC Notification.usp_GetRequestNotification @RequestId", new { RequestId = requestId });

            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            // Check Complete Report.
            if (request.ReportCompletedInd)
            {
                // Load Template.
                var template = await _connection.QuerySingleAsync <NotificationTemplateModel>(TemplateSql, new { TemplateName = "ShopReportEmail" });

                // Load Reporting Contacts.
                var reportingContacts = (await _contacts.GetReportingContacts(request.ShopGuid)).ToList();

                // Load Distinct Timezones.
                var timezones = reportingContacts.Select(r => r.TimeZoneInfoId).Distinct().ToArray();

                // Load Upload Attachments.
                var uploads = new List <INotificationMessageAttachment>();
                uploads.AddRange(await LoadAttachments(UploadType.ScanRequests, request.RequestId.ToString()));
                uploads.AddRange(await LoadAttachments(UploadType.VehicleMakes, request.VehicleMakeId.ToString()));

                // Process Report per Timezone.
                foreach (string timezone in timezones)
                {
                    // Load Time Zone Offset.
                    var offset = TimeZoneInfo.FindSystemTimeZoneById(timezone).GetUtcOffset(DateTimeOffset.UtcNow).ToString();

                    // Load Report.
                    var report = (await _reportGenerator.GetScanReportPdfStreamAsync(request.RequestId, offset)).ToBase64String();

                    // Create Attachment.
                    var attachments = new List <INotificationMessageAttachment>
                    {
                        new NotificationMessageAttachment
                        {
                            Filename      = $"AirProScan-{request.RequestId}.pdf",
                            MimeType      = "application/pdf",
                            ContentBase64 = report
                        }
                    };

                    // Check Estimate Plan.
                    if (request.EstimatePlanInd)
                    {
                        var estimateReport = (await _reportGenerator.GetEstimateReportPdfStreamAsync(request.RepairId, offset)).ToBase64String();
                        attachments.Add(new NotificationMessageAttachment
                        {
                            Filename      = $"AirProAssessment-{request.RequestId}.pdf",
                            MimeType      = "application/pdf",
                            ContentBase64 = estimateReport
                        });
                    }

                    // Add Upload Attachments.
                    attachments.AddRange(uploads);

                    // Populate Template.
                    var message = new NotificationMessageModel
                    {
                        Destinations = reportingContacts.Where(r => r.TimeZoneInfoId == timezone).ToArray(),
                        EmailSubject = Templates.ShopReportEmailTemplate(template.Subject, request),
                        EmailBody    = Templates.ShopReportEmailTemplate(template.EmailBody, request),
                        TextMessage  = Templates.ShopReportEmailTemplate(template.TextMessage, request),
                        Attachments  = attachments
                    };

                    // Send Message.
                    await SendNotifications(message);
                }
            }
        }