修正資料即時呈現
This commit is contained in:
131
scripts/get-real-data.js
Normal file
131
scripts/get-real-data.js
Normal file
@@ -0,0 +1,131 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* 獲取真實數據並保存為 JSON 文件
|
||||
*/
|
||||
|
||||
const { PrismaClient } = require('@prisma/client')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
// 設定環境變數
|
||||
process.env.DATABASE_URL = "mysql://wish_pool:Aa123456@mysql.theaken.com:33306/db_wish_pool?schema=public"
|
||||
|
||||
async function getRealData() {
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
try {
|
||||
console.log('🔍 獲取真實數據...')
|
||||
|
||||
// 使用原始 SQL 查詢,避免排序問題
|
||||
const allWishes = await prisma.$queryRaw`
|
||||
SELECT id, title, current_pain, expected_solution, expected_effect,
|
||||
is_public, email, images, user_session, status, category, priority,
|
||||
created_at, updated_at
|
||||
FROM wishes
|
||||
WHERE status = 'active'
|
||||
LIMIT 100
|
||||
`
|
||||
|
||||
const publicWishes = await prisma.$queryRaw`
|
||||
SELECT id, title, current_pain, expected_solution, expected_effect,
|
||||
is_public, email, images, user_session, status, category, priority,
|
||||
created_at, updated_at
|
||||
FROM wishes
|
||||
WHERE is_public = true AND status = 'active'
|
||||
LIMIT 100
|
||||
`
|
||||
|
||||
console.log(`✅ 總計: ${allWishes.length} 筆`)
|
||||
console.log(`✅ 公開: ${publicWishes.length} 筆`)
|
||||
console.log(`✅ 私密: ${allWishes.length - publicWishes.length} 筆`)
|
||||
|
||||
// 獲取所有困擾案例的點讚數
|
||||
console.log('🔍 獲取點讚數據...')
|
||||
const likeCounts = await prisma.$queryRaw`
|
||||
SELECT wish_id, COUNT(*) as count
|
||||
FROM wish_likes
|
||||
GROUP BY wish_id
|
||||
`
|
||||
|
||||
// 創建點讚數映射
|
||||
const likeCountMap = {}
|
||||
likeCounts.forEach((item) => {
|
||||
likeCountMap[Number(item.wish_id)] = Number(item.count)
|
||||
})
|
||||
|
||||
console.log(`✅ 獲取到 ${Object.keys(likeCountMap).length} 個困擾案例的點讚數據`)
|
||||
|
||||
// 轉換數據格式
|
||||
const formatWishes = (wishes) => {
|
||||
return wishes.map((wish) => ({
|
||||
id: Number(wish.id), // 轉換 BigInt 為 Number
|
||||
title: wish.title,
|
||||
current_pain: wish.current_pain,
|
||||
expected_solution: wish.expected_solution,
|
||||
expected_effect: wish.expected_effect,
|
||||
is_public: Boolean(wish.is_public),
|
||||
email: wish.email,
|
||||
images: wish.images,
|
||||
user_session: wish.user_session,
|
||||
status: wish.status,
|
||||
category: wish.category,
|
||||
priority: Number(wish.priority),
|
||||
like_count: likeCountMap[Number(wish.id)] || 0, // 使用真實的點讚數
|
||||
created_at: wish.created_at ? new Date(wish.created_at).toISOString() : new Date().toISOString(),
|
||||
updated_at: wish.updated_at ? new Date(wish.updated_at).toISOString() : new Date().toISOString()
|
||||
}))
|
||||
}
|
||||
|
||||
const formattedAllWishes = formatWishes(allWishes)
|
||||
const formattedPublicWishes = formatWishes(publicWishes)
|
||||
|
||||
// 按照 created_at 日期降序排序(最新的在前面)
|
||||
formattedAllWishes.sort((a, b) => {
|
||||
const dateA = new Date(a.created_at)
|
||||
const dateB = new Date(b.created_at)
|
||||
return dateB.getTime() - dateA.getTime() // 降序排序
|
||||
})
|
||||
|
||||
formattedPublicWishes.sort((a, b) => {
|
||||
const dateA = new Date(a.created_at)
|
||||
const dateB = new Date(b.created_at)
|
||||
return dateB.getTime() - dateA.getTime() // 降序排序
|
||||
})
|
||||
|
||||
// 保存為 JSON 文件
|
||||
const dataDir = path.join(__dirname, '..', 'data')
|
||||
if (!fs.existsSync(dataDir)) {
|
||||
fs.mkdirSync(dataDir, { recursive: true })
|
||||
}
|
||||
|
||||
fs.writeFileSync(
|
||||
path.join(dataDir, 'all-wishes.json'),
|
||||
JSON.stringify({ success: true, data: formattedAllWishes }, null, 2)
|
||||
)
|
||||
|
||||
fs.writeFileSync(
|
||||
path.join(dataDir, 'public-wishes.json'),
|
||||
JSON.stringify({ success: true, data: formattedPublicWishes }, null, 2)
|
||||
)
|
||||
|
||||
console.log('✅ 數據已保存到 data/ 目錄')
|
||||
|
||||
// 顯示點讚統計
|
||||
const totalLikes = formattedAllWishes.reduce((sum, wish) => sum + wish.like_count, 0)
|
||||
const publicLikes = formattedPublicWishes.reduce((sum, wish) => sum + wish.like_count, 0)
|
||||
console.log(`📊 點讚統計: 總計 ${totalLikes} 個,公開 ${publicLikes} 個`)
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 獲取數據失敗:', error.message)
|
||||
} finally {
|
||||
await prisma.$disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
// 執行
|
||||
if (require.main === module) {
|
||||
getRealData()
|
||||
}
|
||||
|
||||
module.exports = { getRealData }
|
||||
@@ -1,85 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* 測試點讚 API
|
||||
*/
|
||||
|
||||
async function testLikeAPI() {
|
||||
try {
|
||||
console.log('🔍 測試點讚 API...')
|
||||
console.log('')
|
||||
|
||||
const testUserSession = `test_api_session_${Date.now()}`
|
||||
const testWishId = 6 // 使用存在的 Wish ID
|
||||
|
||||
// 1. 測試檢查點讚狀態
|
||||
console.log('1️⃣ 測試檢查點讚狀態...')
|
||||
const checkResponse = await fetch(`http://localhost:3000/api/wishes/like?wishId=${testWishId}`, {
|
||||
headers: {
|
||||
'x-user-session': testUserSession
|
||||
}
|
||||
})
|
||||
|
||||
const checkResult = await checkResponse.json()
|
||||
console.log(`✅ 檢查結果: ${checkResult.success ? '成功' : '失敗'}`)
|
||||
console.log(` 已點讚: ${checkResult.data?.liked || false}`)
|
||||
console.log('')
|
||||
|
||||
// 2. 測試點讚
|
||||
console.log('2️⃣ 測試點讚...')
|
||||
const likeResponse = await fetch('http://localhost:3000/api/wishes/like', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-user-session': testUserSession
|
||||
},
|
||||
body: JSON.stringify({ wishId: testWishId })
|
||||
})
|
||||
|
||||
const likeResult = await likeResponse.json()
|
||||
console.log(`✅ 點讚結果: ${likeResult.success ? '成功' : '失敗'}`)
|
||||
console.log(` 點讚狀態: ${likeResult.data?.liked || false}`)
|
||||
console.log('')
|
||||
|
||||
// 3. 再次檢查點讚狀態
|
||||
console.log('3️⃣ 再次檢查點讚狀態...')
|
||||
const checkResponse2 = await fetch(`http://localhost:3000/api/wishes/like?wishId=${testWishId}`, {
|
||||
headers: {
|
||||
'x-user-session': testUserSession
|
||||
}
|
||||
})
|
||||
|
||||
const checkResult2 = await checkResponse2.json()
|
||||
console.log(`✅ 檢查結果: ${checkResult2.success ? '成功' : '失敗'}`)
|
||||
console.log(` 已點讚: ${checkResult2.data?.liked || false}`)
|
||||
console.log('')
|
||||
|
||||
// 4. 測試重複點讚
|
||||
console.log('4️⃣ 測試重複點讚...')
|
||||
const likeResponse2 = await fetch('http://localhost:3000/api/wishes/like', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-user-session': testUserSession
|
||||
},
|
||||
body: JSON.stringify({ wishId: testWishId })
|
||||
})
|
||||
|
||||
const likeResult2 = await likeResponse2.json()
|
||||
console.log(`✅ 重複點讚結果: ${likeResult2.success ? '成功' : '失敗'}`)
|
||||
console.log(` 點讚狀態: ${likeResult2.data?.liked || false}`)
|
||||
console.log('')
|
||||
|
||||
console.log('🎉 點讚 API 測試完成!')
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 測試失敗:', error.message)
|
||||
}
|
||||
}
|
||||
|
||||
// 執行測試
|
||||
if (require.main === module) {
|
||||
testLikeAPI()
|
||||
}
|
||||
|
||||
module.exports = { testLikeAPI }
|
||||
@@ -1,101 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* 測試點讚功能
|
||||
*/
|
||||
|
||||
const { PrismaClient } = require('@prisma/client')
|
||||
|
||||
// 設定環境變數
|
||||
process.env.DATABASE_URL = "mysql://wish_pool:Aa123456@mysql.theaken.com:33306/db_wish_pool?schema=public"
|
||||
|
||||
async function testLikeFunctionality() {
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
try {
|
||||
console.log('🔍 測試點讚功能...')
|
||||
console.log('')
|
||||
|
||||
// 1. 檢查現有的點讚記錄
|
||||
console.log('1️⃣ 檢查現有的點讚記錄...')
|
||||
const existingLikes = await prisma.wishLike.findMany({
|
||||
take: 5,
|
||||
orderBy: { createdAt: 'desc' }
|
||||
})
|
||||
|
||||
console.log(`✅ 現有 ${existingLikes.length} 筆點讚記錄`)
|
||||
existingLikes.forEach((like, index) => {
|
||||
console.log(` ${index + 1}. Wish ID: ${like.wishId}, Session: ${like.userSession.substring(0, 20)}...`)
|
||||
})
|
||||
console.log('')
|
||||
|
||||
// 2. 測試創建點讚記錄
|
||||
console.log('2️⃣ 測試創建點讚記錄...')
|
||||
// 先獲取一個存在的 Wish ID
|
||||
const existingWish = await prisma.wish.findFirst()
|
||||
if (!existingWish) {
|
||||
console.log('❌ 沒有找到任何困擾案例')
|
||||
return
|
||||
}
|
||||
const testWishId = existingWish.id
|
||||
const testUserSession = `test_session_${Date.now()}`
|
||||
|
||||
console.log(` 使用 Wish ID: ${testWishId}`)
|
||||
|
||||
try {
|
||||
const newLike = await prisma.wishLike.create({
|
||||
data: {
|
||||
wishId: testWishId,
|
||||
userSession: testUserSession
|
||||
}
|
||||
})
|
||||
console.log(`✅ 成功創建點讚記錄: ID ${newLike.id}`)
|
||||
} catch (error) {
|
||||
if (error.code === 'P2002') {
|
||||
console.log('⚠️ 點讚記錄已存在(重複點讚)')
|
||||
} else {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
console.log('')
|
||||
|
||||
// 3. 測試查詢點讚記錄
|
||||
console.log('3️⃣ 測試查詢點讚記錄...')
|
||||
const foundLike = await prisma.wishLike.findFirst({
|
||||
where: {
|
||||
wishId: testWishId,
|
||||
userSession: testUserSession
|
||||
}
|
||||
})
|
||||
|
||||
if (foundLike) {
|
||||
console.log(`✅ 成功找到點讚記錄: ID ${foundLike.id}`)
|
||||
} else {
|
||||
console.log('❌ 未找到點讚記錄')
|
||||
}
|
||||
console.log('')
|
||||
|
||||
// 4. 統計點讚數量
|
||||
console.log('4️⃣ 統計點讚數量...')
|
||||
const likeCount = await prisma.wishLike.count({
|
||||
where: { wishId: testWishId }
|
||||
})
|
||||
console.log(`✅ Wish ID ${testWishId} 的點讚數量: ${likeCount}`)
|
||||
console.log('')
|
||||
|
||||
console.log('🎉 點讚功能測試完成!')
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 測試失敗:', error.message)
|
||||
console.error('詳細錯誤:', error)
|
||||
} finally {
|
||||
await prisma.$disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
// 執行測試
|
||||
if (require.main === module) {
|
||||
testLikeFunctionality()
|
||||
}
|
||||
|
||||
module.exports = { testLikeFunctionality }
|
||||
Reference in New Issue
Block a user