< Summary - Syki

Information
Class: Syki.Back.Emails.EmailsService
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Emails/EmailsService.cs
Tag: 97_27801654829
Line coverage
0%
Covered lines: 0
Uncovered lines: 37
Coverable lines: 37
Total lines: 70
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
.ctor(...)100%210%
SendResetPasswordEmail()100%210%
SendFirstAccessMagicLinkEmail()100%210%
SendEmail()100%210%
LoadTemplate(...)100%210%

File(s)

/home/runner/work/syki/syki/Back/Emails/EmailsService.cs

#LineLine coverage
 1using System.Text.Json;
 2using System.Reflection;
 3using System.Collections.Concurrent;
 4
 5namespace Syki.Back.Emails;
 6
 7public class EmailsService : IEmailsService
 8{
 9    private readonly HttpClient _client;
 10    private readonly EmailSettings _settings;
 11    private readonly ILogger<EmailsService> _logger;
 012    private static readonly ConcurrentDictionary<string, string> Templates = new();
 013    private static readonly JsonSerializerOptions _logOptions = new() { WriteIndented = false };
 14
 015    public EmailsService(EmailSettings settings, IHttpClientFactory httpClientFactory, ILogger<EmailsService> logger)
 16    {
 017        _logger = logger;
 018        _settings = settings;
 019        _client = httpClientFactory.CreateClient();
 020        _client.BaseAddress = new Uri(_settings.ApiUrl);
 021        _client.DefaultRequestHeaders.Add("api-key", _settings.ApiKey);
 022    }
 23
 24    public async Task SendResetPasswordEmail(string to, string token)
 25    {
 026        var link = $"{_settings.FrontUrl}/reset-password?token={token}";
 027        await SendEmail("SendResetPasswordEmail", to, "Redefinição de senha", "ResetPassword.html", link);
 028    }
 29
 30    public async Task SendFirstAccessMagicLinkEmail(string to, string token)
 31    {
 032        var link = $"{_settings.FrontUrl}/magic-link?token={token}";
 033        await SendEmail("SendFirstAccessMagicLinkEmail", to, "Acesse sua conta", "FirstAccessMagicLink.html", link);
 034    }
 35
 36    private async Task SendEmail(string method, string to, string subject, string templateName, string link)
 37    {
 38        try
 39        {
 040            var content = LoadTemplate(templateName, link);
 041            var body = new BrevoEmailMessage(sender: "suporte@estud.com.br", to: to, subject: subject, content: content)
 42
 043            var bodyJson = JsonSerializer.Serialize(new { body.Sender, body.To, body.Subject }, _logOptions);
 44
 045            var response = await _client.PostAsJsonAsync("", body);
 046            var responseBody = await response.Content.ReadAsStringAsync();
 047        }
 048        catch (Exception ex)
 49        {
 050            _logger.LogError(ex, "[EmailsService] {Method} - exception sending to {To}", method, to);
 051        }
 052    }
 53
 54    private static string LoadTemplate(string name, string link)
 55    {
 056        var raw = Templates.GetOrAdd(name, static n =>
 057        {
 058            var assembly = Assembly.GetExecutingAssembly();
 059            var resourceNames = assembly.GetManifestResourceNames();
 060            var resourcePath = resourceNames.SingleOrDefault(str => str.EndsWith(n));
 061
 062            using var stream = assembly.GetManifestResourceStream(resourcePath)!;
 063            using var reader = new StreamReader(stream);
 064
 065            return reader.ReadToEnd();
 066        });
 67
 068        return raw.Replace("{{link}}", link);
 69    }
 70}