30 lines
783 B
TypeScript
30 lines
783 B
TypeScript
import type { EntityManager } from '@mikro-orm/core';
|
|
import { product } from '../modules/products/entities/product.entity';
|
|
import { Inventory } from '../modules/inventory/entities/inventory.entity';
|
|
|
|
export class InventorySeeder {
|
|
async run(em: EntityManager): Promise<void> {
|
|
const products = await em.find(product, {});
|
|
|
|
for (const product of products) {
|
|
// Check if inventory already exists for this product
|
|
const existingInventory = await em.findOne(Inventory, {
|
|
product: { id: product.id },
|
|
});
|
|
|
|
if (!existingInventory) {
|
|
const inventory = em.create(Inventory, {
|
|
product,
|
|
totalStock: 50,
|
|
availableStock: 50,
|
|
});
|
|
|
|
em.persist(inventory);
|
|
}
|
|
}
|
|
|
|
await em.flush();
|
|
}
|
|
}
|
|
|