Repository navigation
Expand file tree
/
Copy pathTeamsService.cs
More file actions
290 lines (234 loc) · 10.3 KB
/
Copy pathTeamsService.cs
File metadata and controls
290 lines (234 loc) · 10.3 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
using TeamsHook.NET;
using Xml2SqlImport.Data.SPModels;
using Xml2SqlImport.Helpers.Responses;
using Xml2SqlImport.Interfaces;
namespace Xml2SqlImport.Services
{
public class TeamsService : ITeamsService
{
private readonly ILocalDataService _db;
private readonly IAppSettings _appSettings;
public TeamsService(ILocalDataService db, IAppSettings appSettings)
{
_db = db;
_appSettings = appSettings;
}
public async Task<RetVal> sendTeamsMessage(string body, string title)
{
var ret = new RetVal { success = false, errorMessage = "Could not send the message" };
try
{
var client = new TeamsHookClient();
var card = new MessageCard();
card.Text = body;
card.Title = title;
await client.PostAsync(_appSettings.teamsWebhook, card);
ret.success = true;
ret.errorMessage = null;
}
catch (Exception e)
{
ret.errorMessage = e.Message;
}
return ret;
}
public async Task<RetVal> sendTeamsLastImportDates()
{
var ret = new RetVal { success = false, errorMessage = "Could not send the message" };
try
{
var client = new TeamsHookClient();
var card = new MessageCard();
card.Title = $"Last PEGA BIX file import dates for: {(DateTime.Today, 10).ToString().Substring(1, 10)}";
var imp = await _db.getLastFileImportDates();
if (!imp.success)
{
card.Text = "Could not read the latest file import dates";
}
else
{
var lst = imp.returnVal!;
var tbl = _convertRecordsToHtml<spIMPORT_GetLastFileImportDates>(lst);
card.Text = tbl;
}
var result = await client.PostAsync(_appSettings.teamsWebhook, card);
if (!result.IsSuccessStatusCode)
{
ret.success = false;
ret.errorMessage = result.ReasonPhrase;
return ret;
}
ret.success = true;
ret.errorMessage = null;
}
catch (Exception e)
{
ret.errorMessage = e.Message;
}
return ret;
}
public async Task<RetVal> sendTeamsLastKpiDateValues()
{
var ret = new RetVal { success = false, errorMessage = "Could not send the message" };
try
{
var client = new TeamsHookClient();
var card = new MessageCard();
card.Title = $"Last PEGA BIX KPI dates values: {(DateTime.Today, 10).ToString().Substring(1, 10)}";
var imp = await _db.getLastKpiDateValues();
if (!imp.success)
{
card.Text = "Could not read the latest file kpi values";
}
else
{
var lst = imp.returnVal!;
var tbl = _convertRecordsToHtmlWithIsDifferent<spIMPORT_GetLast2KpiDateValues>(lst);
card.Text = tbl;
//var grpIds = lst.Select(r => r.groupId).Distinct();
//foreach (int? grpId in grpIds) {
// var records = lst.Where(r => r.groupId == grpId).Select(c => c.fileName && c.status && c.currentDate && c.).ToList();
// var tbl = _convertRecordsToHtml<spIMPORT_GetLastKpiDateValues>(records);
// card.Text = tbl;
//}
var result = await client.PostAsync(_appSettings.teamsWebhook, card);
if (!result.IsSuccessStatusCode)
{
ret.success = false;
ret.errorMessage = result.ReasonPhrase;
return ret;
}
}
ret.success = true;
ret.errorMessage = null;
}
catch (Exception e)
{
ret.errorMessage = e.Message;
}
return ret;
}
protected string _convertRecordsToHtml<T>(List<T> records)
{
PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
// internal css is not supported by teams
// only inline is supported
StringBuilder strHTMLBuilder = new StringBuilder();
strHTMLBuilder.Append("<html >");
strHTMLBuilder.Append("<head>");
strHTMLBuilder.Append("</head>");
strHTMLBuilder.Append("<body>");
strHTMLBuilder.Append("<table style='border-collapse: collapse; border-spacing: 1; border-radius: 12px; overflow: hidden; margin: 25px 0;font-size: 0.9em;font-family: sans-serif;min-width: 400px;box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);'>");
strHTMLBuilder.Append("<thead><tr style='background-color: #009879;color: #ffffff;text-align: left;font-size: 1.2em;'>");
foreach (PropertyInfo prop in props)
{
var ttl = prop.Name;
if (ttl.Substring(0, 1) == "_") ttl = ttl.Substring(1);
strHTMLBuilder.Append("<td style='padding: 7px 15px;'><b>");
strHTMLBuilder.Append(ttl);
strHTMLBuilder.Append("</b></td>");
}
strHTMLBuilder.Append("</tr></thead><tbody>");
foreach (T rec in records)
{
strHTMLBuilder.Append("<tr>");
var values = new object[props.Length];
for (int i = 0; i < props.Length; i++)
{
if (props[i].PropertyType == typeof(int?))
{
strHTMLBuilder.Append("<td style='padding: 7px 15px;text-align: center;'>");
}
else
{
strHTMLBuilder.Append("<td style='padding: 7px 15px;'>");
}
var val = props[i].GetValue(rec!, null) ?? "";
strHTMLBuilder.Append(val);
strHTMLBuilder.Append("</td>");
}
strHTMLBuilder.Append("</tr>");
}
//Close tags.
strHTMLBuilder.Append("</tbody></table>");
strHTMLBuilder.Append("</body>");
strHTMLBuilder.Append("</html>");
string Htmltext = strHTMLBuilder.ToString();
return Htmltext;
}
protected string _convertRecordsToHtmlWithIsDifferent<T>(List<T> records)
{
// be sure that the isDifferent column is the first column of T
PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
// internal css is not supported by teams
// only inline is supported
StringBuilder strHTMLBuilder = new StringBuilder();
strHTMLBuilder.Append("<html >");
strHTMLBuilder.Append("<head>");
strHTMLBuilder.Append("</head>");
strHTMLBuilder.Append("<body>");
strHTMLBuilder.Append("<table style='border-collapse: collapse; border-spacing: 1; border-radius: 12px; overflow: hidden; margin: 25px 0;font-size: 0.9em;font-family: sans-serif;min-width: 400px;box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);'>");
strHTMLBuilder.Append("<thead><tr style='background-color: #009879;color: #ffffff;text-align: left;font-size: 1.2em;'>");
foreach (PropertyInfo prop in props)
{
if (prop.Name == "isDifferent") continue;
var ttl = prop.Name;
if (ttl.Substring(0, 1) == "_") ttl = ttl.Substring(1);
strHTMLBuilder.Append("<td style='padding: 7px 15px;'><b>");
strHTMLBuilder.Append(ttl);
strHTMLBuilder.Append("</b></td>");
}
strHTMLBuilder.Append("</tr></thead><tbody>");
foreach (T rec in records)
{
bool isDiff = false;
if (props[0].PropertyType == typeof(bool?))
{
isDiff = (bool)props[0].GetValue(rec, null)!;
}
if (isDiff)
{
strHTMLBuilder.Append("<tr style='background-color: #D7AB1F;color: #ffffff'>");
}
else
{
strHTMLBuilder.Append("<tr>");
}
var values = new object[props.Length];
for (int i = 1; i < props.Length; i++)
{
int number;
var val = props[i].GetValue(rec!, null) ?? "--";
// if val is a number, center it in the cell
if (int.TryParse(val.ToString(),out number)== true)
{
strHTMLBuilder.Append("<td style='padding: 7px 15px;text-align: center;'>");
}
else
{
strHTMLBuilder.Append("<td style='padding: 7px 15px;'>");
}
strHTMLBuilder.Append(val);
strHTMLBuilder.Append("</td>");
}
strHTMLBuilder.Append("</tr>");
}
//Close tags.
strHTMLBuilder.Append("</tbody></table>");
//strHTMLBuilder.Append("<a href= \"file://///jpfile01/#FILES//DATA_INTELLIGENCE_PEGA//data/P/archive/AG_NL_Claims_Data_BIXKPI_BIXExtract_Full_230413.xml\">Link to file</a>");
strHTMLBuilder.Append("</body>");
strHTMLBuilder.Append("</html>");
string Htmltext = strHTMLBuilder.ToString();
return Htmltext;
}
}
}