212 lines
5.3 KiB
TypeScript
212 lines
5.3 KiB
TypeScript
/**
|
|
* Queries de servidores
|
|
* CRUD completo para a tabela servers
|
|
*/
|
|
|
|
import { query } from "../index";
|
|
|
|
export interface Server {
|
|
server_id: number;
|
|
server_name: string;
|
|
server_type: "pvp" | "no-pvp" | "retro-pvp" | "hardcore-pvp" | null;
|
|
location: "NA" | "SA" | "EU" | "OC" | "BR" | null;
|
|
is_active: boolean;
|
|
total_listings: number;
|
|
total_users: number;
|
|
total_subscribers: number;
|
|
total_transactions: number;
|
|
total_volume: number;
|
|
average_listing_price: number;
|
|
last_activity_at: Date | null;
|
|
created_at: Date;
|
|
updated_at: Date;
|
|
}
|
|
|
|
export interface ServerStats {
|
|
total_listings?: number;
|
|
total_users?: number;
|
|
total_subscribers?: number;
|
|
total_transactions?: number;
|
|
total_volume?: number;
|
|
average_listing_price?: number;
|
|
last_activity_at?: Date;
|
|
}
|
|
|
|
/**
|
|
* Lista todos os servidores
|
|
*/
|
|
export async function getAllServers(): Promise<Server[]> {
|
|
const result = await query<Server>(
|
|
`SELECT * FROM servers ORDER BY server_name ASC`,
|
|
);
|
|
return result.rows;
|
|
}
|
|
|
|
/**
|
|
* Lista apenas servidores ativos
|
|
*/
|
|
export async function getActiveServers(): Promise<Server[]> {
|
|
const result = await query<Server>(
|
|
`SELECT * FROM servers WHERE is_active = TRUE ORDER BY server_name ASC`,
|
|
);
|
|
return result.rows;
|
|
}
|
|
|
|
/**
|
|
* Busca servidor por nome
|
|
*/
|
|
export async function getServerByName(
|
|
serverName: string,
|
|
): Promise<Server | null> {
|
|
const result = await query<Server>(
|
|
`SELECT * FROM servers WHERE server_name = $1`,
|
|
[serverName],
|
|
);
|
|
return result.rows[0] || null;
|
|
}
|
|
|
|
/**
|
|
* Busca servidor por ID
|
|
*/
|
|
export async function getServerById(serverId: number): Promise<Server | null> {
|
|
const result = await query<Server>(
|
|
`SELECT * FROM servers WHERE server_id = $1`,
|
|
[serverId],
|
|
);
|
|
return result.rows[0] || null;
|
|
}
|
|
|
|
/**
|
|
* Verifica se servidor existe e está ativo
|
|
*/
|
|
export async function isServerActive(serverName: string): Promise<boolean> {
|
|
const result = await query<{ count: string }>(
|
|
`SELECT COUNT(*) as count FROM servers WHERE server_name = $1 AND is_active = TRUE`,
|
|
[serverName],
|
|
);
|
|
return parseInt(result.rows[0].count, 10) > 0;
|
|
}
|
|
|
|
/**
|
|
* Incrementa estatísticas do servidor
|
|
*/
|
|
export async function incrementServerStats(
|
|
serverName: string,
|
|
stats: Partial<{
|
|
total_listings: number;
|
|
total_users: number;
|
|
total_subscribers: number;
|
|
total_transactions: number;
|
|
total_volume: number;
|
|
}>,
|
|
): Promise<void> {
|
|
const updates: string[] = [];
|
|
const values: any[] = [];
|
|
let paramIndex = 1;
|
|
|
|
if (stats.total_listings !== undefined) {
|
|
updates.push(`total_listings = total_listings + $${paramIndex++}`);
|
|
values.push(stats.total_listings);
|
|
}
|
|
if (stats.total_users !== undefined) {
|
|
updates.push(`total_users = total_users + $${paramIndex++}`);
|
|
values.push(stats.total_users);
|
|
}
|
|
if (stats.total_subscribers !== undefined) {
|
|
updates.push(`total_subscribers = total_subscribers + $${paramIndex++}`);
|
|
values.push(stats.total_subscribers);
|
|
}
|
|
if (stats.total_transactions !== undefined) {
|
|
updates.push(`total_transactions = total_transactions + $${paramIndex++}`);
|
|
values.push(stats.total_transactions);
|
|
}
|
|
if (stats.total_volume !== undefined) {
|
|
updates.push(`total_volume = total_volume + $${paramIndex++}`);
|
|
values.push(stats.total_volume);
|
|
}
|
|
|
|
if (updates.length === 0) {
|
|
return;
|
|
}
|
|
|
|
// Sempre atualizar last_activity_at
|
|
updates.push(`last_activity_at = CURRENT_TIMESTAMP`);
|
|
values.push(serverName);
|
|
|
|
await query(
|
|
`UPDATE servers SET ${updates.join(", ")} WHERE server_name = $${paramIndex}`,
|
|
values,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Atualiza estatísticas completas do servidor
|
|
*/
|
|
export async function updateServerStats(
|
|
serverName: string,
|
|
stats: ServerStats,
|
|
): Promise<void> {
|
|
const updates: string[] = [];
|
|
const values: any[] = [];
|
|
let paramIndex = 1;
|
|
|
|
if (stats.total_listings !== undefined) {
|
|
updates.push(`total_listings = $${paramIndex++}`);
|
|
values.push(stats.total_listings);
|
|
}
|
|
if (stats.total_users !== undefined) {
|
|
updates.push(`total_users = $${paramIndex++}`);
|
|
values.push(stats.total_users);
|
|
}
|
|
if (stats.total_subscribers !== undefined) {
|
|
updates.push(`total_subscribers = $${paramIndex++}`);
|
|
values.push(stats.total_subscribers);
|
|
}
|
|
if (stats.total_transactions !== undefined) {
|
|
updates.push(`total_transactions = $${paramIndex++}`);
|
|
values.push(stats.total_transactions);
|
|
}
|
|
if (stats.total_volume !== undefined) {
|
|
updates.push(`total_volume = $${paramIndex++}`);
|
|
values.push(stats.total_volume);
|
|
}
|
|
if (stats.average_listing_price !== undefined) {
|
|
updates.push(`average_listing_price = $${paramIndex++}`);
|
|
values.push(stats.average_listing_price);
|
|
}
|
|
if (stats.last_activity_at !== undefined) {
|
|
updates.push(`last_activity_at = $${paramIndex++}`);
|
|
values.push(stats.last_activity_at);
|
|
}
|
|
|
|
if (updates.length === 0) {
|
|
return;
|
|
}
|
|
|
|
values.push(serverName);
|
|
|
|
await query(
|
|
`UPDATE servers SET ${updates.join(", ")} WHERE server_name = $${paramIndex}`,
|
|
values,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Incrementa total_users de múltiplos servidores
|
|
*/
|
|
export async function incrementUsersForServers(
|
|
serverNames: string[],
|
|
): Promise<void> {
|
|
if (serverNames.length === 0) {
|
|
return;
|
|
}
|
|
|
|
await query(
|
|
`UPDATE servers
|
|
SET total_users = total_users + 1,
|
|
last_activity_at = CURRENT_TIMESTAMP
|
|
WHERE server_name = ANY($1::text[])`,
|
|
[serverNames],
|
|
);
|
|
}
|