Feat Varios 4

This commit is contained in:
2026-01-06 14:20:44 -03:00
parent 9fa21ebec3
commit c6496099bd
16 changed files with 613 additions and 208 deletions

View File

@@ -22,7 +22,8 @@ public class ClientRepository
Id,
ISNULL(BillingName, Username) as Name,
ISNULL(BillingTaxId, '') as DniOrCuit,
Email, Phone, BillingAddress as Address
Email, Phone, BillingAddress as Address, BillingTaxType as TaxType,
Username, IsActive, Role
FROM Users
WHERE BillingName LIKE @Query OR BillingTaxId LIKE @Query OR Username LIKE @Query
ORDER BY BillingName";
@@ -54,23 +55,34 @@ public class ClientRepository
}
}
// Obtener todos con estadísticas desde Users
public async Task<IEnumerable<dynamic>> GetAllWithStatsAsync()
// Obtener con estadísticas desde Users (con filtro y límite para performance)
public async Task<IEnumerable<dynamic>> GetAllWithStatsAsync(string? searchTerm = null)
{
using var conn = _db.CreateConnection();
var sql = @"
SELECT
SELECT TOP 100
u.Id as id,
ISNULL(u.BillingName, u.Username) as name,
ISNULL(u.BillingTaxId, 'S/D') as dniOrCuit,
ISNULL(u.Email, 'Sin correo') as email,
ISNULL(u.Phone, 'Sin teléfono') as phone,
ISNULL(u.BillingTaxType, 'Consumidor Final') as taxType,
u.Username as username,
u.IsActive as isActive,
u.Role as role,
(SELECT COUNT(1) FROM Listings l WHERE l.ClientId = u.Id) as totalAds,
ISNULL((SELECT SUM(AdFee) FROM Listings l WHERE l.ClientId = u.Id), 0) as totalSpent
FROM Users u
WHERE Role IN ('Client', 'User') -- Mostramos tanto clientes puros como usuarios web
ORDER BY name";
return await conn.QueryAsync(sql);
WHERE Role IN ('Client', 'User')";
if (!string.IsNullOrWhiteSpace(searchTerm))
{
sql += " AND (u.BillingName LIKE @Query OR u.BillingTaxId LIKE @Query OR u.Username LIKE @Query)";
}
sql += " ORDER BY name";
return await conn.QueryAsync(sql, new { Query = $"%{searchTerm}%" });
}
public async Task UpdateAsync(Client client)
@@ -82,7 +94,10 @@ public class ClientRepository
BillingTaxId = @DniOrCuit,
Email = @Email,
Phone = @Phone,
BillingAddress = @Address
BillingAddress = @Address,
BillingTaxType = @TaxType,
Username = @Username,
IsActive = @IsActive
WHERE Id = @Id";
await conn.ExecuteAsync(sql, client);
}
@@ -95,6 +110,8 @@ public class ClientRepository
u.Id,
ISNULL(u.BillingName, u.Username) as Name,
u.BillingTaxId as DniOrCuit, u.Email, u.Phone, u.BillingAddress as Address,
u.BillingTaxType as TaxType,
u.Username, u.IsActive, u.Role,
(SELECT COUNT(1) FROM Listings WHERE ClientId = u.Id) as TotalAds,
ISNULL((SELECT SUM(AdFee) FROM Listings WHERE ClientId = u.Id), 0) as TotalInvested,
(SELECT MAX(CreatedAt) FROM Listings WHERE ClientId = u.Id) as LastAdDate,
@@ -112,4 +129,11 @@ public class ClientRepository
return await conn.QuerySingleOrDefaultAsync<dynamic>(sql, new { Id = clientId });
}
public async Task ResetPasswordAsync(int clientId, string passwordHash)
{
using var conn = _db.CreateConnection();
var sql = "UPDATE Users SET PasswordHash = @Hash, MustChangePassword = 1 WHERE Id = @Id";
await conn.ExecuteAsync(sql, new { Hash = passwordHash, Id = clientId });
}
}