From c2d4eedf07014dad789095232f492e3a605bb0f8 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Mon, 7 Mar 2022 21:05:43 +0000 Subject: [PATCH] --- server/AyaNova/generator/CoreJobNotify.cs | 79 +++++++++++++++++++---- 1 file changed, 66 insertions(+), 13 deletions(-) diff --git a/server/AyaNova/generator/CoreJobNotify.cs b/server/AyaNova/generator/CoreJobNotify.cs index 29350a36..8199aaad 100644 --- a/server/AyaNova/generator/CoreJobNotify.cs +++ b/server/AyaNova/generator/CoreJobNotify.cs @@ -398,23 +398,13 @@ namespace AyaNova.Biz { log.LogDebug($"DeliverCustomerNotificationSMTP delivering notify event: {ne}"); if (string.IsNullOrWhiteSpace(deliveryAddress)) - { + { DeliveryLogItem.Fail = true; DeliveryLogItem.Error = $"No email address provided for smtp delivery; event: {ne}"; } else - { - - - string subject = ne.Subject; - - + { IMailer m = AyaNova.Util.ServiceProviderProvider.Mailer; - var body = ne.Message; - - - - if (!ServerGlobalOpsSettingsCache.Notify.SmtpDeliveryActive) { await NotifyEventHelper.AddOpsProblemEvent($"Email notifications are set to OFF at server, unable to send Customer email notification for this event:{ne}"); @@ -423,7 +413,70 @@ namespace AyaNova.Biz DeliveryLogItem.Error = $"Email notifications are set to OFF at server, unable to send Customer email notification for this event: {ne}"; } else - await m.SendEmailAsync(deliveryAddress, subject, body, ServerGlobalOpsSettingsCache.Notify); + { + //generate report if applicable + + if (subscription.LinkReportId != null) + { + long overrideLanguageId = 2; + + ReportBiz biz = new ReportBiz(ct, 1, overrideLanguageId, AuthorizationRoles.BizAdmin); + //example with workorder report + //{"AType":34,"selectedRowIds":[355],"ReportId":9,"ClientMeta":{"UserName":"AyaNova SuperUser","Authorization":"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxNjQ2NzgyNTc4IiwiaXNzIjoiYXlhbm92YS5jb20iLCJpZCI6IjEifQ.ad7Acq54JCRGitDWKDJFFnqKkidbdaKaFmj-RA_RG5E","DownloadToken":"NdoU8ca3LG4L39Tj2oi3UReeeM7FLevTgbgopTPhGbA","TimeZoneName":"America/Los_Angeles","LanguageName":"en-US","Hour12":true,"CurrencyName":"USD","DefaultLocale":"en","PDFDate":"3/3/22","PDFTime":"3:38 PM"}} + + var reportRequest = new DataListReportRequest(); + reportRequest.AType = AyaType.WorkOrder; + reportRequest.ReportId = 9; + reportRequest.SelectedRowIds = new long[] { 1 }; + var jwt = Api.Controllers.AuthController.GenRpt(overrideLanguageId); + + //this could be adjusted by culture if we allow user to set a culture but that's getting a bit into the weeds, likely the server default is fine + var pdfDate = new DateTime().ToShortDateString(); + var pdfTime = new DateTime().ToShortTimeString(); + + reportRequest.ClientMeta = JToken.Parse($"{{'UserName':'-','Authorization':'Bearer {jwt}','TimeZoneName':'America/Los_Angeles','LanguageName':'en-US','Hour12':true,'CurrencyName':'USD','DefaultLocale':'en','PDFDate':'{pdfDate}','PDFTime':'{pdfTime}'}}"); + //get port number + var match = System.Text.RegularExpressions.Regex.Match(ServerBootConfig.AYANOVA_USE_URLS, "[0-9]+"); + var API_URL = $"http://127.0.0.1:{match.Value}/api/{AyaNovaVersion.CurrentApiVersion}/"; + var jobid = await biz.RequestRenderReport(reportRequest, DateTime.UtcNow.AddMinutes(ServerBootConfig.AYANOVA_REPORT_RENDERING_TIMEOUT), API_URL, "AUTOMATED NO USER"); + if (jobid == null) + { + throw new ApplicationException($"Report render job id is null failed to start"); + } + else + { + bool done = false; + bool timedOut = false; + //todo: need a timeout here that sets done as failed due to timeout + while (!done && !timedOut) + { + var status = await JobsBiz.GetJobStatusAsync((Guid)jobid); + switch (status) + { + case JobStatus.Completed: + { + //get job logs and parse file name from it + JobOperationsBiz jobopsbiz = new JobOperationsBiz(ct, 1, AuthorizationRoles.BizAdmin); + List log = await jobopsbiz.GetJobLogListAsync((Guid)jobid); + var lastLog = log[log.Count - 1]; + var lastLogJ = JObject.Parse(lastLog.StatusText); + var path = (string)lastLogJ["reportfilename"]; + var FilePath = FileUtil.GetFullPathForTemporaryFile(path); + await m.SendEmailAsync(deliveryAddress, ne.Subject, ne.Message, ServerGlobalOpsSettingsCache.Notify, FilePath); + break; + } + case JobStatus.Failed: + throw new ApplicationException($"REPORT RENDER JOB {jobid} started but failed"); + } + + } + //throw new TimeoutException("JOB FAILED DUE TO REPORT RENDER TIMEOUT"); + } + } + else + await m.SendEmailAsync(deliveryAddress, ne.Subject, ne.Message, ServerGlobalOpsSettingsCache.Notify); + + } } }