protected override async Task <OperationMessageHandlerResult> OnHandle(IDirectoryUserNotificationMessage message) { var culture = (message.Culture ?? "de-ch").ToLower(); var messageBuilder = new MailMessageBuilder( new MailAddress("*****@*****.**", "Microsoft AIALA"), new MailAddress(message.Receiver), new CultureInfo(culture)); switch (message) { case DirectoryAccountInvitedMessage invitedMessage: { var invitationLink = string.Format(_linkOptions.Value.ConfirmInvitation, culture, invitedMessage.Account.Id, WebUtility.UrlEncode(invitedMessage.Account.Invitation.ConfirmationToken)); messageBuilder.UseEncoding(Encoding.UTF8).CreateFromTemplate(new ConfirmInvitationMailTemplate(invitationLink)); break; } case DirectoryRegistrationConfirmationRequiredMessage registrationMessage: { var registrationLink = string.Format(_linkOptions.Value.ConfirmRegistration, culture, registrationMessage.RegistrationId, WebUtility.UrlEncode(registrationMessage.Token)); messageBuilder.UseEncoding(Encoding.UTF8).CreateFromTemplate(new ConfirmRegistrationMailTemplate(registrationLink)); break; } case DirectoryPasswordResetMessage passwordResetMessage: var resetPasswordLink = string.Format(_linkOptions.Value.ResetPassword, culture, WebUtility.UrlEncode(message.Receiver), WebUtility.UrlEncode(passwordResetMessage.Token)); messageBuilder.UseEncoding(Encoding.UTF8).CreateFromTemplate(new ResetPasswordMailTemplate(resetPasswordLink)); break; default: return(OperationMessageHandlerResult.Succeed()); } try { await _smtpMailService.SendMail(messageBuilder.Build()); return(OperationMessageHandlerResult.Succeed()); } catch (Exception ex) { return(OperationMessageHandlerResult.Fail(ex)); } }
protected override async Task <IOperationResult> Execute(EmergencyActivity input) { if (!ActivityTypeRanges.Emergency.Contains(input.Type)) { return(Invalid("Activity type is not an emergency activity type.")); } string eventName; switch (input.Type) { case ActivityType.EmergencyStart: eventName = MailMessages.EmergencyMailStartEventName; break; case ActivityType.EmergencyMood: eventName = MailMessages.EmergencyMailUpdateEventName; break; case ActivityType.EmergencyEnd: eventName = MailMessages.EmergencyMailEndEventName; break; default: throw new NotImplementedException(); } var previousActivities = await _dbContext.EmergencyActivities .Include(a => a.ActiveTask) .Where(a => a.EmergencyId == input.EmergencyId) .OrderBy(a => a.Timestamp) .ToListAsync(); var settings = await _settingsService.Load <TenantSettings>(input.Tenant.Id); var localTimeZone = TimeZoneInfo.FindSystemTimeZoneById(settings.TimeZoneId); var startActivity = input.Type == ActivityType.EmergencyStart ? input : previousActivities.Single(a => a.Type == ActivityType.EmergencyStart); var newActivityLocalTime = TimeZoneInfo.ConvertTime(input.Timestamp, localTimeZone); var startActivityLocalTime = TimeZoneInfo.ConvertTime(startActivity.Timestamp, localTimeZone); var baseMailText = MailMessages.EmergencyMailMessage .Replace("{time}", $"{newActivityLocalTime:t}") .Replace("{date}", $"{newActivityLocalTime:d}") .Replace("{eventName}", eventName) .Replace("{userName}", input.Tenant.Name); var subject = MailMessages.EmergencyMailSubject .Replace("{time}", $"{startActivityLocalTime:t}") .Replace("{date}", $"{startActivityLocalTime:d}"); if (startActivity.ActiveTask != null) { baseMailText = baseMailText.Replace("{taskBlock}", MailMessages.EmergencyTaskBlock.Replace("{taskName}", startActivity.ActiveTask.Name)); } else { baseMailText = baseMailText.Replace("{taskBlock}", ""); } if (previousActivities.Any()) { var protocol = "<ul style='list-style-type: none'>"; foreach (var activity in previousActivities) { var timestamp = TimeZoneInfo.ConvertTime(activity.Timestamp, localTimeZone); protocol += $"<li>({timestamp:T}) "; switch (activity.Type) { case ActivityType.EmergencyStart: protocol += MailMessages.EmergencyMailStartProtocolEntry; break; case ActivityType.EmergencyMood: protocol += GetMoodProtocolEntry(activity.ActivityData); break; case ActivityType.EmergencyEnd: protocol += MailMessages.EmergencyMailEndProtocolEntry; break; } protocol += "</li>"; } protocol += "</ul>"; baseMailText = baseMailText.Replace("{protocolBlock}", MailMessages.EmergencyProtocolBlock.Replace("{protocol}", protocol)); } else { baseMailText = baseMailText.Replace("{protocolBlock}", ""); } if (input.Latitude.HasValue && input.Longitude.HasValue) { var linkText = MailMessages.EmergencyMailLinkText .Replace("{link}", string.Format("https://www.google.com/maps/search/?api=1&query={0},{1}", input.Latitude, input.Longitude)); baseMailText = baseMailText.Replace("{linkText}", linkText); } else { baseMailText = baseMailText.Replace("{linkText}", ""); } var tenantSettings = await _settingsService.Load <TenantSettings>(input.Tenant.Id); foreach (var contactId in new[] { tenantSettings.EmergencyContact1Id, tenantSettings.EmergencyContact2Id }.Where(id => id != null)) { var account = await _dbContext.Accounts .Include(a => a.User) .FirstOrDefaultAsync(a => a.Id == contactId && a.Tenant.Id == input.Tenant.Id); if (account == null) { continue; } var mailBody = baseMailText.Replace("{recipientName}", $"{account.User.Firstname} {account.User.Lastname}"); var mail = new MailMessage(new MailAddress("*****@*****.**", "Microsoft AIALA"), new MailAddress(account.User.Email)) { BodyEncoding = Encoding.UTF8, SubjectEncoding = Encoding.UTF8, HeadersEncoding = Encoding.UTF8, IsBodyHtml = true, Subject = subject, Body = mailBody }; try { await _mailService.SendMail(mail); } catch (Exception ಠ_ಠ) { return(Failed(ಠ_ಠ.Message)); } } return(Succeeded()); }