add landing and subscription plans
Signed-off-by: Omar Sánchez Pizarro <omar.sanchez@pistacero.net>
This commit is contained in:
@@ -1277,6 +1277,104 @@ export async function updateArticleFavorite(platform, id, is_favorite, username)
|
||||
}
|
||||
}
|
||||
|
||||
// Funciones para suscripciones
|
||||
export async function getUserSubscription(username) {
|
||||
if (!db) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const usersCollection = db.collection('users');
|
||||
const user = await usersCollection.findOne({ username });
|
||||
|
||||
if (!user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Si no tiene suscripción, retornar plan gratuito por defecto
|
||||
if (!user.subscription) {
|
||||
return {
|
||||
planId: 'free',
|
||||
status: 'active',
|
||||
currentPeriodStart: user.createdAt || new Date(),
|
||||
currentPeriodEnd: null, // Plan gratuito no expira
|
||||
cancelAtPeriodEnd: false,
|
||||
};
|
||||
}
|
||||
|
||||
return user.subscription;
|
||||
} catch (error) {
|
||||
console.error(`Error obteniendo suscripción de ${username}:`, error.message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateUserSubscription(username, subscriptionData) {
|
||||
if (!db) {
|
||||
throw new Error('MongoDB no está disponible');
|
||||
}
|
||||
|
||||
try {
|
||||
const usersCollection = db.collection('users');
|
||||
|
||||
// Verificar que el usuario existe
|
||||
const user = await usersCollection.findOne({ username });
|
||||
if (!user) {
|
||||
throw new Error(`Usuario ${username} no existe`);
|
||||
}
|
||||
|
||||
// Actualizar suscripción
|
||||
await usersCollection.updateOne(
|
||||
{ username },
|
||||
{
|
||||
$set: {
|
||||
subscription: {
|
||||
planId: subscriptionData.planId || 'free',
|
||||
status: subscriptionData.status || 'active',
|
||||
currentPeriodStart: subscriptionData.currentPeriodStart || new Date(),
|
||||
currentPeriodEnd: subscriptionData.currentPeriodEnd || null,
|
||||
cancelAtPeriodEnd: subscriptionData.cancelAtPeriodEnd || false,
|
||||
stripeCustomerId: subscriptionData.stripeCustomerId || null,
|
||||
stripeSubscriptionId: subscriptionData.stripeSubscriptionId || null,
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
updatedAt: new Date(),
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error(`Error actualizando suscripción de ${username}:`, error.message);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getWorkerCount(username) {
|
||||
if (!db) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
try {
|
||||
const workersCollection = db.collection('workers');
|
||||
const workersData = await workersCollection.findOne({ username });
|
||||
|
||||
if (!workersData || !workersData.items) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Contar solo workers activos (no deshabilitados)
|
||||
const activeWorkers = (workersData.items || []).filter(
|
||||
(item, index) => !(workersData.disabled || []).includes(index)
|
||||
);
|
||||
|
||||
return activeWorkers.length;
|
||||
} catch (error) {
|
||||
console.error(`Error contando workers de ${username}:`, error.message);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Cerrar conexión
|
||||
export async function closeMongoDB() {
|
||||
if (mongoClient) {
|
||||
|
||||
Reference in New Issue
Block a user