This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
import dotenv from 'dotenv';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
import { createDatabase } from '../../../infrastructure/database/client.js';
|
||||
import {
|
||||
categories,
|
||||
conditionProfiles,
|
||||
conditions,
|
||||
inventoryItems,
|
||||
listings,
|
||||
products,
|
||||
sellers,
|
||||
users,
|
||||
} from '../../../infrastructure/database/schema.js';
|
||||
|
||||
dotenv.config({ path: fileURLToPath(new URL('../../../../../../.env', import.meta.url)) });
|
||||
|
||||
const demoEmail = 'demo-catalog@example.test';
|
||||
|
||||
async function seedDemoCatalog() {
|
||||
const database = createDatabase();
|
||||
const [existingUser] = await database.db.select().from(users).where(eq(users.email, demoEmail));
|
||||
|
||||
if (existingUser) {
|
||||
console.log(`Demo catalog already exists for ${demoEmail}.`);
|
||||
await database.close();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await database.db.transaction(async (tx) => {
|
||||
const [user] = await tx.insert(users).values({
|
||||
email: demoEmail,
|
||||
passwordHash: 'demo-catalog-password-not-for-login',
|
||||
}).returning();
|
||||
const [seller] = await tx.insert(sellers).values({
|
||||
userId: user.id,
|
||||
displayName: 'Demo Catalog',
|
||||
}).returning();
|
||||
const [category] = await tx.insert(categories).values({
|
||||
name: 'Demo Collectibles',
|
||||
slug: 'demo-collectibles',
|
||||
}).returning();
|
||||
const [profile] = await tx.insert(conditionProfiles).values({
|
||||
categoryId: category.id,
|
||||
name: 'Collectible Condition',
|
||||
}).returning();
|
||||
const [condition] = await tx.insert(conditions).values({
|
||||
conditionProfileId: profile.id,
|
||||
name: 'Near Mint',
|
||||
code: 'NM',
|
||||
qualityRank: 1,
|
||||
}).returning();
|
||||
|
||||
const fixtures = [
|
||||
{ name: 'Charizard ex #199/165', slug: 'charizard-ex-199-165', price: 11500, status: 'AVAILABLE' },
|
||||
{ name: 'Pikachu World Collection', slug: 'pikachu-world-collection', price: 8200, status: 'PENDING' },
|
||||
{ name: 'Gengar holo promo', slug: 'gengar-holo-promo', price: 4500, status: 'CLOSED', inventoryStatus: 'SOLD' },
|
||||
];
|
||||
|
||||
for (const fixture of fixtures) {
|
||||
const [product] = await tx.insert(products).values({
|
||||
sellerId: seller.id,
|
||||
categoryId: category.id,
|
||||
name: fixture.name,
|
||||
slug: fixture.slug,
|
||||
description: 'A representative demo collectible for local browser testing.',
|
||||
brand: 'Demo Archive',
|
||||
setName: 'Demo Collection',
|
||||
cardNumber: fixture.slug === 'charizard-ex-199-165' ? '199/165' : null,
|
||||
releaseYear: 2026,
|
||||
}).returning();
|
||||
const [inventoryItem] = await tx.insert(inventoryItems).values({
|
||||
productId: product.id,
|
||||
conditionId: condition.id,
|
||||
status: fixture.inventoryStatus ?? 'ACTIVE',
|
||||
notes: 'Private demo inventory note.',
|
||||
}).returning();
|
||||
await tx.insert(listings).values({
|
||||
inventoryItemId: inventoryItem.id,
|
||||
productName: product.name,
|
||||
categoryId: category.id,
|
||||
conditionId: condition.id,
|
||||
askingPriceMinor: fixture.price,
|
||||
status: fixture.status,
|
||||
notes: fixture.status === 'CLOSED' ? 'Sold history retained.' : 'Available for local preview.',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
console.log('Seeded three demo collectibles for browser testing.');
|
||||
} finally {
|
||||
await database.close();
|
||||
}
|
||||
}
|
||||
|
||||
seedDemoCatalog().catch((error) => {
|
||||
console.error(error);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
Reference in New Issue
Block a user