Files
psp_api/prisma/seed.ts
T

102 lines
2.8 KiB
TypeScript
Raw Normal View History

import { prisma } from '../src/lib/prisma'
async function main() {
if ((await prisma.role.count()) === 0) {
await prisma.role.upsert({
where: { id: 0, name: 'Admin' },
update: {},
create: {
name: 'Admin',
},
})
}
if ((await prisma.inventory.count()) === 0) {
await prisma.inventory.createMany({
data: [
{ name: 'انبار مرکزی', location: 'تهران', isPointOfSale: false, isActive: true },
{
name: 'فروشگاه حضوری',
location: 'مرکز شهر',
isPointOfSale: true,
isActive: true,
},
{
name: 'فروشگاه اینترنتی',
location: 'مرکز شهر',
isPointOfSale: true,
isActive: true,
},
],
})
}
if ((await prisma.productCategory.count()) === 0) {
await prisma.productCategory.createMany({
data: Array.from({ length: 10 }, (_, i) => ({ name: `دسته‌ی ${i + 1}` })),
})
}
if ((await prisma.productBrand.count()) === 0) {
await prisma.productBrand.createMany({
data: Array.from({ length: 10 }, (_, i) => ({ name: `برند ${i + 1}` })),
})
}
if ((await prisma.supplier.count()) === 0) {
await prisma.supplier.createMany({
data: Array.from({ length: 20 }, (_, i) => ({
firstName: 'تامین‌',
lastName: `کننده ${i + 1}`,
mobileNumber: `0912000000${i + 1}`,
email: `supplier${i + 1}@example.com`,
})),
})
}
if ((await prisma.customer.count()) === 0) {
await prisma.customer.createMany({
data: Array.from({ length: 5 }, (_, i) => ({
firstName: 'مشتری',
lastName: `${i + 1}`,
mobileNumber: `0913000000${i + 1}`,
email: `customer${i + 1}@example.com`,
})),
})
}
if ((await prisma.product.count()) === 0) {
const categories = await prisma.productCategory.findMany()
const brands = await prisma.productBrand.findMany()
await prisma.product.createMany({
data: Array.from({ length: 100 }, (_, i) => ({
name: `کالای ${i + 1}`,
sku: `SKU-${1000 + i + 1}`,
salePrice: parseInt((Math.random() * (100 - 10) + 10).toString()) * 10000,
categoryId: categories[i % categories.length].id,
brandId: brands[i % brands.length].id,
})),
})
}
// const products = await prisma.product.findMany()
// for (const product of products) {
// await prisma.product.update({
// where: { id: product.id },
// data: {
// salePrice: parseInt((Math.random() * (100 - 10) + 10).toString()) * 10000,
// },
// })
// }
}
main()
.then(async () => {
await prisma.$disconnect()
})
.catch(async e => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})