← back to Handbag Auth Nextjs
scripts/populate-price-history.js
69 lines
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
async function populatePriceHistory() {
console.log('Starting price history population...')
try {
// Get some sample listings
const listings = await prisma.listing.findMany({
where: { isActive: 1 },
take: 50,
orderBy: { id: 'asc' }
})
console.log(`Found ${listings.length} listings to generate history for`)
let totalRecords = 0
for (const listing of listings) {
// Generate 30-90 days of price history
const daysOfHistory = Math.floor(Math.random() * 60) + 30 // 30-90 days
const records = []
for (let i = daysOfHistory; i >= 0; i--) {
const date = new Date()
date.setDate(date.getDate() - i)
const recordedAt = date.toISOString().replace('T', ' ').split('.')[0]
// Add some price variation (±10%)
const variation = 0.9 + (Math.random() * 0.2) // 90% to 110%
const priceJpy = Math.round(listing.priceJpy * variation)
const priceUsd = listing.priceUsd ? Math.round((listing.priceUsd * variation) * 100) / 100 : null
// Randomly mark some as unavailable (sold)
const isAvailable = Math.random() > 0.1 ? 1 : 0 // 90% available
records.push({
listingId: listing.id,
externalId: listing.externalId || `item-${listing.id}`,
source: listing.source,
priceJpy,
priceUsd,
condition: listing.condition,
listingType: listing.listingType,
recordedAt,
isAvailable,
})
}
// Insert all records for this listing
await prisma.priceHistory.createMany({
data: records,
})
totalRecords += records.length
console.log(`✓ Created ${records.length} price records for listing ${listing.id} (${listing.brand})`)
}
console.log(`\n✅ Successfully created ${totalRecords} price history records for ${listings.length} listings`)
} catch (error) {
console.error('Error populating price history:', error)
} finally {
await prisma.$disconnect()
}
}
populatePriceHistory()