| | | 1 | | using System.Reflection; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | |
| | | 4 | | namespace Syki.Back.Emails; |
| | | 5 | | |
| | | 6 | | public class EmailsService : IEmailsService |
| | | 7 | | { |
| | | 8 | | private readonly HttpClient _client; |
| | | 9 | | private readonly EmailSettings _settings; |
| | 0 | 10 | | private static readonly ConcurrentDictionary<string, string> Templates = new(); |
| | | 11 | | |
| | 0 | 12 | | public EmailsService(EmailSettings settings) |
| | | 13 | | { |
| | 0 | 14 | | _settings = settings; |
| | 0 | 15 | | _client = new HttpClient { BaseAddress = new Uri(_settings.ApiUrl) }; |
| | 0 | 16 | | _client.DefaultRequestHeaders.Add("api-key", _settings.ApiKey); |
| | 0 | 17 | | } |
| | | 18 | | |
| | | 19 | | public async Task SendResetPasswordEmail(string to, string token) |
| | | 20 | | { |
| | 0 | 21 | | var link = $"{_settings.FrontUrl}/reset-password?token={token}"; |
| | | 22 | | |
| | 0 | 23 | | var body = new BrevoEmailMessage( |
| | 0 | 24 | | sender: "suporte@syki.com", |
| | 0 | 25 | | to: to, |
| | 0 | 26 | | subject: "Syki - Redefinição de senha", |
| | 0 | 27 | | content: LoadTemplate("ResetPassword.html", link) |
| | 0 | 28 | | ); |
| | | 29 | | |
| | 0 | 30 | | await _client.PostAsJsonAsync("", body); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | public async Task SendFirstAccessMagicLinkEmail(string to, string token) |
| | | 34 | | { |
| | 0 | 35 | | var link = $"{_settings.FrontUrl}/magic-link?token={token}"; |
| | | 36 | | |
| | 0 | 37 | | var body = new BrevoEmailMessage( |
| | 0 | 38 | | sender: "suporte@syki.com", |
| | 0 | 39 | | to: to, |
| | 0 | 40 | | subject: "Syki - Acesse sua conta", |
| | 0 | 41 | | content: LoadTemplate("FirstAccessMagicLink.html", link) |
| | 0 | 42 | | ); |
| | | 43 | | |
| | 0 | 44 | | await _client.PostAsJsonAsync("", body); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | private static string LoadTemplate(string name, string link) |
| | | 48 | | { |
| | 0 | 49 | | var raw = Templates.GetOrAdd(name, static n => |
| | 0 | 50 | | { |
| | 0 | 51 | | var assembly = Assembly.GetExecutingAssembly(); |
| | 0 | 52 | | var resourcePath = assembly.GetManifestResourceNames().Single(str => str.EndsWith(n)); |
| | 0 | 53 | | |
| | 0 | 54 | | using var stream = assembly.GetManifestResourceStream(resourcePath)!; |
| | 0 | 55 | | using var reader = new StreamReader(stream); |
| | 0 | 56 | | |
| | 0 | 57 | | return reader.ReadToEnd(); |
| | 0 | 58 | | }); |
| | | 59 | | |
| | 0 | 60 | | return raw.Replace("{{link}}", link); |
| | | 61 | | } |
| | | 62 | | } |