136 lines
4.1 KiB
JavaScript
136 lines
4.1 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* 创建付费测试用户的脚本
|
|
*
|
|
* 使用方法:
|
|
* node scripts/create-premium-user.js [email] [password] [displayName]
|
|
*
|
|
* 例子:
|
|
* node scripts/create-premium-user.js premium@test.com TestPass123 "Premium User"
|
|
*/
|
|
|
|
const { Client } = require('pg');
|
|
const path = require('path');
|
|
require('dotenv').config({ path: path.join(__dirname, '..', '.env') });
|
|
|
|
async function createPremiumUser(email, password, displayName) {
|
|
const client = new Client({
|
|
connectionString: process.env.DATABASE_URL
|
|
});
|
|
|
|
try {
|
|
await client.connect();
|
|
console.log('✅ Connected to database');
|
|
|
|
// 1. 首先注册普通用户
|
|
console.log(`\n📝 Registering user: ${email}`);
|
|
|
|
const registerResponse = await fetch('http://localhost:3001/api/v1/auth/register-email', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
email,
|
|
password,
|
|
displayName
|
|
})
|
|
});
|
|
|
|
if (!registerResponse.ok) {
|
|
const error = await registerResponse.json();
|
|
throw new Error(`Registration failed: ${error.message}`);
|
|
}
|
|
|
|
const registerData = await registerResponse.json();
|
|
const userId = registerData.userId;
|
|
|
|
console.log(`✅ User registered successfully with ID: ${userId}`);
|
|
|
|
// 2. 更新用户为付费用户
|
|
const fakeCustomerId = 'cus_test_premium_' + Date.now();
|
|
const fakeSubscriptionId = 'sub_test_premium_' + Date.now();
|
|
|
|
const updateQuery = `
|
|
UPDATE user_profiles
|
|
SET
|
|
payment_provider_customer_id = $1,
|
|
payment_provider_subscription_id = $2,
|
|
updated_at = NOW()
|
|
WHERE id = $3
|
|
`;
|
|
|
|
const result = await client.query(updateQuery, [fakeCustomerId, fakeSubscriptionId, userId]);
|
|
|
|
if (result.rowCount > 0) {
|
|
console.log('✅ User upgraded to premium successfully!');
|
|
|
|
// 3. 测试登录和profile获取
|
|
console.log('\n🔐 Testing login...');
|
|
const loginResponse = await fetch('http://localhost:3001/api/v1/auth/login-email', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
email,
|
|
password
|
|
})
|
|
});
|
|
|
|
if (loginResponse.ok) {
|
|
const loginData = await loginResponse.json();
|
|
console.log('✅ Login successful');
|
|
|
|
// 测试profile
|
|
const profileResponse = await fetch('http://localhost:3001/api/v1/auth/profile', {
|
|
method: 'GET',
|
|
headers: {
|
|
'Authorization': `Bearer ${loginData.accessToken}`,
|
|
'Content-Type': 'application/json',
|
|
}
|
|
});
|
|
|
|
if (profileResponse.ok) {
|
|
const profileData = await profileResponse.json();
|
|
console.log('✅ Profile retrieved successfully');
|
|
|
|
console.log('\n🎉 Premium user created successfully!');
|
|
console.log('='.repeat(50));
|
|
console.log(`👤 User Details:`);
|
|
console.log(` Email: ${email}`);
|
|
console.log(` Password: ${password}`);
|
|
console.log(` Display Name: ${displayName}`);
|
|
console.log(` User ID: ${userId}`);
|
|
console.log(` Customer ID: ${fakeCustomerId}`);
|
|
console.log(` Subscription ID: ${fakeSubscriptionId}`);
|
|
console.log(` Subscription Status: ${profileData.subscriptionStatus}`);
|
|
console.log(` Has Active Subscription: ${profileData.hasActiveSubscription}`);
|
|
console.log('='.repeat(50));
|
|
}
|
|
}
|
|
} else {
|
|
console.log('❌ Failed to upgrade user to premium');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('❌ Error:', error.message);
|
|
process.exit(1);
|
|
} finally {
|
|
await client.end();
|
|
}
|
|
}
|
|
|
|
// 命令行参数处理
|
|
const args = process.argv.slice(2);
|
|
const email = args[0] || 'premium' + Date.now() + '@test.com';
|
|
const password = args[1] || 'TestPassword123';
|
|
const displayName = args[2] || 'Premium User';
|
|
|
|
console.log('🚀 Creating Premium User...');
|
|
console.log(`Email: ${email}`);
|
|
console.log(`Password: ${password}`);
|
|
console.log(`Display Name: ${displayName}`);
|
|
|
|
createPremiumUser(email, password, displayName); |