Added transaction event and mail sending for the transaction

This commit is contained in:
Baumgartner Marcel
2023-10-26 09:45:54 +02:00
parent 7a3d61c659
commit 4de6804f13
5 changed files with 38 additions and 7 deletions

View File

@@ -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; }
}

View File

@@ -11,4 +11,5 @@ public class Events
public static EventHandler<User> OnUserTotpSet; public static EventHandler<User> OnUserTotpSet;
public static EventHandler<MailVerificationEventArgs> OnUserMailVerify; public static EventHandler<MailVerificationEventArgs> OnUserMailVerify;
public static EventHandler<Service> OnServiceOrdered; public static EventHandler<Service> OnServiceOrdered;
public static EventHandler<TransactionCreatedEventArgs> OnTransactionCreated;
} }

View File

@@ -1,6 +1,7 @@
using Moonlight.App.Database.Entities; using Moonlight.App.Database.Entities;
using Moonlight.App.Database.Entities.Store; using Moonlight.App.Database.Entities.Store;
using Moonlight.App.Event; using Moonlight.App.Event;
using Moonlight.App.Event.Args;
namespace Moonlight.App.Services.Background; namespace Moonlight.App.Services.Background;
@@ -14,6 +15,18 @@ public class AutoMailSendService // This service is responsible for sending mail
Events.OnUserRegistered += OnUserRegistered; Events.OnUserRegistered += OnUserRegistered;
Events.OnServiceOrdered += OnServiceOrdered; 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) private async void OnServiceOrdered(object? _, Service service)

View File

@@ -1,5 +1,7 @@
using Moonlight.App.Database.Entities; using Moonlight.App.Database.Entities;
using Moonlight.App.Database.Entities.Store; using Moonlight.App.Database.Entities.Store;
using Moonlight.App.Event;
using Moonlight.App.Extensions;
using Moonlight.App.Helpers; using Moonlight.App.Helpers;
using Moonlight.App.Repositories; using Moonlight.App.Repositories;
@@ -14,16 +16,17 @@ public class TransactionService
UserRepository = userRepository; 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 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, Text = message,
Price = amount Price = amount
}); };
user.Transactions.Add(transaction);
UserRepository.Update(user); UserRepository.Update(user);
// We divide the call to ensure the transaction can be written to the database // 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 user.Balance = Math.Round(user.Balance, 2); // To prevent weird numbers
UserRepository.Update(user); UserRepository.Update(user);
return Task.CompletedTask; await Events.OnTransactionCreated.InvokeAsync(new()
{
Transaction = transaction,
User = user
});
} }
} }

View File

@@ -1,3 +1,3 @@
@page "/admin/store" @page "/admin/store"
<AdminStoreNavigation Index="0" /> <AdminStoreNavigation Index="0"/>