always send an email, at notifying some performer

main
Paul Schneider 6 years ago
parent d62caa048c
commit 53fc0e26a6
1 changed files with 62 additions and 53 deletions

@ -41,7 +41,7 @@ namespace Yavsc.Services
_cxManager = cxManager; _cxManager = cxManager;
} }
public async Task <MessageWithPayloadResponse> NotifyEvent<Event> public async Task<MessageWithPayloadResponse> NotifyEvent<Event>
(IEnumerable<string> userIds, Event ev) (IEnumerable<string> userIds, Event ev)
where Event : IEvent where Event : IEvent
{ {
@ -52,34 +52,38 @@ namespace Yavsc.Services
if (ev.Sender == null) if (ev.Sender == null)
throw new Exception("Spécifier un expéditeur"); throw new Exception("Spécifier un expéditeur");
if (userIds == null ) if (userIds == null)
throw new Exception("Notify e No user id"); throw new Exception("Notify e No user id");
MessageWithPayloadResponse response = new MessageWithPayloadResponse(); MessageWithPayloadResponse response = new MessageWithPayloadResponse();
var raa = userIds.ToArray(); var raa = userIds.ToArray();
if (raa.Length<1) if (raa.Length < 1)
throw new Exception("No dest id"); throw new Exception("No dest id");
try { try
{
List<MessageWithPayloadResponse.Result> results = new List<MessageWithPayloadResponse.Result>(); List<MessageWithPayloadResponse.Result> results = new List<MessageWithPayloadResponse.Result>();
foreach(var userId in raa) { foreach (var userId in raa)
{
MessageWithPayloadResponse.Result result = new MessageWithPayloadResponse.Result(); MessageWithPayloadResponse.Result result = new MessageWithPayloadResponse.Result();
result.registration_id = userId; result.registration_id = userId;
var user = _dbContext.Users.FirstOrDefault(u=> u.Id == userId); var user = _dbContext.Users.FirstOrDefault(u => u.Id == userId);
if (user==null) if (user == null)
{ {
response.failure++; response.failure++;
result.error = "no such user."; result.error = "no such user.";
continue; continue;
} }
if (!user.EmailConfirmed){ if (!user.EmailConfirmed)
{
response.failure++; response.failure++;
result.error = "user has not confirmed his email address."; result.error = "user has not confirmed his email address.";
continue; continue;
} }
if (user.Email==null){ if (user.Email == null)
{
response.failure++; response.failure++;
result.error = "user has no legacy email address."; result.error = "user has no legacy email address.";
continue; continue;
@ -87,19 +91,17 @@ namespace Yavsc.Services
var body = ev.CreateBody(); var body = ev.CreateBody();
var cxids = _cxManager.GetConnexionIds(user.UserName).ToArray();
if (cxids.Length==0)
{
_logger.LogDebug($"Sending to {user.UserName} <{user.Email}> : {body}"); _logger.LogDebug($"Sending to {user.UserName} <{user.Email}> : {body}");
var mailSent = await _emailSender.SendEmailAsync( var mailSent = await _emailSender.SendEmailAsync(
user.UserName, user.UserName,
user.Email, user.Email,
$"{ev.Sender} (un client) vous demande un rendez-vous", $"{ev.Sender} (un client) vous demande un rendez-vous",
body+Environment.NewLine body + Environment.NewLine
); );
if (!mailSent.Sent) { if (!mailSent.Sent)
{
result.message_id = mailSent.MessageId; result.message_id = mailSent.MessageId;
result.error = mailSent.ErrorMessage; result.error = mailSent.ErrorMessage;
response.failure++; response.failure++;
@ -109,19 +111,25 @@ namespace Yavsc.Services
result.message_id = mailSent.MessageId; result.message_id = mailSent.MessageId;
response.success++; response.success++;
} }
var cxids = _cxManager.GetConnexionIds(user.UserName).ToArray();
if (cxids.Length == 0)
{
_logger.LogDebug($"no cx to {user.UserName} <{user.Email}> ");
} }
else { else
_logger.LogDebug($"Sending signal to {string.Join(" ",cxids)} : "+JsonConvert.SerializeObject(ev)); {
_logger.LogDebug($"Sending signal to {string.Join(" ", cxids)} : " + JsonConvert.SerializeObject(ev));
foreach( var cxid in cxids) { foreach (var cxid in cxids)
{
var hubClient = hubContext.Clients.User(cxid); var hubClient = hubContext.Clients.User(cxid);
var data = new Dictionary<string,object>(); var data = new Dictionary<string, object>();
data["event"] = ev; data["event"] = ev;
hubClient.push(ev.Topic, data ); hubClient.push(ev.Topic, data);
} }
result.message_id=MimeKit.Utils.MimeUtils.GenerateMessageId( result.message_id = MimeKit.Utils.MimeUtils.GenerateMessageId(
siteSettings.Authority siteSettings.Authority
); );
@ -132,13 +140,14 @@ namespace Yavsc.Services
response.results = results.ToArray(); response.results = results.ToArray();
return response; return response;
} }
catch (Exception ex) { catch (Exception ex)
_logger.LogError("Quelque chose s'est mal passé à l'envoi: "+ex.Message); {
_logger.LogError("Quelque chose s'est mal passé à l'envoi: " + ex.Message);
throw; throw;
} }
} }
public async Task<MessageWithPayloadResponse> NotifyBookQueryAsync( IEnumerable<string> userIds, RdvQueryEvent ev) public async Task<MessageWithPayloadResponse> NotifyBookQueryAsync(IEnumerable<string> userIds, RdvQueryEvent ev)
{ {
return await NotifyEvent<RdvQueryEvent>(userIds, ev); return await NotifyEvent<RdvQueryEvent>(userIds, ev);
} }

Loading…