always send an email, at notifying some performer

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

@ -25,14 +25,14 @@ namespace Yavsc.Services
IConnexionManager _cxManager; IConnexionManager _cxManager;
public YavscMessageSender( public YavscMessageSender(
ILoggerFactory loggerFactory, ILoggerFactory loggerFactory,
IOptions<SiteSettings> sitesOptions, IOptions<SiteSettings> sitesOptions,
IOptions<SmtpSettings> smtpOptions, IOptions<SmtpSettings> smtpOptions,
IEmailSender emailSender, IEmailSender emailSender,
ApplicationDbContext dbContext, ApplicationDbContext dbContext,
IConnexionManager cxManager IConnexionManager cxManager
) )
{ {
_logger = loggerFactory.CreateLogger<MailSender>(); _logger = loggerFactory.CreateLogger<MailSender>();
_emailSender = emailSender; _emailSender = emailSender;
siteSettings = sitesOptions?.Value; siteSettings = sitesOptions?.Value;
@ -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,79 +52,87 @@ 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;
} }
var body = ev.CreateBody(); var body = ev.CreateBody();
var cxids = _cxManager.GetConnexionIds(user.UserName).ToArray();
_logger.LogDebug($"Sending to {user.UserName} <{user.Email}> : {body}");
if (cxids.Length==0) var mailSent = await _emailSender.SendEmailAsync(
user.UserName,
user.Email,
$"{ev.Sender} (un client) vous demande un rendez-vous",
body + Environment.NewLine
);
if (!mailSent.Sent)
{ {
_logger.LogDebug($"Sending to {user.UserName} <{user.Email}> : {body}"); result.message_id = mailSent.MessageId;
var mailSent = await _emailSender.SendEmailAsync( result.error = mailSent.ErrorMessage;
user.UserName, response.failure++;
user.Email, }
$"{ev.Sender} (un client) vous demande un rendez-vous", else
body+Environment.NewLine {
); result.message_id = mailSent.MessageId;
response.success++;
}
var cxids = _cxManager.GetConnexionIds(user.UserName).ToArray();
if (cxids.Length == 0)
{
_logger.LogDebug($"no cx to {user.UserName} <{user.Email}> ");
}
else
{
_logger.LogDebug($"Sending signal to {string.Join(" ", cxids)} : " + JsonConvert.SerializeObject(ev));
if (!mailSent.Sent) { foreach (var cxid in cxids)
result.message_id = mailSent.MessageId;
result.error = mailSent.ErrorMessage;
response.failure++;
}
else
{ {
result.message_id = mailSent.MessageId; var hubClient = hubContext.Clients.User(cxid);
response.success++; var data = new Dictionary<string, object>();
data["event"] = ev;
hubClient.push(ev.Topic, data);
} }
}
else { result.message_id = MimeKit.Utils.MimeUtils.GenerateMessageId(
_logger.LogDebug($"Sending signal to {string.Join(" ",cxids)} : "+JsonConvert.SerializeObject(ev));
foreach( var cxid in cxids) {
var hubClient = hubContext.Clients.User(cxid);
var data = new Dictionary<string,object>();
data["event"] = ev;
hubClient.push(ev.Topic, data );
}
result.message_id=MimeKit.Utils.MimeUtils.GenerateMessageId(
siteSettings.Authority siteSettings.Authority
); );
response.success++; response.success++;
} }
results.Add(result); results.Add(result);
@ -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); {
throw; _logger.LogError("Quelque chose s'est mal passé à l'envoi: " + ex.Message);
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);
} }
@ -158,7 +167,7 @@ namespace Yavsc.Services
{ {
return await NotifyEvent<IEvent>(userIds, yaev); return await NotifyEvent<IEvent>(userIds, yaev);
} }
/* SMS with Twilio: /* SMS with Twilio:
public Task SendSmsAsync(TwilioSettings twilioSettigns, string number, string message) public Task SendSmsAsync(TwilioSettings twilioSettigns, string number, string message)
{ {

Loading…