product verity

This commit is contained in:
morteza-mortezai
2025-11-29 08:52:05 +03:30
parent e54fc58b37
commit 37ecf9f449
35 changed files with 741 additions and 529 deletions
+50 -42
View File
@@ -2,14 +2,12 @@ import { faker } from "@faker-js/faker";
import { Types } from "mongoose";
faker.locale = "fa";
import { CategoryThemeEnum } from "../../common/enums/category.enum";
import { Logger } from "../../core/logging/logger";
import { AttributeValueModel } from "../../modules/category/models/attributeValue.model";
import { CategoryModel } from "../../modules/category/models/category.model";
import { CategoryAttributeModel } from "../../modules/category/models/CategoryAttribute.model";
import { ColorModel } from "../../modules/category/models/color.model";
import { MeterageModel } from "../../modules/category/models/meterage.model";
import { SizeModel } from "../../modules/category/models/size.model";
import { ThemeModel } from "../../modules/category/models/theme.model";
import { ThemeValueModel } from "../../modules/category/models/themeValue.model";
interface ICategorySeed {
title_fa: string;
@@ -18,7 +16,7 @@ interface ICategorySeed {
imageUrl: string;
description: string;
leaf: boolean;
theme?: CategoryThemeEnum;
themeType?: "color" | "size" | "meterage" | null;
parent?: Types.ObjectId;
}
@@ -123,7 +121,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
title_en: "mobile-phones",
leaf: true,
imageUrl: faker.image.imageUrl(),
theme: CategoryThemeEnum.Colored,
themeType: "color",
},
{
title_fa: "تبلت",
@@ -131,7 +129,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع تبلت ها",
title_en: "tablets",
leaf: true,
theme: CategoryThemeEnum.Colored,
themeType: "color",
imageUrl: faker.image.imageUrl(),
},
{
@@ -140,7 +138,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع ساعت هوشمند",
title_en: "smart-watch",
leaf: true,
theme: CategoryThemeEnum.Colored,
themeType: "color",
imageUrl: faker.image.imageUrl(),
},
{
@@ -149,7 +147,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع پاور بانک",
title_en: "power-banks",
leaf: true,
theme: CategoryThemeEnum.No_color_No_sized,
themeType: null,
imageUrl: faker.image.imageUrl(),
},
],
@@ -160,7 +158,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع خودکار و خودنویس",
title_en: "pens",
leaf: true,
theme: CategoryThemeEnum.Colored,
themeType: "color",
imageUrl: faker.image.imageUrl(),
},
{
@@ -169,7 +167,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع دفتر و کاغذ",
title_en: "paper-notebooks",
leaf: true,
theme: CategoryThemeEnum.Colored,
themeType: "color",
imageUrl: faker.image.imageUrl(),
},
],
@@ -180,7 +178,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع لامپ ها",
title_en: "bulbs",
leaf: true,
theme: CategoryThemeEnum.No_color_No_sized,
themeType: null,
imageUrl: faker.image.imageUrl(),
},
{
@@ -189,7 +187,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع کابل و سیم",
title_en: "cables-wires",
leaf: true,
theme: CategoryThemeEnum.No_color_No_sized,
themeType: null,
imageUrl: faker.image.imageUrl(),
},
],
@@ -200,7 +198,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع آچار و پیچ گوشتی",
title_en: "wrenches-screwdrivers",
leaf: true,
theme: CategoryThemeEnum.No_color_No_sized,
themeType: null,
imageUrl: faker.image.imageUrl(),
},
{
@@ -209,7 +207,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع دریل ها",
title_en: "drills",
leaf: true,
theme: CategoryThemeEnum.No_color_No_sized,
themeType: null,
imageUrl: faker.image.imageUrl(),
},
],
@@ -220,7 +218,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع تلویزیون",
title_en: "televisions",
leaf: true,
theme: CategoryThemeEnum.No_color_No_sized,
themeType: null,
imageUrl: faker.image.imageUrl(),
},
{
@@ -229,7 +227,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع اسپیکر",
title_en: "speakers",
leaf: true,
theme: CategoryThemeEnum.No_color_No_sized,
themeType: null,
imageUrl: faker.image.imageUrl(),
},
{
@@ -238,7 +236,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع هدفون",
title_en: "headphones",
leaf: true,
theme: CategoryThemeEnum.Colored,
themeType: "color",
imageUrl: faker.image.imageUrl(),
},
{
@@ -247,7 +245,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع دوربین عکاسی",
title_en: "cameras",
leaf: true,
theme: CategoryThemeEnum.No_color_No_sized,
themeType: null,
imageUrl: faker.image.imageUrl(),
},
],
@@ -258,7 +256,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع لپ تاپ",
title_en: "laptops",
leaf: true,
theme: CategoryThemeEnum.No_color_No_sized,
themeType: null,
imageUrl: faker.image.imageUrl(),
},
{
@@ -267,7 +265,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع موس و کیبورد",
title_en: "mouse-keyboards",
leaf: true,
theme: CategoryThemeEnum.No_color_No_sized,
themeType: null,
imageUrl: faker.image.imageUrl(),
},
],
@@ -278,7 +276,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع پلی استیشن",
title_en: "playstations",
leaf: true,
theme: CategoryThemeEnum.No_color_No_sized,
themeType: null,
imageUrl: faker.image.imageUrl(),
},
{
@@ -287,7 +285,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع ایکس باکس",
title_en: "xbox",
leaf: true,
theme: CategoryThemeEnum.No_color_No_sized,
themeType: null,
imageUrl: faker.image.imageUrl(),
},
],
@@ -298,7 +296,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع تجهیزات ورزشی",
title_en: "sports-equipment",
leaf: true,
theme: CategoryThemeEnum.No_color_No_sized,
themeType: null,
imageUrl: faker.image.imageUrl(),
},
{
@@ -307,7 +305,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع لوازم سفر",
title_en: "travel-equipment",
leaf: true,
theme: CategoryThemeEnum.No_color_No_sized,
themeType: null,
imageUrl: faker.image.imageUrl(),
},
],
@@ -318,7 +316,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع لباس زنانه",
title_en: "women-clothing",
leaf: true,
theme: CategoryThemeEnum.Sized,
themeType: "size",
imageUrl: faker.image.imageUrl(),
},
{
@@ -327,7 +325,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع لباس مردانه",
title_en: "men-clothing",
leaf: true,
theme: CategoryThemeEnum.Sized,
themeType: "size",
imageUrl: faker.image.imageUrl(),
},
],
@@ -338,7 +336,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع لوازم آشپزخانه",
title_en: "kitchen-appliances",
leaf: true,
theme: CategoryThemeEnum.No_color_No_sized,
themeType: null,
imageUrl: faker.image.imageUrl(),
},
{
@@ -347,7 +345,7 @@ const subcategories: { [key: string]: ICategorySeed[] } = {
description: "انواع لوازم نظافت",
title_en: "cleaning-appliances",
leaf: true,
theme: CategoryThemeEnum.No_color_No_sized,
themeType: null,
imageUrl: faker.image.imageUrl(),
},
],
@@ -461,9 +459,13 @@ export const seedCategories = async (logger: Logger) => {
try {
logger.info("Categories seeding started");
const colors = (await ColorModel.find()).map((color) => color._id);
const sizes = (await SizeModel.find()).map((size) => size._id);
const meterages = (await MeterageModel.find()).map((meterage) => meterage._id);
const colorTheme = await ThemeModel.findOne({ title: "رنگ" });
const sizeTheme = await ThemeModel.findOne({ title: "سایز" });
const meterageTheme = await ThemeModel.findOne({ title: "متراژ" });
const colors = colorTheme ? (await ThemeValueModel.find({ theme: colorTheme._id })).map((color) => color._id) : [];
const sizes = sizeTheme ? (await ThemeValueModel.find({ theme: sizeTheme._id })).map((size) => size._id) : [];
const meterages = meterageTheme ? (await ThemeValueModel.find({ theme: meterageTheme._id })).map((meterage) => meterage._id) : [];
const createdCategories: { [key: string]: Types.ObjectId } = {};
const createdSubCategories: { [key: string]: Types.ObjectId } = {};
@@ -483,16 +485,22 @@ export const seedCategories = async (logger: Logger) => {
const parentId = createdCategories[rootName];
if (parentId) {
for (const subcategory of subs) {
const { title_fa, icon, description, title_en, leaf, theme, imageUrl } = subcategory;
const { title_fa, icon, description, title_en, leaf, themeType, imageUrl } = subcategory;
const catVariants =
theme === CategoryThemeEnum.Colored
? [...colors]
: theme === CategoryThemeEnum.Meterage
? [...meterages]
: theme === CategoryThemeEnum.Sized
? [...sizes]
: [];
// Map themeType to theme ID and variants
let themeId: Types.ObjectId | undefined;
let catVariants: Types.ObjectId[] = [];
if (themeType === "color" && colorTheme) {
themeId = colorTheme._id;
catVariants = [...colors];
} else if (themeType === "size" && sizeTheme) {
themeId = sizeTheme._id;
catVariants = [...sizes];
} else if (themeType === "meterage" && meterageTheme) {
themeId = meterageTheme._id;
catVariants = [...meterages];
}
const subCategoryDoc = new CategoryModel({
title_fa,
@@ -501,7 +509,7 @@ export const seedCategories = async (logger: Logger) => {
title_en,
description,
leaf,
theme,
theme: themeId,
variants: catVariants,
parent: parentId,
});