Files
psp_api/prisma/seed.ts
T

350 lines
9.3 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.user.count()) === 0) {
const adminRole = await prisma.role.findUnique({ where: { name: 'Admin' } })
if (!adminRole) {
return
}
await prisma.user.upsert({
where: { id: 1, firstName: 'عباس', lastName: 'حسنی' },
update: {},
create: {
firstName: 'عباس',
lastName: 'حسنی',
roleId: adminRole.id,
mobileNumber: '09120258156',
password: '12345678',
},
})
}
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: 9 }, (_, 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: 20 }, (_, 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,
})),
})
}
if ((await prisma.bank.count()) === 0) {
await prisma.bank.createMany({
data: [
{
name: 'آینده',
shortName: 'ain',
},
{
name: 'ایران زمین',
shortName: 'irz',
},
{
name: 'اقتصاد نوین',
shortName: 'eqn',
},
{
name: 'انصار',
shortName: 'ans',
},
{
name: 'پاسارگاد',
shortName: 'pas',
},
{
name: 'پارسیان',
shortName: 'prs',
},
{
name: 'پست‌ بانک ایران',
shortName: 'pbi',
},
{
name: 'تجارت',
shortName: 'tej',
},
{
name: 'توسعه تعاون',
shortName: 'tav',
},
{
name: 'توسعه صادرات',
shortName: 'tes',
},
{
name: 'حکمت ایرانیان',
shortName: 'hek',
},
{
name: 'رفاه کارگران',
shortName: 'ref',
},
{
name: 'قرض‌الحسنه رسالت',
shortName: 'res',
},
{
name: 'قرض‌الحسنه مهر ایران',
shortName: 'meh',
},
{
name: 'قوامین',
shortName: 'qva',
},
{
name: 'کشاورزی',
shortName: 'kes',
},
{
name: 'کوثر',
shortName: 'kos',
},
{
name: 'دی',
shortName: 'diy',
},
{
name: 'صنعت و معدن',
shortName: 'san',
},
{
name: 'سینا',
shortName: 'sin',
},
{
name: 'سرمایه',
shortName: 'sar',
},
{
name: 'سپه',
shortName: 'sep',
},
{
name: 'شهر',
shortName: 'shr',
},
{
name: 'صادرات ایران',
shortName: 'sir',
},
{
name: 'سامان',
shortName: 'sam',
},
{
name: 'مرکزی',
shortName: 'mar',
},
{
name: 'مسکن',
shortName: 'mas',
},
{
name: 'ملت',
shortName: 'mel',
},
{
name: 'ملی ایران',
shortName: 'mli',
},
{
name: 'مهر اقتصاد',
shortName: 'meg',
},
{
name: 'کارآفرین',
shortName: 'kar',
},
{
name: 'تات',
shortName: 'tat',
},
],
})
}
if ((await prisma.bankBranch.count()) === 0) {
await prisma.bankBranch.createMany({
data: [
{
bankId: 1,
name: 'شعبه مرکزی',
code: '001',
},
{
bankId: 2,
name: 'شعبه مرکزی',
code: '002',
},
{
bankId: 1,
name: 'شعبه ونک',
code: '003',
},
],
})
}
if ((await prisma.bankAccount.count()) === 0) {
await prisma.bankAccount.createMany({
data: [
{
branchId: 1,
accountNumber: '1234567890',
name: 'حساب اصلی آینده',
iban: 'IR000123456789012345678901',
},
{
branchId: 2,
accountNumber: '0987654321',
name: 'حساب اصلی ایران زمین',
iban: 'IR000987654321098765432109',
},
{
branchId: 3,
accountNumber: '1122334455',
name: 'حساب ونک آینده',
iban: 'IR000112233445566778899001',
},
],
})
}
if ((await prisma.inventoryBankAccount.count()) === 0) {
const inventories = await prisma.inventory.findMany()
const bankAccounts = await prisma.bankAccount.findMany()
if (inventories.length || bankAccounts.length) {
// Assign bank accounts to inventories
const inventoryBankAccounts = []
for (let i = 0; i < inventories.length; i++) {
const inventory = inventories[i]
const bankAccount = bankAccounts[i % bankAccounts.length] // Cycle through bank accounts
// @ts-ignore
inventoryBankAccounts.push({
inventoryId: inventory.id,
bankAccountId: bankAccount.id,
})
}
await prisma.inventoryBankAccount.createMany({
data: inventoryBankAccounts,
})
}
}
if ((await prisma.posAccount.count()) === 0) {
const inventories = await prisma.inventory.findMany({
where: { isPointOfSale: true },
})
const inventoryBankAccounts = await prisma.inventoryBankAccount.findMany()
const posAccounts = []
for (let i = 0; i < inventories.length; i++) {
const inventory = inventories[i]
// Find a bank account assigned to this inventory
const inventoryBankAccount = inventoryBankAccounts.find(
iba => iba.inventoryId === inventory.id,
)
if (inventoryBankAccount) {
// @ts-ignore
posAccounts.push({
name: `پوز ${inventory.name}`,
code: `POS${(i + 1).toString().padStart(3, '0')}`,
description: `پوز فروشگاه ${inventory.name}`,
inventoryId: inventory.id,
bankAccountId: inventoryBankAccount.bankAccountId,
})
}
}
await prisma.posAccount.createMany({
data: posAccounts,
})
}
// Seed purchase, transfer, and sales transactions
// const inventories = await prisma.inventory.findMany()
// const products = await prisma.product.findMany({ take: 5 }) // select 5 products for demo
// const supplier = await prisma.supplier.findFirst()
// const customers = await prisma.customer.findMany({ take: 2 })
// if (supplier && customers.length > 0) {
// PurchaseReceiptsService.prototype.create({
// code: '123123',
// inventoryId: inventories[0].id,
// supplierId: supplier.id,
// paidAmount: 0,
// totalAmount: products.reduce((acc, a) => (acc += Number(a.salePrice) * 10), 0),
// items: products.map(product => ({
// productId: product.id,
// count: 10,
// unitPrice: Number(product.salePrice),
// total: 10 * Number(product.salePrice),
// })),
// })
// }
}
main()
.then(async () => {
await prisma.$disconnect()
})
.catch(async e => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})