-
Notifications
You must be signed in to change notification settings - Fork 5
/
ReportService.cs
247 lines (222 loc) · 10.5 KB
/
ReportService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using ExpressBase.Common;
using ExpressBase.Common.Constants;
using ExpressBase.Common.Data;
using ExpressBase.Common.Structures;
using ExpressBase.Objects.Services;
using ExpressBase.Objects.ServiceStack_Artifacts;
using ExpressBase.Security;
using ExpressBase.ServiceStack.Services;
using Newtonsoft.Json;
using ServiceStack;
using ServiceStack.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ExpressBase.ServiceStack.MQServices
{
//[Restrict(InternalOnly = true)]
public class ReportInternalService : EbMqBaseService
{
//public ReportInternalService(IServiceClient _ssclient, IMessageProducer _mqp, IMessageQueueClient _mqc) : base(_ssclient, _mqp, _mqc)
public ReportInternalService(IMessageProducer _mqp, IMessageQueueClient _mqc) : base(_mqp, _mqc)
{
ObjectService = base.ResolveService<EbObjectService>();
ReportService = base.ResolveService<ReportService>();
SchedulersService = base.ResolveService<SchedulerServices>();
}
EbObjectService ObjectService { get; set; }
ReportService ReportService { get; set; }
SchedulerServices SchedulersService { get; set; }
ReportRenderResponse RepRes = null;
EbJobArguments JobArgs = null;
AllGroupCollection GroupCollection { get; set; }
AllUserCollection UserCollection { get; set; }
AllDelMessagaeCollection MessageCollection { get; set; }
Dictionary<string, List<User>> Locales = new Dictionary<string, List<User>>();
//Dictionary<string, List<User>> SlackLocales = new Dictionary<string, List<User>>();
public ReportInternalResponse Post(ReportInternalRequest request)
{
try
{
Console.WriteLine(" Reached Inside MQService/ReportServiceInternal in SS .. Before Report Render");
JobArgs = request.JobArgs;
if (JobArgs != null)
{
EbConnectionFactory ebConnectionFactory = new EbConnectionFactory(JobArgs.SolnId, this.Redis);
ObjectService.EbConnectionFactory = ReportService.EbConnectionFactory = SchedulersService.EbConnectionFactory = ebConnectionFactory;
Dictionary<string, byte[]> LocaleReport = new Dictionary<string, byte[]>();
Console.WriteLine(" Fetching Live version " + JobArgs.ObjId);
EbObjectFetchLiveVersionResponse res = ((EbObjectFetchLiveVersionResponse)ObjectService.Get(new EbObjectFetchLiveVersionRequest()
{
Id = JobArgs.ObjId
}));
if (res.Data != null && res.Data.Count > 0 && JobArgs.ToUserIds != null)
{
UserCollection = JsonConvert.DeserializeObject<AllUserCollection>(JobArgs.ToUserIds);
GroupCollection = JsonConvert.DeserializeObject<AllGroupCollection>(JobArgs.ToUserGroupIds);
MessageCollection = JsonConvert.DeserializeObject<AllDelMessagaeCollection>(JobArgs.Message);
if (UserCollection?.EmailUser != "" || GroupCollection?.EmailGroup != "")
{
Locales = new Dictionary<string, List<User>>();
JobArgs.DeliveryMechanisms = (DeliveryMechanisms)1;
JobArgs.Message = MessageCollection?.EmailMessage;
GetEmailUserDetails(JobArgs);
JobPush(res);
}
if (UserCollection?.SMSUser != "" || GroupCollection?.SMSGroup != "")
{
JobArgs.DeliveryMechanisms = (DeliveryMechanisms)2;
JobArgs.Message = MessageCollection?.SMSMessage;
JobPush(res);
}
if (UserCollection?.SlackUser != "" || GroupCollection?.SlackGroup != "")
{
Locales = new Dictionary<string, List<User>>();
JobArgs.DeliveryMechanisms = (DeliveryMechanisms)3;
JobArgs.Message = MessageCollection?.SlackMessage;
getSlackUser();
JobPush(res);
}
}
else
{
Console.WriteLine("No Live version avaialble for :" + JobArgs.ObjId);
}
}
}
catch (Exception e)
{
Console.WriteLine(" Error in ReportInternal " + e.Message + e.StackTrace);
}
return new ReportInternalResponse() { };
}
public void JobPush(EbObjectFetchLiveVersionResponse res)
{
try
{
EbObjectWrapper Live_ver = res.Data[0];
foreach (KeyValuePair<string, List<User>> locale in Locales)
{
RepRes = ReportService.Get(new ReportRenderRequest
{
Refid = Live_ver.RefId,
//RenderingUser = new User { FullName = "Machine User" },
//ReadingUser = locale.Value[0],
Params = JobArgs.Params,
SolnId = JobArgs.SolnId,
UserAuthId = JobArgs.UserAuthId,
UserId = JobArgs.UserId,
WhichConsole = "uc"
});
Console.WriteLine(locale.Key);
Console.WriteLine("Inside MQService/ReportServiceInternal in SS \n After Report Render .Going to send email");
if (RepRes != null)
{
RepRes.StreamWrapper.Memorystream.Position = 0;
foreach (User _u in locale.Value)
{
if (JobArgs.DeliveryMechanisms == DeliveryMechanisms.Email)
PublishMail(_u.Email);
else if (JobArgs.DeliveryMechanisms == DeliveryMechanisms.Slack)
PublishSlackChat(_u.Email);
//else if (JobArgs.DeliveryMechanisms == DeliveryMechanisms.Sms)
// PublishSMS(_u.Email);
}
}
//LocaleReport.Add(locale.Key, RepRes.ReportBytea);
}
}
catch (Exception e)
{
Console.WriteLine(" Error in ReportInternal " + e.Message + e.StackTrace);
}
}
public void GetEmailUserDetails(EbJobArguments JobArgs)
{
Console.WriteLine("----------Got Live Version Getting mail ids");
GetUserEmailsResponse mailres = SchedulersService.Get(new GetUserEmailsRequest()
{
UserIds = UserCollection.EmailUser,
UserGroupIds = GroupCollection.EmailGroup
});
Dictionary<int, string> MailIds = mailres.UserEmails
.Concat(mailres.UserGroupEmails)
.GroupBy(d => d.Key)
.ToDictionary(d => d.Key, d => d.First().Value);
foreach (KeyValuePair<int, string> u in MailIds)
{
User usr = GetUserObject(string.Format(TokenConstants.SUB_FORMAT, JobArgs.SolnId, u.Key, "uc"));
if (usr != null)
{
if (Locales.ContainsKey(usr.Preference.Locale))
Locales[usr.Preference.Locale].Add(usr);
else Locales.Add(usr.Preference.Locale, new List<User> { usr });
}
else
{
Console.WriteLine("Redis User Object is empty for : " + u.Value);
}
}
Console.WriteLine("Number of locales : " + Locales.Count());
}
public void getSlackUser()
{
List<User> Users = new List<User>();
string[] _slackUsers = (UserCollection.SlackUser + "," + UserCollection.SlackUser).Split(',').Distinct().ToArray();
_slackUsers = (GroupCollection.SlackGroup + "," + GroupCollection.SlackGroup).Split(',').Distinct().ToArray();
foreach (string slackid in _slackUsers)
{
User _u = new User { Email = slackid, Preference = new Preferences { TimeZone = "(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi", Locale = "en-US" } };
Users.Add(_u);
}
Locales.Add("en-US", Users);
}
public void PublishMail(string _mailId)
{
MessageProducer3.Publish(new EmailServicesRequest()
{
From = "request.from",
To = _mailId,
Cc = /*ebEmailTemplate.Cc.Split(",")*/ null,
Bcc = /*ebEmailTemplate.Bcc.Split(",")*/ null,
Subject = RepRes.ReportName + " - " + RepRes.CurrentTimestamp.ToShortDateString(),
Message = JobArgs.Message,
UserId = JobArgs.UserId,
UserAuthId = JobArgs.UserAuthId,
SolnId = JobArgs.SolnId,
AttachmentReport = RepRes.ReportBytea,
AttachmentName = RepRes.ReportName + " - " + RepRes.CurrentTimestamp.ToString("dd-MM-yy") + ".pdf"
});
Console.WriteLine("Email to " + _mailId + " pushed.....");
}
public void PublishSlackChat(string _to)
{
MessageProducer3.Publish(new SlackCreateRequest
{
ObjId = JobArgs.ObjId,
Params = JobArgs.Params,
SolnId = JobArgs.SolnId,
UserId = JobArgs.UserId,
UserAuthId = JobArgs.UserAuthId,
AttachmentReport = RepRes.ReportBytea,
AttachmentName = RepRes.ReportName + " - " + RepRes.CurrentTimestamp.ToString("dd-MM-yy"),
Message = JobArgs.Message,
To = _to
});
}
public void PublishSMS(string _to)
{
MessageProducer3.Publish(new SMSPrepareRequest
{
ObjId = JobArgs.ObjId,
Params = JobArgs.Params,
SolnId = JobArgs.SolnId,
UserId = JobArgs.UserId,
UserAuthId = JobArgs.UserAuthId,
Message = JobArgs.Message,
To = _to
});
}
}
}