實作個人收藏、個人活動紀錄
This commit is contained in:
67
scripts/check-app-status.js
Normal file
67
scripts/check-app-status.js
Normal file
@@ -0,0 +1,67 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
|
||||
// 資料庫連接配置
|
||||
const dbConfig = {
|
||||
host: 'mysql.theaken.com',
|
||||
port: 33306,
|
||||
user: 'AI_Platform',
|
||||
password: 'Aa123456',
|
||||
database: 'db_AI_Platform',
|
||||
charset: 'utf8mb4'
|
||||
};
|
||||
|
||||
async function checkAppStatus() {
|
||||
let connection;
|
||||
|
||||
try {
|
||||
console.log('連接到資料庫...');
|
||||
connection = await mysql.createConnection(dbConfig);
|
||||
|
||||
const appId = '7f7395f4-ad9f-4d14-9e2c-84962ecbcfd7';
|
||||
|
||||
// 檢查應用狀態
|
||||
console.log('檢查應用狀態...');
|
||||
const appSql = 'SELECT id, name, is_active FROM apps WHERE id = ?';
|
||||
const appResult = await connection.execute(appSql, [appId]);
|
||||
console.log('應用狀態:', appResult[0]);
|
||||
|
||||
// 檢查活動日誌
|
||||
console.log('\n檢查活動日誌...');
|
||||
const activitySql = 'SELECT * FROM activity_logs WHERE resource_id = ? ORDER BY created_at DESC LIMIT 3';
|
||||
const activityResult = await connection.execute(activitySql, [appId]);
|
||||
console.log('活動日誌:', activityResult[0]);
|
||||
|
||||
// 測試 JOIN 查詢
|
||||
console.log('\n測試 JOIN 查詢...');
|
||||
const joinSql = `
|
||||
SELECT
|
||||
a.*,
|
||||
u.name as creator_name,
|
||||
u.department as creator_department,
|
||||
al.created_at as last_used,
|
||||
al.details
|
||||
FROM activity_logs al
|
||||
JOIN apps a ON al.resource_id = a.id
|
||||
LEFT JOIN users u ON a.creator_id = u.id
|
||||
WHERE al.user_id = ?
|
||||
AND al.action = 'view'
|
||||
AND al.resource_type = 'app'
|
||||
AND a.is_active = TRUE
|
||||
ORDER BY al.created_at DESC
|
||||
LIMIT ?
|
||||
`;
|
||||
const joinResult = await connection.execute(joinSql, ['7fbe6712-fcce-45b8-9889-608232161315', 10]);
|
||||
console.log('JOIN 查詢結果:', joinResult[0]);
|
||||
|
||||
} catch (error) {
|
||||
console.error('檢查過程中發生錯誤:', error);
|
||||
} finally {
|
||||
if (connection) {
|
||||
await connection.end();
|
||||
console.log('資料庫連接已關閉');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 執行檢查
|
||||
checkAppStatus().catch(console.error);
|
92
scripts/fix-favorites-duplicates.js
Normal file
92
scripts/fix-favorites-duplicates.js
Normal file
@@ -0,0 +1,92 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
|
||||
// 資料庫連接配置
|
||||
const dbConfig = {
|
||||
host: 'mysql.theaken.com',
|
||||
port: 33306,
|
||||
user: 'AI_Platform',
|
||||
password: 'Aa123456',
|
||||
database: 'db_AI_Platform',
|
||||
charset: 'utf8mb4'
|
||||
};
|
||||
|
||||
async function fixFavoritesDuplicates() {
|
||||
let connection;
|
||||
|
||||
try {
|
||||
console.log('連接到資料庫...');
|
||||
connection = await mysql.createConnection(dbConfig);
|
||||
|
||||
console.log('檢查重複的收藏記錄...');
|
||||
|
||||
// 查找重複記錄
|
||||
const [duplicates] = await connection.execute(`
|
||||
SELECT user_id, app_id, COUNT(*) as count
|
||||
FROM user_favorites
|
||||
GROUP BY user_id, app_id
|
||||
HAVING COUNT(*) > 1
|
||||
`);
|
||||
|
||||
if (duplicates.length === 0) {
|
||||
console.log('沒有發現重複的收藏記錄');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`發現 ${duplicates.length} 組重複記錄:`);
|
||||
|
||||
for (const duplicate of duplicates) {
|
||||
console.log(`用戶 ${duplicate.user_id} 對應用 ${duplicate.app_id} 有 ${duplicate.count} 條記錄`);
|
||||
|
||||
// 保留最早的記錄,刪除其他重複記錄
|
||||
const [records] = await connection.execute(`
|
||||
SELECT id, created_at
|
||||
FROM user_favorites
|
||||
WHERE user_id = ? AND app_id = ?
|
||||
ORDER BY created_at ASC
|
||||
`, [duplicate.user_id, duplicate.app_id]);
|
||||
|
||||
if (records.length > 1) {
|
||||
// 保留第一條記錄,刪除其他記錄
|
||||
const keepRecord = records[0];
|
||||
const deleteIds = records.slice(1).map(r => r.id);
|
||||
|
||||
console.log(`保留記錄 ID: ${keepRecord.id} (創建時間: ${keepRecord.created_at})`);
|
||||
console.log(`刪除記錄 IDs: ${deleteIds.join(', ')}`);
|
||||
|
||||
await connection.execute(`
|
||||
DELETE FROM user_favorites
|
||||
WHERE id IN (${deleteIds.map(() => '?').join(',')})
|
||||
`, deleteIds);
|
||||
|
||||
console.log(`已清理 ${deleteIds.length} 條重複記錄`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('重複記錄清理完成!');
|
||||
|
||||
// 驗證修復結果
|
||||
const [remainingDuplicates] = await connection.execute(`
|
||||
SELECT user_id, app_id, COUNT(*) as count
|
||||
FROM user_favorites
|
||||
GROUP BY user_id, app_id
|
||||
HAVING COUNT(*) > 1
|
||||
`);
|
||||
|
||||
if (remainingDuplicates.length === 0) {
|
||||
console.log('✅ 所有重複記錄已成功清理');
|
||||
} else {
|
||||
console.log('❌ 仍有重複記錄存在:', remainingDuplicates);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('修復過程中發生錯誤:', error);
|
||||
} finally {
|
||||
if (connection) {
|
||||
await connection.end();
|
||||
console.log('資料庫連接已關閉');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 執行修復
|
||||
fixFavoritesDuplicates().catch(console.error);
|
86
scripts/test-favorites.js
Normal file
86
scripts/test-favorites.js
Normal file
@@ -0,0 +1,86 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
|
||||
// 資料庫連接配置
|
||||
const dbConfig = {
|
||||
host: 'mysql.theaken.com',
|
||||
port: 33306,
|
||||
user: 'AI_Platform',
|
||||
password: 'Aa123456',
|
||||
database: 'db_AI_Platform',
|
||||
charset: 'utf8mb4'
|
||||
};
|
||||
|
||||
async function testFavorites() {
|
||||
let connection;
|
||||
|
||||
try {
|
||||
console.log('連接到資料庫...');
|
||||
connection = await mysql.createConnection(dbConfig);
|
||||
|
||||
// 測試添加收藏
|
||||
const testUserId = 'test-user-123';
|
||||
const testAppId = 'test-app-456';
|
||||
|
||||
console.log('測試添加收藏...');
|
||||
|
||||
// 先清理可能存在的測試數據
|
||||
await connection.execute(
|
||||
'DELETE FROM user_favorites WHERE user_id = ? AND app_id = ?',
|
||||
[testUserId, testAppId]
|
||||
);
|
||||
|
||||
// 第一次添加收藏
|
||||
const favoriteId1 = require('crypto').randomUUID();
|
||||
await connection.execute(`
|
||||
INSERT INTO user_favorites (id, user_id, app_id, created_at)
|
||||
VALUES (?, ?, ?, NOW())
|
||||
`, [favoriteId1, testUserId, testAppId]);
|
||||
|
||||
console.log('✅ 第一次添加收藏成功');
|
||||
|
||||
// 檢查是否已收藏
|
||||
const [checkResult] = await connection.execute(`
|
||||
SELECT COUNT(*) as count
|
||||
FROM user_favorites
|
||||
WHERE user_id = ? AND app_id = ?
|
||||
`, [testUserId, testAppId]);
|
||||
|
||||
console.log(`收藏記錄數量: ${checkResult[0].count}`);
|
||||
|
||||
// 嘗試重複添加收藏(應該失敗)
|
||||
try {
|
||||
const favoriteId2 = require('crypto').randomUUID();
|
||||
await connection.execute(`
|
||||
INSERT INTO user_favorites (id, user_id, app_id, created_at)
|
||||
VALUES (?, ?, ?, NOW())
|
||||
`, [favoriteId2, testUserId, testAppId]);
|
||||
|
||||
console.log('❌ 重複添加收藏應該失敗但成功了');
|
||||
} catch (error) {
|
||||
if (error.code === 'ER_DUP_ENTRY') {
|
||||
console.log('✅ 重複添加收藏正確地被阻止');
|
||||
} else {
|
||||
console.log('❌ 重複添加收藏失敗,但錯誤類型不正確:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
// 清理測試數據
|
||||
await connection.execute(
|
||||
'DELETE FROM user_favorites WHERE user_id = ? AND app_id = ?',
|
||||
[testUserId, testAppId]
|
||||
);
|
||||
|
||||
console.log('✅ 測試數據已清理');
|
||||
|
||||
} catch (error) {
|
||||
console.error('測試過程中發生錯誤:', error);
|
||||
} finally {
|
||||
if (connection) {
|
||||
await connection.end();
|
||||
console.log('資料庫連接已關閉');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 執行測試
|
||||
testFavorites().catch(console.error);
|
Reference in New Issue
Block a user