| | 1 | | using System.Text; |
| | 2 | | using System.Text.Json; |
| | 3 | | using LGDXRobotCloud.Data.Contracts; |
| | 4 | | using LGDXRobotCloud.Data.DbContexts; |
| | 5 | | using LGDXRobotCloud.Data.Models.Emails; |
| | 6 | | using LGDXRobotCloud.Utilities.Enums; |
| | 7 | | using LGDXRobotCloud.Utilities.Helpers; |
| | 8 | | using MassTransit; |
| | 9 | | using Microsoft.EntityFrameworkCore; |
| | 10 | |
|
| | 11 | | namespace LGDXRobotCloud.API.Services.Common; |
| | 12 | |
|
| | 13 | | public 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 | |
|
| 0 | 23 | | public sealed class EmailService( |
| 0 | 24 | | IBus bus, |
| 0 | 25 | | LgdxContext context |
| 0 | 26 | | ) : IEmailService |
| | 27 | | { |
| 0 | 28 | | private readonly IBus _bus = bus ?? throw new ArgumentNullException(nameof(bus)); |
| 0 | 29 | | private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 30 | |
|
| | 31 | | private async Task<List<EmailRecipient>> GetRecipientsAsync() |
| 0 | 32 | | { |
| 0 | 33 | | List<string> recipientIds = await _context.UserRoles.AsNoTracking() |
| 0 | 34 | | .Where(ur => ur.RoleId == LgdxRolesHelper.GetSystemRoleId(LgdxRoleType.EmailRecipient).ToString()) |
| 0 | 35 | | .Select(ur => ur.UserId).ToListAsync(); |
| 0 | 36 | | return await _context.Users.AsNoTracking() |
| 0 | 37 | | .Where(u => recipientIds.Contains(u.Id)) |
| 0 | 38 | | .Select(u => new EmailRecipient |
| 0 | 39 | | { |
| 0 | 40 | | Email = u.Email!, |
| 0 | 41 | | Name = u.Name! |
| 0 | 42 | | }) |
| 0 | 43 | | .ToListAsync() ?? []; |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | public async Task SendWelcomeEmailAsync(string recipientEmail, string recipientName, string userName) |
| 0 | 47 | | { |
| 0 | 48 | | var emailContract = new EmailContract |
| 0 | 49 | | { |
| 0 | 50 | | EmailType = EmailType.Welcome, |
| 0 | 51 | | Recipients = [new EmailRecipient |
| 0 | 52 | | { |
| 0 | 53 | | Email = recipientEmail, |
| 0 | 54 | | Name = recipientName |
| 0 | 55 | | }], |
| 0 | 56 | | Metadata = JsonSerializer.Serialize(new WelcomeViewModel { |
| 0 | 57 | | UserName = userName |
| 0 | 58 | | }) |
| 0 | 59 | | }; |
| 0 | 60 | | await _bus.Publish(emailContract); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | public async Task SendWellcomePasswordSetEmailAsync(string recipientEmail, string recipientName, string userName, stri |
| 0 | 64 | | { |
| 0 | 65 | | var emailContract = new EmailContract |
| 0 | 66 | | { |
| 0 | 67 | | EmailType = EmailType.WelcomePasswordSet, |
| 0 | 68 | | Recipients = [new EmailRecipient |
| 0 | 69 | | { |
| 0 | 70 | | Email = recipientEmail, |
| 0 | 71 | | Name = recipientName |
| 0 | 72 | | }], |
| 0 | 73 | | Metadata = JsonSerializer.Serialize(new WelcomePasswordSetViewModel { |
| 0 | 74 | | UserName = userName, |
| 0 | 75 | | Email = recipientEmail, |
| 0 | 76 | | Token = Convert.ToBase64String(Encoding.UTF8.GetBytes(token)) |
| 0 | 77 | | }) |
| 0 | 78 | | }; |
| 0 | 79 | | await _bus.Publish(emailContract); |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | public async Task SendPasswordResetEmailAsync(string recipientEmail, string recipientName, string userName, string tok |
| 0 | 83 | | { |
| 0 | 84 | | var emailContract = new EmailContract |
| 0 | 85 | | { |
| 0 | 86 | | EmailType = EmailType.PasswordReset, |
| 0 | 87 | | Recipients = [new EmailRecipient |
| 0 | 88 | | { |
| 0 | 89 | | Email = recipientEmail, |
| 0 | 90 | | Name = recipientName |
| 0 | 91 | | }], |
| 0 | 92 | | Metadata = JsonSerializer.Serialize(new PasswordResetViewModel{ |
| 0 | 93 | | UserName = userName, |
| 0 | 94 | | Email = recipientEmail, |
| 0 | 95 | | Token = Convert.ToBase64String(Encoding.UTF8.GetBytes(token)) |
| 0 | 96 | | }) |
| 0 | 97 | | }; |
| 0 | 98 | | await _bus.Publish(emailContract); |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public async Task SendPasswordUpdateEmailAsync(string recipientEmail, string recipientName, string userName) |
| 0 | 102 | | { |
| 0 | 103 | | var emailContract = new EmailContract |
| 0 | 104 | | { |
| 0 | 105 | | EmailType = EmailType.PasswordUpdate, |
| 0 | 106 | | Recipients = [new EmailRecipient |
| 0 | 107 | | { |
| 0 | 108 | | Email = recipientEmail, |
| 0 | 109 | | Name = recipientName |
| 0 | 110 | | }], |
| 0 | 111 | | Metadata = JsonSerializer.Serialize(new PasswordUpdateViewModel{ |
| 0 | 112 | | UserName = userName, |
| 0 | 113 | | Time = DateTime.Now.ToString("dd MMMM yyyy, hh:mm:ss tt") |
| 0 | 114 | | }) |
| 0 | 115 | | }; |
| 0 | 116 | | await _bus.Publish(emailContract); |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | public async Task SendRobotStuckEmailAsync(Guid robotId, double x, double y) |
| 0 | 120 | | { |
| 0 | 121 | | var recipients = await GetRecipientsAsync(); |
| 0 | 122 | | if (recipients.Count == 0) |
| 0 | 123 | | { |
| 0 | 124 | | return; |
| | 125 | | } |
| 0 | 126 | | var viewModel = await _context.Robots.AsNoTracking() |
| 0 | 127 | | .Where(r => r.Id == robotId) |
| 0 | 128 | | .Include(r => r.Realm) |
| 0 | 129 | | .Select(r => new RobotStuckViewModel { |
| 0 | 130 | | RobotId = r.Id.ToString(), |
| 0 | 131 | | RobotName = r.Name, |
| 0 | 132 | | RealmId = r.Realm.Id.ToString(), |
| 0 | 133 | | RealmName = r.Realm.Name, |
| 0 | 134 | | Time = DateTime.Now.ToString("dd MMMM yyyy, hh:mm:ss tt"), |
| 0 | 135 | | X = @Math.Round(x, 4).ToString(), |
| 0 | 136 | | Y = @Math.Round(y, 4).ToString() |
| 0 | 137 | | }) |
| 0 | 138 | | .FirstOrDefaultAsync(); |
| 0 | 139 | | if (viewModel != null) |
| 0 | 140 | | { |
| 0 | 141 | | var emailContract = new EmailContract |
| 0 | 142 | | { |
| 0 | 143 | | EmailType = EmailType.RobotStuck, |
| 0 | 144 | | Recipients = recipients, |
| 0 | 145 | | Metadata = JsonSerializer.Serialize(viewModel) |
| 0 | 146 | | }; |
| 0 | 147 | | await _bus.Publish(emailContract); |
| 0 | 148 | | } |
| 0 | 149 | | } |
| | 150 | |
|
| | 151 | | public async Task SendAutoTaskAbortEmailAsync(Guid robotId, int taskId, AutoTaskAbortReason autoTaskAbortReason) |
| 0 | 152 | | { |
| 0 | 153 | | var recipients = await GetRecipientsAsync(); |
| 0 | 154 | | if (recipients.Count == 0) |
| 0 | 155 | | { |
| 0 | 156 | | return; |
| | 157 | | } |
| 0 | 158 | | var viewModel = await _context.AutoTasks.AsNoTracking() |
| 0 | 159 | | .Where(at => at.Id == taskId) |
| 0 | 160 | | .Include(at => at.AssignedRobot) |
| 0 | 161 | | .ThenInclude(r => r!.Realm) |
| 0 | 162 | | .Select(at => new AutoTaskAbortViewModel { |
| 0 | 163 | | AutoTaskId = at.Id.ToString(), |
| 0 | 164 | | AutoTaskName = at.Name ?? string.Empty, |
| 0 | 165 | | AbortReason = ((int)autoTaskAbortReason).ToString(), |
| 0 | 166 | | RobotId = at.AssignedRobot!.Id.ToString(), |
| 0 | 167 | | RobotName = at.AssignedRobot.Name, |
| 0 | 168 | | RealmId = at.AssignedRobot.Realm.Id.ToString(), |
| 0 | 169 | | RealmName = at.AssignedRobot.Realm.Name, |
| 0 | 170 | | Time = DateTime.Now.ToString("dd MMMM yyyy, hh:mm:ss tt") |
| 0 | 171 | | }) |
| 0 | 172 | | .FirstOrDefaultAsync(); |
| 0 | 173 | | if (viewModel != null) |
| 0 | 174 | | { |
| 0 | 175 | | var emailContract = new EmailContract |
| 0 | 176 | | { |
| 0 | 177 | | EmailType = EmailType.AutoTaskAbort, |
| 0 | 178 | | Recipients = recipients, |
| 0 | 179 | | Metadata = JsonSerializer.Serialize(viewModel) |
| 0 | 180 | | }; |
| 0 | 181 | | await _bus.Publish(emailContract); |
| 0 | 182 | | } |
| 0 | 183 | | } |
| | 184 | | } |