< Summary

Information
Class: LGDXRobotCloud.API.Services.Common.EmailService
Assembly: LGDXRobotCloud.API
File(s): /builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Services/Common/EmailService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 144
Coverable lines: 144
Total lines: 184
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)0%2040%
GetRecipientsAsync()0%620%
SendWelcomeEmailAsync()100%210%
SendWellcomePasswordSetEmailAsync()100%210%
SendPasswordResetEmailAsync()100%210%
SendPasswordUpdateEmailAsync()100%210%
SendRobotStuckEmailAsync()0%2040%
SendAutoTaskAbortEmailAsync()0%2040%

File(s)

/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Services/Common/EmailService.cs

#LineLine coverage
 1using System.Text;
 2using System.Text.Json;
 3using LGDXRobotCloud.Data.Contracts;
 4using LGDXRobotCloud.Data.DbContexts;
 5using LGDXRobotCloud.Data.Models.Emails;
 6using LGDXRobotCloud.Utilities.Enums;
 7using LGDXRobotCloud.Utilities.Helpers;
 8using MassTransit;
 9using Microsoft.EntityFrameworkCore;
 10
 11namespace LGDXRobotCloud.API.Services.Common;
 12
 13public interface IEmailService
 14{
 15  Task SendWelcomeEmailAsync(string recipientEmail, string recipientName, string userName);
 16  Task SendWellcomePasswordSetEmailAsync(string recipientEmail, string recipientName, string userName, string token);
 17  Task SendPasswordResetEmailAsync(string recipientEmail, string recipientName, string userName, string token);
 18  Task SendPasswordUpdateEmailAsync(string recipientEmail, string recipientName, string userName);
 19  Task SendRobotStuckEmailAsync(Guid robotId, double x, double y);
 20  Task SendAutoTaskAbortEmailAsync(Guid robotId, int taskId, AutoTaskAbortReason autoTaskAbortReason);
 21}
 22
 023public sealed class EmailService(
 024    IBus bus,
 025    LgdxContext context
 026  ) : IEmailService
 27{
 028  private readonly IBus _bus = bus ?? throw new ArgumentNullException(nameof(bus));
 029  private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context));
 30
 31  private async Task<List<EmailRecipient>> GetRecipientsAsync()
 032  {
 033    List<string> recipientIds = await _context.UserRoles.AsNoTracking()
 034      .Where(ur => ur.RoleId == LgdxRolesHelper.GetSystemRoleId(LgdxRoleType.EmailRecipient).ToString())
 035      .Select(ur => ur.UserId).ToListAsync();
 036    return await _context.Users.AsNoTracking()
 037      .Where(u => recipientIds.Contains(u.Id))
 038      .Select(u => new EmailRecipient
 039      {
 040        Email = u.Email!,
 041        Name = u.Name!
 042      })
 043      .ToListAsync() ?? [];
 044  }
 45
 46  public async Task SendWelcomeEmailAsync(string recipientEmail, string recipientName, string userName)
 047  {
 048    var emailContract = new EmailContract
 049    {
 050      EmailType = EmailType.Welcome,
 051      Recipients = [new EmailRecipient
 052      {
 053        Email = recipientEmail,
 054        Name = recipientName
 055      }],
 056      Metadata = JsonSerializer.Serialize(new WelcomeViewModel {
 057        UserName = userName
 058      })
 059    };
 060    await _bus.Publish(emailContract);
 061  }
 62
 63  public async Task SendWellcomePasswordSetEmailAsync(string recipientEmail, string recipientName, string userName, stri
 064  {
 065    var emailContract = new EmailContract
 066    {
 067      EmailType = EmailType.WelcomePasswordSet,
 068      Recipients = [new EmailRecipient
 069      {
 070        Email = recipientEmail,
 071        Name = recipientName
 072      }],
 073      Metadata = JsonSerializer.Serialize(new WelcomePasswordSetViewModel {
 074        UserName = userName,
 075        Email = recipientEmail,
 076        Token = Convert.ToBase64String(Encoding.UTF8.GetBytes(token))
 077      })
 078    };
 079    await _bus.Publish(emailContract);
 080  }
 81
 82  public async Task SendPasswordResetEmailAsync(string recipientEmail, string recipientName, string userName, string tok
 083  {
 084    var emailContract = new EmailContract
 085    {
 086      EmailType = EmailType.PasswordReset,
 087      Recipients = [new EmailRecipient
 088      {
 089        Email = recipientEmail,
 090        Name = recipientName
 091      }],
 092      Metadata = JsonSerializer.Serialize(new PasswordResetViewModel{
 093        UserName = userName,
 094        Email = recipientEmail,
 095        Token = Convert.ToBase64String(Encoding.UTF8.GetBytes(token))
 096      })
 097    };
 098    await _bus.Publish(emailContract);
 099  }
 100
 101  public async Task SendPasswordUpdateEmailAsync(string recipientEmail, string recipientName, string userName)
 0102  {
 0103    var emailContract = new EmailContract
 0104    {
 0105      EmailType = EmailType.PasswordUpdate,
 0106      Recipients = [new EmailRecipient
 0107      {
 0108        Email = recipientEmail,
 0109        Name = recipientName
 0110      }],
 0111      Metadata = JsonSerializer.Serialize(new PasswordUpdateViewModel{
 0112        UserName = userName,
 0113        Time = DateTime.Now.ToString("dd MMMM yyyy, hh:mm:ss tt")
 0114      })
 0115    };
 0116    await _bus.Publish(emailContract);
 0117  }
 118
 119  public async Task SendRobotStuckEmailAsync(Guid robotId, double x, double y)
 0120  {
 0121    var recipients = await GetRecipientsAsync();
 0122    if (recipients.Count == 0)
 0123    {
 0124      return;
 125    }
 0126    var viewModel = await _context.Robots.AsNoTracking()
 0127      .Where(r => r.Id == robotId)
 0128      .Include(r => r.Realm)
 0129      .Select(r => new RobotStuckViewModel {
 0130          RobotId = r.Id.ToString(),
 0131          RobotName = r.Name,
 0132          RealmId = r.Realm.Id.ToString(),
 0133          RealmName = r.Realm.Name,
 0134          Time = DateTime.Now.ToString("dd MMMM yyyy, hh:mm:ss tt"),
 0135          X = @Math.Round(x, 4).ToString(),
 0136          Y = @Math.Round(y, 4).ToString()
 0137        })
 0138      .FirstOrDefaultAsync();
 0139    if (viewModel != null)
 0140    {
 0141      var emailContract = new EmailContract
 0142      {
 0143        EmailType = EmailType.RobotStuck,
 0144        Recipients = recipients,
 0145        Metadata = JsonSerializer.Serialize(viewModel)
 0146      };
 0147      await _bus.Publish(emailContract);
 0148    }
 0149  }
 150
 151  public async Task SendAutoTaskAbortEmailAsync(Guid robotId, int taskId, AutoTaskAbortReason autoTaskAbortReason)
 0152  {
 0153    var recipients = await GetRecipientsAsync();
 0154    if (recipients.Count == 0)
 0155    {
 0156      return;
 157    }
 0158    var viewModel = await _context.AutoTasks.AsNoTracking()
 0159      .Where(at => at.Id == taskId)
 0160      .Include(at => at.AssignedRobot)
 0161      .ThenInclude(r => r!.Realm)
 0162      .Select(at => new AutoTaskAbortViewModel {
 0163          AutoTaskId = at.Id.ToString(),
 0164          AutoTaskName = at.Name ?? string.Empty,
 0165          AbortReason = ((int)autoTaskAbortReason).ToString(),
 0166          RobotId = at.AssignedRobot!.Id.ToString(),
 0167          RobotName = at.AssignedRobot.Name,
 0168          RealmId = at.AssignedRobot.Realm.Id.ToString(),
 0169          RealmName = at.AssignedRobot.Realm.Name,
 0170          Time = DateTime.Now.ToString("dd MMMM yyyy, hh:mm:ss tt")
 0171        })
 0172      .FirstOrDefaultAsync();
 0173      if (viewModel != null)
 0174      {
 0175        var emailContract = new EmailContract
 0176        {
 0177          EmailType = EmailType.AutoTaskAbort,
 0178          Recipients = recipients,
 0179          Metadata = JsonSerializer.Serialize(viewModel)
 0180        };
 0181        await _bus.Publish(emailContract);
 0182      }
 0183  }
 184}