// ===================================================== // 獲取可用應用列表 API // ===================================================== import { NextRequest, NextResponse } from 'next/server'; import { db } from '@/lib/database'; export async function GET(request: NextRequest) { try { console.log('🚀 ========== 應用 API 開始執行 =========='); const { searchParams } = new URL(request.url); const teamId = searchParams.get('teamId'); console.log('🔍 獲取可用應用列表, teamId:', teamId); console.log('🔍 請求 URL:', request.url); // 先檢查所有應用 console.log('📊 開始檢查數據庫...'); const allAppsSql = `SELECT COUNT(*) as count FROM apps`; const allAppsResult = await db.query(allAppsSql); console.log('📊 數據庫中應用總數:', allAppsResult[0].count); // 檢查活躍應用 const activeAppsSql = `SELECT COUNT(*) as count FROM apps WHERE is_active = TRUE`; const activeAppsResult = await db.query(activeAppsSql); console.log('✅ 活躍應用數量 (is_active = TRUE):', activeAppsResult[0].count); // 檢查所有應用的 is_active 值 const allAppsWithStatusSql = `SELECT id, name, is_active, team_id FROM apps LIMIT 5`; const allAppsWithStatusResult = await db.query(allAppsWithStatusSql); console.log('📋 前5個應用的狀態:', allAppsWithStatusResult); // 檢查是否有 is_active = 1 的應用 const activeAppsWith1Sql = `SELECT COUNT(*) as count FROM apps WHERE is_active = 1`; const activeAppsWith1Result = await db.query(activeAppsWith1Sql); console.log('✅ is_active = 1 的應用數量:', activeAppsWith1Result[0].count); // 檢查是否有 is_active = '1' 的應用(字符串) const activeAppsWithStringSql = `SELECT COUNT(*) as count FROM apps WHERE is_active = '1'`; const activeAppsWithStringResult = await db.query(activeAppsWithStringSql); console.log('✅ is_active = "1" 的應用數量:', activeAppsWithStringResult[0].count); // 檢查沒有團隊的應用 const noTeamAppsSql = `SELECT COUNT(*) as count FROM apps WHERE is_active = 1 AND team_id IS NULL`; const noTeamAppsResult = await db.query(noTeamAppsSql); console.log('🔓 沒有團隊的應用數量:', noTeamAppsResult[0].count); // 檢查屬於其他團隊的應用 const otherTeamAppsSql = `SELECT COUNT(*) as count FROM apps WHERE is_active = 1 AND team_id IS NOT NULL AND team_id != ?`; const otherTeamAppsResult = await db.query(otherTeamAppsSql, [teamId || '']); console.log('🔓 屬於其他團隊的應用數量:', otherTeamAppsResult[0].count); // 獲取所有活躍的應用,編輯團隊時顯示所有應用(包括已綁定的) // 使用 is_active = 1 因為數據庫中存儲的是數字 1 // 與 users 表 JOIN 獲取創建者姓名 let sql = ` SELECT a.id, a.name, a.description, a.category, a.type, a.icon, a.icon_color, a.app_url, a.creator_id, a.team_id, a.created_at as submissionDate, u.name as creator_name FROM apps a LEFT JOIN users u ON a.creator_id = u.id WHERE a.is_active = 1 ORDER BY a.created_at DESC `; const params: any[] = []; console.log('📝 執行的 SQL:', sql); console.log('📝 參數:', params); const apps = await db.query(sql, params); console.log('📊 查詢結果:', apps.length, '個應用'); // 如果沒有結果,嘗試不同的查詢條件 if (apps.length === 0) { console.log('⚠️ 沒有找到 is_active = 1 的應用,嘗試其他查詢條件...'); // 嘗試 is_active = TRUE const sqlTrue = sql.replace('WHERE a.is_active = 1', 'WHERE a.is_active = TRUE'); const appsTrue = await db.query(sqlTrue, params); console.log('📊 is_active = TRUE 查詢結果:', appsTrue.length, '個應用'); // 嘗試 is_active = '1' const sqlString = sql.replace('WHERE a.is_active = 1', 'WHERE a.is_active = "1"'); const appsString = await db.query(sqlString, params); console.log('📊 is_active = "1" 查詢結果:', appsString.length, '個應用'); // 嘗試沒有 is_active 條件 const sqlNoFilter = sql.replace('WHERE a.is_active = 1', 'WHERE 1=1'); const appsNoFilter = await db.query(sqlNoFilter, params); console.log('📊 無 is_active 過濾查詢結果:', appsNoFilter.length, '個應用'); // 使用有結果的查詢 if (appsTrue.length > 0) { console.log('✅ 使用 is_active = TRUE 的結果'); return NextResponse.json({ success: true, message: '可用應用列表獲取成功', data: appsTrue }); } else if (appsString.length > 0) { console.log('✅ 使用 is_active = "1" 的結果'); return NextResponse.json({ success: true, message: '可用應用列表獲取成功', data: appsString }); } else if (appsNoFilter.length > 0) { console.log('✅ 使用無過濾條件的結果'); return NextResponse.json({ success: true, message: '可用應用列表獲取成功', data: appsNoFilter }); } } console.log('🚀 ========== 應用 API 執行完成 =========='); return NextResponse.json({ success: true, message: '可用應用列表獲取成功', data: apps }); } catch (error) { console.error('❌ 獲取可用應用列表失敗:', error); return NextResponse.json({ success: false, message: '獲取可用應用列表失敗', error: error instanceof Error ? error.message : '未知錯誤' }, { status: 500 }); } }