templates2.Admin.Next/prisma/seed.ts
w4c-templates c64f7ffa9e
Some checks failed
CI / quality (push) Has been cancelled
chore(template): publish Admin.Next
2026-10-11 23:25:34 +03:00

75 lines
2.9 KiB
TypeScript

import { PrismaClient } from '@prisma/client';
import bcrypt from 'bcryptjs';
const db = new PrismaClient();
const COMPANIES = ['Northwind', 'Globex', 'Initech', 'Umbrella', 'Soylent', 'Hooli', 'Stark Industries', 'Wayne Enterprises'];
const FIRST = ['Ada', 'Alan', 'Grace', 'Linus', 'Margaret', 'Dennis', 'Barbara', 'Ken'];
const LAST = ['Lovelace', 'Turing', 'Hopper', 'Torvalds', 'Hamilton', 'Ritchie', 'Liskov', 'Thompson'];
const STATUSES = ['active', 'lead', 'churned'];
const STAGES = ['prospecting', 'proposal', 'won', 'lost'];
async function main() {
const password = await bcrypt.hash('password', 10);
const mk = (email: string, name: string, role: string) =>
db.user.upsert({ where: { email }, update: { role }, create: { email, name, role, passwordHash: password } });
const admin = await mk('admin@example.com', 'Ada Admin', 'admin');
await mk('editor@example.com', 'Evan Editor', 'editor');
await mk('viewer@example.com', 'Vera Viewer', 'viewer');
for (const email of ['admin@example.com', 'editor@example.com', 'viewer@example.com']) {
const u = await db.user.findUnique({ where: { email } });
if (u) await db.subscription.upsert({ where: { userId: u.id }, update: {}, create: { userId: u.id, plan: 'free', status: 'active', provider: 'mock' } });
}
if ((await db.customer.count()) === 0) {
for (let i = 0; i < 20; i++) {
const first = FIRST[i % FIRST.length];
const last = LAST[Math.floor(i / FIRST.length) % LAST.length];
const name = `${first} ${last}`;
const email = `${first}.${last}.${i}@example.com`.toLowerCase();
await db.customer.upsert({
where: { email },
update: {},
create: { name, email, company: COMPANIES[i % COMPANIES.length], status: STATUSES[i % STATUSES.length] },
});
}
}
if ((await db.deal.count()) === 0) {
const customers = await db.customer.findMany({ take: 15 });
for (let i = 0; i < Math.min(15, customers.length); i++) {
await db.deal.create({
data: {
title: `Deal #${1000 + i} — ${customers[i].company ?? 'Direct'}`,
customerId: customers[i].id,
value: 1500 + i * 750,
stage: STAGES[i % STAGES.length],
owner: 'editor@example.com',
},
});
}
}
if ((await db.auditLog.count()) === 0) {
await db.auditLog.createMany({
data: [
{ actorId: admin.id, action: 'seed', target: 'system', meta: 'Initial demo data' },
{ actorId: admin.id, action: 'customer.import', target: '20 created' },
{ actorId: admin.id, action: 'deal.create', target: 'Deal #1000' },
],
});
}
console.log('Seeded CRM: admin@example.com (admin), editor@example.com (editor), viewer@example.com (viewer) — password: "password"');
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await db.$disconnect();
});