diff --git a/Moonlight/App/Event/Args/TransactionCreatedEventArgs.cs b/Moonlight/App/Event/Args/TransactionCreatedEventArgs.cs new file mode 100644 index 0000000..585791c --- /dev/null +++ b/Moonlight/App/Event/Args/TransactionCreatedEventArgs.cs @@ -0,0 +1,10 @@ +using Moonlight.App.Database.Entities; +using Moonlight.App.Database.Entities.Store; + +namespace Moonlight.App.Event.Args; + +public class TransactionCreatedEventArgs +{ + public Transaction Transaction { get; set; } + public User User { get; set; } +} \ No newline at end of file diff --git a/Moonlight/App/Event/Events.cs b/Moonlight/App/Event/Events.cs index ed42ae1..35d822e 100644 --- a/Moonlight/App/Event/Events.cs +++ b/Moonlight/App/Event/Events.cs @@ -11,4 +11,5 @@ public class Events public static EventHandler OnUserTotpSet; public static EventHandler OnUserMailVerify; public static EventHandler OnServiceOrdered; + public static EventHandler OnTransactionCreated; } \ No newline at end of file diff --git a/Moonlight/App/Services/Background/AutoMailSendService.cs b/Moonlight/App/Services/Background/AutoMailSendService.cs index ddf833b..12a3f4c 100644 --- a/Moonlight/App/Services/Background/AutoMailSendService.cs +++ b/Moonlight/App/Services/Background/AutoMailSendService.cs @@ -1,6 +1,7 @@ using Moonlight.App.Database.Entities; using Moonlight.App.Database.Entities.Store; using Moonlight.App.Event; +using Moonlight.App.Event.Args; namespace Moonlight.App.Services.Background; @@ -14,6 +15,18 @@ public class AutoMailSendService // This service is responsible for sending mail Events.OnUserRegistered += OnUserRegistered; Events.OnServiceOrdered += OnServiceOrdered; + Events.OnTransactionCreated += OnTransactionCreated; + } + + private async void OnTransactionCreated(object? sender, TransactionCreatedEventArgs eventArgs) + { + await MailService.Send( + eventArgs.User, + "New transaction", + "transactionCreated", + eventArgs.Transaction, + eventArgs.User + ); } private async void OnServiceOrdered(object? _, Service service) diff --git a/Moonlight/App/Services/Store/TransactionService.cs b/Moonlight/App/Services/Store/TransactionService.cs index 27c78f8..b514d2d 100644 --- a/Moonlight/App/Services/Store/TransactionService.cs +++ b/Moonlight/App/Services/Store/TransactionService.cs @@ -1,5 +1,7 @@ using Moonlight.App.Database.Entities; using Moonlight.App.Database.Entities.Store; +using Moonlight.App.Event; +using Moonlight.App.Extensions; using Moonlight.App.Helpers; using Moonlight.App.Repositories; @@ -14,16 +16,17 @@ public class TransactionService UserRepository = userRepository; } - public Task Add(User u, double amount, string message) + public async Task Add(User u, double amount, string message) { var user = UserRepository.Get().First(x => x.Id == u.Id); // Load user with current repo - - user.Transactions.Add(new Transaction() + + var transaction = new Transaction() { Text = message, Price = amount - }); + }; + user.Transactions.Add(transaction); UserRepository.Update(user); // We divide the call to ensure the transaction can be written to the database @@ -32,7 +35,11 @@ public class TransactionService user.Balance = Math.Round(user.Balance, 2); // To prevent weird numbers UserRepository.Update(user); - - return Task.CompletedTask; + + await Events.OnTransactionCreated.InvokeAsync(new() + { + Transaction = transaction, + User = user + }); } } \ No newline at end of file diff --git a/Moonlight/Shared/Views/Admin/Store/Index.razor b/Moonlight/Shared/Views/Admin/Store/Index.razor index e95615c..ae18b68 100644 --- a/Moonlight/Shared/Views/Admin/Store/Index.razor +++ b/Moonlight/Shared/Views/Admin/Store/Index.razor @@ -1,3 +1,3 @@ @page "/admin/store" - \ No newline at end of file + \ No newline at end of file