@@ -12,7 +12,7 @@ Method OnTask() As %Status
1212 Set tHour =$PIECE ($ZTIME ($PIECE ($HOROLOG ," ," ,2 ),2 )," :" ,1 )
1313 Set tMinute =$PIECE ($ZTIME ($PIECE ($HOROLOG ," ," ,2 ),2 )," :" ,2 )
1414
15- If $G ( ^ PivotSubscriptions .Settings (" TestEnv" ) ) {
15+ If ##class ( PivotSubscriptions.Utils ). GetSettings (" TestEnv" , 0 ) {
1616 // Ignore minute for testing purposes
1717 // This will allow the task to be run on demand and send emails immediately
1818 // Leaving DayOfWeek and Hour so the scheduled task does not spam emails
@@ -41,9 +41,6 @@ Method OnTask() As %Status
4141ClassMethod SendSubscription (pID , pPivot As %String , pEmails As %String , pFormat As %String , pDayOfWeek , pHour , pMinute , pName ) As %Status
4242{
4343 Set tSC =$$$OK
44-
45- Set tSC =##class (PivotSubscriptions.EventLog ).LogEvent (" sendSubscription" , $username , pPivot , pDayOfWeek , pHour , pMinute , pFormat , pEmails , pName , .pEvent )
46- If $$$ISERR(tSC ) Quit tSC
4744
4845 If pName =" " {
4946 Set pName =$E (pPivot ,1 ,*-6 )
@@ -54,14 +51,6 @@ ClassMethod SendSubscription(pID, pPivot As %String, pEmails As %String, pFormat
5451 } Catch ex {
5552 Set tSC =ex .AsStatus ()
5653 }
57- // Update send event with status and end time
58- Set tSC =pEvent .LogUpdate (tSC )
59- If $$$ISERR(tSC ) Quit tSC
60-
61- If '(##class (PivotSubscriptions.Utils ).CheckTable (pName )) {
62- Set tSC =##class (PivotSubscriptions.EventLog ).LogEvent (" tableError" , $username , pPivot , pDayOfWeek , pHour , pMinute , pFormat , pEmails , pName )
63- If $$$ISERR(tSC ) Quit tSC
64- }
6554
6655 Quit tSC
6756}
@@ -72,14 +61,17 @@ ClassMethod SendEmail(pID, pPivot As %String, pEmails As %String, pFormat As %St
7261 Set tFilename =" "
7362
7463 Set mdx =##class (%DeepSee.Utils ).%GetMDXFromPivot (pPivot ,.tSC ,0 )
64+ Set tSubscription =##class (PivotSubscriptions.Subscription ).%OpenId (pID )
7565
7666 // Iterate through all subscribers and send emails to each individually
77- For count =1 :1 :$l (pEmails ," ," ) {
78- Set tEmail =$p (pEmails ," ," ,count )
67+ For tCount =1 :1 :$l (pEmails ," ," ) {
68+ Set tEmail =$p (pEmails ," ," ,tCount )
69+
70+ Set tSC =##class (PivotSubscriptions.EventLog ).CreateEvent (" sendSubscription" ,$username ,pPivot ,tSubscription .DayOfWeek ,tSubscription .Hour ,tSubscription .Minute ,tSubscription .Format ,tEmail ,pName ,.pEvent )
71+ If $$$ISERR(tSC ) Quit tSC
7972
8073 Set tHasResults =0
8174
82- Set tSubscription =##class (PivotSubscriptions.Subscription ).%OpenId (pID )
8375 Set tCustomFilter =tSubscription .CustomFilters .GetAt (tEmail )
8476 Set tSC =##class (PivotSubscriptions.Utils ).ExecuteForSubscription (mdx ,tCustomFilter ,.tHasResults )
8577
@@ -101,7 +93,7 @@ ClassMethod SendEmail(pID, pPivot As %String, pEmails As %String, pFormat As %St
10193 Set validAdminEmail =##class (PivotSubscriptions.Utils ).CheckConfigurationGlobal (" AdminEmail" )
10294
10395 If validAdminEmail {
104- Set msg .ReplyTo =^ PivotSubscriptions .Settings (" AdminEmail" )
96+ Set msg .ReplyTo =##class ( PivotSubscriptions.Utils ). GetSettings (" AdminEmail" )
10597 }
10698
10799 // Remove ".pivot" extension for subject
@@ -110,17 +102,16 @@ ClassMethod SendEmail(pID, pPivot As %String, pEmails As %String, pFormat As %St
110102 Set msg .IsHTML =1 // Set IsHTML to 1 if email uses HTML
111103
112104 If tHasResults {
113-
114105 // Add attachment
115- Do ##class (PivotSubscriptions.Utils ).WriteEmail (pName , msg , tableStatus )
106+ Do ##class (PivotSubscriptions.Utils ).WriteEmail (tSubscription , msg , tableStatus )
116107
117108 // if table is too large, then just attach Excel instead
118109 If ((pFormat = " PDF" ) && (tableStatus )) {
119110 Set tSC =##class (PivotSubscriptions.Utils ).GenerateFileForSubscription (" PDF" ,mdx ,tCustomFilter ,pName ,pPivot ,.tFilename )
120111 } ElseIf 'tableStatus {
121112 // Check if both configuration settings for admin email is valid before sending error message
122113 If validAdminEmail {
123- Do ##class (PivotSubscriptions.Utils ).SendErrorMessage ( pPivot , ^PivotSubscriptions . Settings ( " AdminEmail " ) ,pName )
114+ Do ##class (PivotSubscriptions.Utils ).TableErrorMessage ( tSubscription , tEmail ,pName , " TooLarge " )
124115 }
125116 }
126117
@@ -132,18 +123,22 @@ ClassMethod SendEmail(pID, pPivot As %String, pEmails As %String, pFormat As %St
132123 }
133124 } Else {
134125 Do ##class (PivotSubscriptions.Utils ).WriteEmailHeader (msg )
135- Do msg .TextData .WriteLine ($G (^PivotSubscriptions .Settings (" NoDataMessage" )," No data for requested pivot." ))
126+ Do msg .TextData .WriteLine (##class (PivotSubscriptions.Utils ).GetSettings (" NoDataMessage" ," No data for requested pivot." ))
127+ Do ##class (PivotSubscriptions.Utils ).WriteUnsubscribe (msg )
136128 Do ##class (PivotSubscriptions.Utils ).WriteEmailFooter (msg )
129+
130+ Do ##class (PivotSubscriptions.Utils ).TableErrorMessage (tSubscription ,tEmail ,pName ," NoResults" )
137131 }
138132 }
139133
140134 Do msg .To .SetAt (tEmail ,1 )
141135
142136 // Send email
143- If tHasResults ||$G ( ^ PivotSubscriptions .Settings (" SendNoData" ) ,1 ) {
137+ If tHasResults ||##class ( PivotSubscriptions.Utils ). GetSettings (" SendNoData" ,1 ) {
144138 Set tSC =server .Send (msg )
139+ Set tSC =pEvent .UpdateStatus (tSC )
140+ If $$$ISERR(tSC ) Quit tSC
145141 }
146- // TODO: Log individual send status?
147142
148143 If tHasResults {
149144 // Delete file
0 commit comments