< 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: 147
Coverable lines: 147
Total lines: 188
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.Globalization;
 2using System.Text;
 3using System.Text.Json;
 4using LGDXRobotCloud.Data.Contracts;
 5using LGDXRobotCloud.Data.DbContexts;
 6using LGDXRobotCloud.Data.Models.Emails;
 7using LGDXRobotCloud.Utilities.Enums;
 8using LGDXRobotCloud.Utilities.Helpers;
 9using MassTransit;
 10using Microsoft.EntityFrameworkCore;
 11
 12namespace LGDXRobotCloud.API.Services.Common;
 13
 14public interface IEmailService
 15{
 16  Task SendWelcomeEmailAsync(string recipientEmail, string recipientName, string userName);
 17  Task SendWellcomePasswordSetEmailAsync(string recipientEmail, string recipientName, string userName, string token);
 18  Task SendPasswordResetEmailAsync(string recipientEmail, string recipientName, string userName, string token);
 19  Task SendPasswordUpdateEmailAsync(string recipientEmail, string recipientName, string userName);
 20  Task SendRobotStuckEmailAsync(Guid robotId, double x, double y);
 21  Task SendAutoTaskAbortEmailAsync(int taskId, AutoTaskAbortReason autoTaskAbortReason);
 22}
 23
 024public sealed class EmailService(
 025    IBus bus,
 026    LgdxContext context
 027  ) : IEmailService
 28{
 029  private readonly IBus _bus = bus ?? throw new ArgumentNullException(nameof(bus));
 030  private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context));
 31
 32  private async Task<List<EmailRecipient>> GetRecipientsAsync()
 033  {
 034    List<string> recipientIds = await _context.UserRoles.AsNoTracking()
 035      .Where(ur => ur.RoleId == LgdxRolesHelper.GetSystemRoleId(LgdxRoleType.EmailRecipient).ToString())
 036      .Select(ur => ur.UserId).ToListAsync();
 037    return await _context.Users.AsNoTracking()
 038      .Where(u => recipientIds.Contains(u.Id))
 039      .Select(u => new EmailRecipient
 040      {
 041        Email = u.Email!,
 042        Name = u.Name!
 043      })
 044      .ToListAsync() ?? [];
 045  }
 46
 47  public async Task SendWelcomeEmailAsync(string recipientEmail, string recipientName, string userName)
 048  {
 049    var emailContract = new EmailContract
 050    {
 051      EmailType = EmailType.Welcome,
 052      Recipients = [new EmailRecipient
 053      {
 054        Email = recipientEmail,
 055        Name = recipientName
 056      }],
 057      Metadata = JsonSerializer.Serialize(new WelcomeViewModel {
 058        UserName = userName
 059      })
 060    };
 061    await _bus.Publish(emailContract);
 062  }
 63
 64  public async Task SendWellcomePasswordSetEmailAsync(string recipientEmail, string recipientName, string userName, stri
 065  {
 066    var emailContract = new EmailContract
 067    {
 068      EmailType = EmailType.WelcomePasswordSet,
 069      Recipients = [new EmailRecipient
 070      {
 071        Email = recipientEmail,
 072        Name = recipientName
 073      }],
 074      Metadata = JsonSerializer.Serialize(new WelcomePasswordSetViewModel {
 075        UserName = userName,
 076        Email = recipientEmail,
 077        Token = Convert.ToBase64String(Encoding.UTF8.GetBytes(token))
 078      })
 079    };
 080    await _bus.Publish(emailContract);
 081  }
 82
 83  public async Task SendPasswordResetEmailAsync(string recipientEmail, string recipientName, string userName, string tok
 084  {
 085    var emailContract = new EmailContract
 086    {
 087      EmailType = EmailType.PasswordReset,
 088      Recipients = [new EmailRecipient
 089      {
 090        Email = recipientEmail,
 091        Name = recipientName
 092      }],
 093      Metadata = JsonSerializer.Serialize(new PasswordResetViewModel{
 094        UserName = userName,
 095        Email = recipientEmail,
 096        Token = Convert.ToBase64String(Encoding.UTF8.GetBytes(token))
 097      })
 098    };
 099    await _bus.Publish(emailContract);
 0100  }
 101
 102  public async Task SendPasswordUpdateEmailAsync(string recipientEmail, string recipientName, string userName)
 0103  {
 0104    string currentTime = DateTime.Now.ToString(CultureInfo.CurrentCulture);
 0105    var emailContract = new EmailContract
 0106    {
 0107      EmailType = EmailType.PasswordUpdate,
 0108      Recipients = [new EmailRecipient
 0109      {
 0110        Email = recipientEmail,
 0111        Name = recipientName
 0112      }],
 0113      Metadata = JsonSerializer.Serialize(new PasswordUpdateViewModel{
 0114        UserName = userName,
 0115        Time = currentTime
 0116      })
 0117    };
 0118    await _bus.Publish(emailContract);
 0119  }
 120
 121  public async Task SendRobotStuckEmailAsync(Guid robotId, double x, double y)
 0122  {
 0123    var recipients = await GetRecipientsAsync();
 0124    if (recipients.Count == 0)
 0125    {
 0126      return;
 127    }
 0128    string currentTime = DateTime.Now.ToString(CultureInfo.CurrentCulture);
 0129    var viewModel = await _context.Robots.AsNoTracking()
 0130      .Where(r => r.Id == robotId)
 0131      .Include(r => r.Realm)
 0132      .Select(r => new RobotStuckViewModel {
 0133          RobotId = r.Id.ToString(),
 0134          RobotName = r.Name,
 0135          RealmId = r.Realm.Id.ToString(),
 0136          RealmName = r.Realm.Name,
 0137          Time = currentTime,
 0138          X = @Math.Round(x, 4).ToString(),
 0139          Y = @Math.Round(y, 4).ToString()
 0140        })
 0141      .FirstOrDefaultAsync();
 0142    if (viewModel != null)
 0143    {
 0144      var emailContract = new EmailContract
 0145      {
 0146        EmailType = EmailType.RobotStuck,
 0147        Recipients = recipients,
 0148        Metadata = JsonSerializer.Serialize(viewModel)
 0149      };
 0150      await _bus.Publish(emailContract);
 0151    }
 0152  }
 153
 154  public async Task SendAutoTaskAbortEmailAsync(int taskId, AutoTaskAbortReason autoTaskAbortReason)
 0155  {
 0156    var recipients = await GetRecipientsAsync();
 0157    if (recipients.Count == 0)
 0158    {
 0159      return;
 160    }
 0161    string currentTime = DateTime.Now.ToString(CultureInfo.CurrentCulture);
 0162    var viewModel = await _context.AutoTasks.AsNoTracking()
 0163      .Where(at => at.Id == taskId)
 0164      .Include(at => at.AssignedRobot)
 0165      .ThenInclude(r => r!.Realm)
 0166      .Select(at => new AutoTaskAbortViewModel {
 0167          AutoTaskId = at.Id.ToString(),
 0168          AutoTaskName = at.Name ?? string.Empty,
 0169          AbortReason = ((int)autoTaskAbortReason).ToString(),
 0170          RobotId = at.AssignedRobot != null ? at.AssignedRobot.Id.ToString() : string.Empty,
 0171          RobotName = at.AssignedRobot != null ? at.AssignedRobot.Name : string.Empty,
 0172          RealmId = at.Realm.Id.ToString(),
 0173          RealmName = at.Realm.Name,
 0174          Time = currentTime
 0175        })
 0176      .FirstOrDefaultAsync();
 0177      if (viewModel != null)
 0178      {
 0179        var emailContract = new EmailContract
 0180        {
 0181          EmailType = EmailType.AutoTaskAbort,
 0182          Recipients = recipients,
 0183          Metadata = JsonSerializer.Serialize(viewModel)
 0184        };
 0185        await _bus.Publish(emailContract);
 0186      }
 0187  }
 188}