attribute seeder

This commit is contained in:
2026-01-27 15:38:48 +03:30
parent e2b065aa2b
commit 355d4afaa2
7 changed files with 129 additions and 7 deletions
+2 -2
View File
@@ -279,7 +279,7 @@ export class OrderService {
}
async updateStatus(orderId: string, newStatus: OrderStatusEnum) {
const order = await this.findOrderOrFail(orderId)
order.status = newStatus
@@ -344,7 +344,7 @@ export class OrderService {
em.persist(order)
for (const item of items) {
const product = products.find(p => Number(p.id) === item.productId)
const product = products.find(p => Number(p.id) === Number(item.productId))
if (!product) {
throw new BadRequestException("Product not found!")
+13 -2
View File
@@ -6,6 +6,8 @@ import { ProductsSeeder } from './product.seeder';
import { AdminsSeeder } from './admins.seeder';
import { UsersSeeder } from './users.seeder';
import { CategoriesSeeder } from './categories.seeder';
import { AttributesSeeder } from './attribute.seeder';
import { AttributeValueSeeder } from './attribute-value.seeder';
// import { NotificationsSeeder } from './notifications.seeder';
@@ -32,7 +34,7 @@ export class DatabaseSeeder extends Seeder {
const categoriesSeeder = new CategoriesSeeder();
const categoriesMap = await categoriesSeeder.run(em);
console.info(`[Seeder] Step 8/${TOTAL_STEPS}: Done`);
// 9. Create products
console.info(`[Seeder] Step 9/${TOTAL_STEPS}: Creating products`);
@@ -40,8 +42,17 @@ export class DatabaseSeeder extends Seeder {
await productsSeeder.run(em, categoriesMap);
console.info(`[Seeder] Step 9/${TOTAL_STEPS}: Done`);
// 10. Create products
console.info(`[Seeder] Step 9/${TOTAL_STEPS}: Creating attributes`);
const attributesSeeder = new AttributesSeeder();
await attributesSeeder.run(em);
console.info(`[Seeder] Step 9/${TOTAL_STEPS}: Done`);
// 11. Create products
console.info(`[Seeder] Step 10/${TOTAL_STEPS}: Creating attributes`);
const attributesValueSeeder = new AttributeValueSeeder();
await attributesValueSeeder.run(em);
console.info(`[Seeder] Step 11/${TOTAL_STEPS}: Done`);
// 12. Create Admins
console.info(`[Seeder] Step 12/${TOTAL_STEPS}: Creating admins`);
+30
View File
@@ -0,0 +1,30 @@
import type { EntityManager } from '@mikro-orm/core';
import { Attribute } from 'src/modules/product/entities/attribute.entity';
import { attributeValueData } from './data/attribute-value.data';
import { AttributeValue } from 'src/modules/product/entities/attribute-value.entity';
export class AttributeValueSeeder {
async run(
em: EntityManager,
): Promise<void> {
const attributes = await em.findAll(Attribute)
for (const attr of attributes) {
const attrValues = attributeValueData.filter(a => a.attributeName === attr.name)
for (const av of attrValues) {
const a= em.create(AttributeValue, {
value: av.value,
attribute: attr
});
em.persist(a)
}
}
await em.flush();
}
}
+28
View File
@@ -0,0 +1,28 @@
import type { EntityManager } from '@mikro-orm/core';
import { Product } from '../modules/product/entities/product.entity';
import { attributeData } from './data/attribute.data';
import { Attribute } from 'src/modules/product/entities/attribute.entity';
export class AttributesSeeder {
async run(
em: EntityManager,
): Promise<void> {
const products = await em.findAll(Product)
for (const product of products) {
for (const attrData of attributeData) {
const attr = em.create(Attribute, {
name: attrData.name,
type: attrData.type,
isRequired: attrData.isRequired,
order: 1,
product
});
em.persist(attr);
}
}
await em.flush();
}
}
+27
View File
@@ -0,0 +1,27 @@
import { FieldType } from "src/modules/print/interface/print";
export interface AttributeValueData {
value: string;
attributeName: string
}
export const attributeValueData: AttributeValueData[] = [
{
attributeName: 'گراماژ کاغذ',
value: '70',
},
{
attributeName: 'گراماژ کاغذ',
value: '80',
},
{
attributeName: 'رنگ',
value: 'آبی',
},
{
attributeName: 'رنگ',
value: 'قرمز',
},
]
+29
View File
@@ -0,0 +1,29 @@
import { FieldType } from "src/modules/print/interface/print";
export interface AttributeData {
name: string;
isRequired: boolean;
type: FieldType,
}
export const attributeData: AttributeData[] = [
{
name: 'گراماژ کاغذ',
type: FieldType.radio,
isRequired: true,
},
{
name: 'رنگ',
type: FieldType.select,
isRequired: true,
},
{
name: 'ویژگی نوشتنی',
type: FieldType.text,
isRequired: true,
},
]
-3
View File
@@ -12,15 +12,12 @@ export class ProductsSeeder {
const key = `${productData.categoryTitle}`;
const normalize = (s: string) => (s || '').replace(/\s+/g, ' ').trim();
let category = categoriesMap.get(key);
if (!category) {
return
}
const product = em.create(Product, {
title: productData.title,
desc: productData.desc,