166 lines
5.2 KiB
JavaScript
166 lines
5.2 KiB
JavaScript
/* eslint-disable no-console */
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
require('dotenv').config({ path: path.join(__dirname, '..', '.env') });
|
|
if (fs.existsSync(path.join(__dirname, '..', 'creds', '.env'))) {
|
|
require('dotenv').config({ path: path.join(__dirname, '..', 'creds', '.env') });
|
|
}
|
|
const bcrypt = require('bcryptjs');
|
|
const { PrismaClient, Role, UserStatus, ListingStatus } = require('@prisma/client');
|
|
const { PrismaPg } = require('@prisma/adapter-pg');
|
|
const { Pool } = require('pg');
|
|
|
|
if (!process.env.DATABASE_URL) {
|
|
console.error('DATABASE_URL is not set; cannot seed.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const prisma = new PrismaClient({
|
|
adapter: new PrismaPg(new Pool({ connectionString: process.env.DATABASE_URL })),
|
|
});
|
|
|
|
const SAMPLE_SLUG = 'saimaa-lakeside-cabin';
|
|
const DEFAULT_LOCALE = 'en';
|
|
const SAMPLE_EMAIL = 'host@lomavuokraus.fi';
|
|
|
|
async function main() {
|
|
const adminEmail = process.env.ADMIN_EMAIL;
|
|
const adminPassword = process.env.ADMIN_INITIAL_PASSWORD;
|
|
|
|
if (!adminEmail || !adminPassword) {
|
|
console.warn('ADMIN_EMAIL or ADMIN_INITIAL_PASSWORD missing; admin user will not be seeded.');
|
|
}
|
|
|
|
let adminUser = null;
|
|
if (adminEmail && adminPassword) {
|
|
const adminHash = await bcrypt.hash(adminPassword, 12);
|
|
adminUser = await prisma.user.upsert({
|
|
where: { email: adminEmail },
|
|
update: {
|
|
passwordHash: adminHash,
|
|
role: Role.ADMIN,
|
|
status: UserStatus.ACTIVE,
|
|
emailVerifiedAt: new Date(),
|
|
approvedAt: new Date(),
|
|
},
|
|
create: {
|
|
email: adminEmail,
|
|
passwordHash: adminHash,
|
|
role: Role.ADMIN,
|
|
status: UserStatus.ACTIVE,
|
|
emailVerifiedAt: new Date(),
|
|
approvedAt: new Date(),
|
|
},
|
|
});
|
|
}
|
|
|
|
const sampleHostHash = await bcrypt.hash('changeme-sample', 12);
|
|
const existing = await prisma.listingTranslation.findFirst({ where: { slug: SAMPLE_SLUG } });
|
|
if (existing) {
|
|
console.log('Sample listing already exists, skipping seed.');
|
|
return;
|
|
}
|
|
|
|
const owner = await prisma.user.upsert({
|
|
where: { email: SAMPLE_EMAIL },
|
|
update: { name: 'Sample Host', role: 'USER', passwordHash: sampleHostHash, status: UserStatus.ACTIVE, emailVerifiedAt: new Date(), approvedAt: new Date() },
|
|
create: {
|
|
email: SAMPLE_EMAIL,
|
|
name: 'Sample Host',
|
|
role: 'USER',
|
|
passwordHash: sampleHostHash,
|
|
status: UserStatus.ACTIVE,
|
|
emailVerifiedAt: new Date(),
|
|
approvedAt: new Date(),
|
|
},
|
|
});
|
|
|
|
const listing = await prisma.listing.create({
|
|
data: {
|
|
ownerId: owner.id,
|
|
status: ListingStatus.PUBLISHED,
|
|
approvedAt: new Date(),
|
|
approvedById: adminUser ? adminUser.id : owner.id,
|
|
country: 'Finland',
|
|
region: 'South Karelia',
|
|
city: 'Punkaharju',
|
|
streetAddress: 'Saimaan rantatie 12',
|
|
addressNote: 'Lakeside trail, 5 min from main road',
|
|
latitude: 61.756,
|
|
longitude: 29.328,
|
|
maxGuests: 6,
|
|
bedrooms: 3,
|
|
beds: 4,
|
|
bathrooms: 1,
|
|
hasSauna: true,
|
|
hasFireplace: true,
|
|
hasWifi: true,
|
|
petsAllowed: false,
|
|
byTheLake: true,
|
|
hasAirConditioning: false,
|
|
evCharging: 'FREE',
|
|
priceHintPerNightCents: 14500,
|
|
contactName: 'Sample Host',
|
|
contactEmail: SAMPLE_EMAIL,
|
|
contactPhone: '+358401234567',
|
|
externalUrl: 'https://example.com/saimaa-cabin',
|
|
published: true,
|
|
images: {
|
|
createMany: {
|
|
data: [
|
|
{
|
|
url: 'https://images.unsplash.com/photo-1505691938895-1758d7feb511?auto=format&fit=crop&w=1600&q=80',
|
|
altText: 'Lakeside cabin with sauna',
|
|
isCover: true,
|
|
order: 1,
|
|
},
|
|
{
|
|
url: 'https://images.unsplash.com/photo-1505693416388-ac5ce068fe85?auto=format&fit=crop&w=1600&q=80',
|
|
altText: 'Wood-fired sauna by the lake',
|
|
order: 2,
|
|
},
|
|
{
|
|
url: 'https://images.unsplash.com/photo-1470246973918-29a93221c455?auto=format&fit=crop&w=1600&q=80',
|
|
altText: 'Living area with fireplace',
|
|
order: 3,
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
await prisma.listingTranslation.createMany({
|
|
data: [
|
|
{
|
|
listingId: listing.id,
|
|
locale: DEFAULT_LOCALE,
|
|
slug: SAMPLE_SLUG,
|
|
title: 'Saimaa lakeside cabin with sauna',
|
|
description:
|
|
'Classic timber cabin right on Lake Saimaa. Wood-fired sauna, private dock, and a short forest walk to the nearest village. Perfect for slow weekends and midsummer gatherings.',
|
|
teaser: 'Sauna, lake view, private dock, and cozy fireplace.',
|
|
},
|
|
{
|
|
listingId: listing.id,
|
|
locale: 'fi',
|
|
slug: SAMPLE_SLUG,
|
|
title: 'Saimaan rantamökki saunalla',
|
|
description:
|
|
'Perinteinen hirsimökki Saimaan rannalla. Puusauna, oma laituri ja lyhyt metsäreitti kylään. Sopii täydellisesti viikonloppuihin ja juhannukseen.',
|
|
teaser: 'Puusauna, järvinäköala, oma laituri ja takka.',
|
|
},
|
|
],
|
|
});
|
|
|
|
console.log('Seeded sample listing at slug:', SAMPLE_SLUG);
|
|
}
|
|
|
|
main()
|
|
.catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|