Add whitelisting service + update the jwt valid audience
This commit is contained in:
111
src/Managing.Application/Whitelist/WhitelistService.cs
Normal file
111
src/Managing.Application/Whitelist/WhitelistService.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Domain.Whitelist;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Managing.Application.Whitelist;
|
||||
|
||||
public class WhitelistService : IWhitelistService
|
||||
{
|
||||
private readonly IWhitelistRepository _whitelistRepository;
|
||||
private readonly ILogger<WhitelistService> _logger;
|
||||
|
||||
public WhitelistService(
|
||||
IWhitelistRepository whitelistRepository,
|
||||
ILogger<WhitelistService> logger)
|
||||
{
|
||||
_whitelistRepository = whitelistRepository;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<(IEnumerable<WhitelistAccount> Accounts, int TotalCount)> GetPaginatedAsync(
|
||||
int pageNumber,
|
||||
int pageSize,
|
||||
string? searchExternalEthereumAccount = null,
|
||||
string? searchTwitterAccount = null)
|
||||
{
|
||||
if (pageNumber < 1)
|
||||
{
|
||||
throw new ArgumentException("Page number must be greater than 0", nameof(pageNumber));
|
||||
}
|
||||
|
||||
if (pageSize < 1 || pageSize > 100)
|
||||
{
|
||||
throw new ArgumentException("Page size must be between 1 and 100", nameof(pageSize));
|
||||
}
|
||||
|
||||
return await _whitelistRepository.GetPaginatedAsync(
|
||||
pageNumber,
|
||||
pageSize,
|
||||
searchExternalEthereumAccount,
|
||||
searchTwitterAccount);
|
||||
}
|
||||
|
||||
public async Task<int> SetIsWhitelistedAsync(IEnumerable<int> accountIds, bool isWhitelisted)
|
||||
{
|
||||
var idsList = accountIds?.ToList() ?? new List<int>();
|
||||
|
||||
if (!idsList.Any())
|
||||
{
|
||||
throw new ArgumentException("At least one account ID must be provided", nameof(accountIds));
|
||||
}
|
||||
|
||||
_logger.LogInformation("Setting IsWhitelisted to {IsWhitelisted} for {Count} account(s)",
|
||||
isWhitelisted, idsList.Count);
|
||||
|
||||
var updatedCount = await _whitelistRepository.SetIsWhitelistedAsync(idsList, isWhitelisted);
|
||||
|
||||
_logger.LogInformation("Successfully updated {Count} account(s) to IsWhitelisted = {IsWhitelisted}",
|
||||
updatedCount, isWhitelisted);
|
||||
|
||||
return updatedCount;
|
||||
}
|
||||
|
||||
public async Task<WhitelistAccount?> GetByIdAsync(int id)
|
||||
{
|
||||
return await _whitelistRepository.GetByIdAsync(id);
|
||||
}
|
||||
|
||||
public async Task<WhitelistAccount> ProcessPrivyWebhookAsync(
|
||||
string privyUserId,
|
||||
long privyCreatedAt,
|
||||
string walletAddress,
|
||||
string? externalEthereumAccount,
|
||||
string? twitterAccount)
|
||||
{
|
||||
_logger.LogInformation("Processing Privy webhook - PrivyId: {PrivyId}, Wallet: {Wallet}, ExternalEthereum: {ExternalEthereum}, Twitter: {Twitter}",
|
||||
privyUserId, walletAddress, externalEthereumAccount ?? "null", twitterAccount ?? "null");
|
||||
|
||||
// Convert Unix timestamp to DateTime
|
||||
var privyCreationDate = DateTimeOffset.FromUnixTimeSeconds(privyCreatedAt).DateTime;
|
||||
|
||||
// Check if account already exists
|
||||
var existing = await _whitelistRepository.GetByPrivyIdAsync(privyUserId) ??
|
||||
await _whitelistRepository.GetByEmbeddedWalletAsync(walletAddress);
|
||||
|
||||
var whitelistAccount = new WhitelistAccount
|
||||
{
|
||||
PrivyId = privyUserId,
|
||||
PrivyCreationDate = privyCreationDate,
|
||||
EmbeddedWallet = walletAddress,
|
||||
ExternalEthereumAccount = externalEthereumAccount,
|
||||
TwitterAccount = twitterAccount,
|
||||
IsWhitelisted = false, // Default to false, admin will set to true later
|
||||
CreatedAt = existing?.CreatedAt ?? DateTime.UtcNow
|
||||
};
|
||||
|
||||
// If existing, preserve the ID
|
||||
if (existing != null)
|
||||
{
|
||||
whitelistAccount.Id = existing.Id;
|
||||
}
|
||||
|
||||
var result = await _whitelistRepository.CreateOrUpdateAsync(whitelistAccount);
|
||||
|
||||
_logger.LogInformation("Privy webhook processed successfully - AccountId: {Id}, PrivyId: {PrivyId}",
|
||||
result.Id, result.PrivyId);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user