This commit is contained in:
vahidrezvani 2025-08-23 14:16:53 +03:30
parent 9eaf8ddfc5
commit 22d4306b2a
15 changed files with 358 additions and 37 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"nxConsole.generateAiAgentRules": true
}

View File

@ -10,6 +10,7 @@ import { AppController } from './app.controller';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { LanguageController } from './language/language.controller'; import { LanguageController } from './language/language.controller';
import { I18nextModule } from './i18next/i18next.module'; import { I18nextModule } from './i18next/i18next.module';
import { ShopCategoryDocumentModule } from './shop-category-document/shop-category-document.module';
@Module({ @Module({
imports: [ imports: [
@ -28,6 +29,7 @@ import { I18nextModule } from './i18next/i18next.module';
ShopCategoryModule, ShopCategoryModule,
LanguageModule, LanguageModule,
I18nextModule, I18nextModule,
ShopCategoryDocumentModule,
], ],
controllers: [AppController, LanguageController], controllers: [AppController, LanguageController],
providers: [AppService], providers: [AppService],

View File

@ -84,7 +84,7 @@ export class LanguageService {
} }
/** Find One by ID */ /** Find One by ID */
async findOneWithID(id: string, lang: string): Promise<ApiResponse<Language>> { async findOneWithID(id: string, lang: string): Promise<ApiResponse<any>> {
try { try {
const language = await this.languageModel.findOne({ ID: id }).exec(); const language = await this.languageModel.findOne({ ID: id }).exec();
if (!language) { if (!language) {

View File

@ -0,0 +1,25 @@
import { IsString, IsOptional, IsNumber, Min, Max, IsNotEmpty } from 'class-validator';
import { Types } from 'mongoose';
import { i18nValidationMessage } from 'nestjs-i18n';
import { IsMongoId } from 'class-validator';
export class CreateShopCategoryDocumentDto {
@IsString({ message: i18nValidationMessage('validation.isString') })
@IsNotEmpty({ message: i18nValidationMessage('validation.notEmpty') })
Name?: string;
@IsString({ message: i18nValidationMessage('validation.isString') })
@IsNotEmpty({ message: i18nValidationMessage('validation.notEmpty') })
Description?: string;
@IsString({ message: i18nValidationMessage('validation.isMongoId') })
@IsNotEmpty({ message: i18nValidationMessage('validation.notEmpty') })
ShopCategory?:string;
@IsString({ message: i18nValidationMessage('validation.isString') })
@IsNotEmpty({ message: i18nValidationMessage('validation.notEmpty') })
Language?: string
}

View File

@ -0,0 +1,19 @@
import { IsString, IsOptional, IsMongoId, IsNumber, Min, Max } from 'class-validator';
import { Types } from 'mongoose';
import { i18nValidationMessage } from 'nestjs-i18n';
export class UpdateShopCategoryDocumentDto {
@IsString({ message: i18nValidationMessage('validation.isString') })
@IsOptional()
Name?: string;
@IsString({ message: i18nValidationMessage('validation.isString') })
@IsOptional()
Description?: string;
@IsString({ message: i18nValidationMessage('validation.isMongoId') })
@IsOptional()
Language?: string;
}

View File

@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ShopCategoryDocumentController } from './shop-category-document.controller';
describe('ShopCategoryDocumentController', () => {
let controller: ShopCategoryDocumentController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ShopCategoryDocumentController],
}).compile();
controller = module.get<ShopCategoryDocumentController>(
ShopCategoryDocumentController
);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@ -0,0 +1,4 @@
import { Controller } from '@nestjs/common';
@Controller('shop-category-document')
export class ShopCategoryDocumentController {}

View File

@ -0,0 +1,16 @@
import { forwardRef, Module } from '@nestjs/common';
import { ShopCategoryDocumentService } from './shop-category-document.service';
import { ShopCategoryDocumentController } from './shop-category-document.controller';
import { MongooseModule } from '@nestjs/mongoose';
import { ShopCategoryDocument, ShopCategoryDocumentSchema } from '../../schemas/shopCategoryDocument';
import { ShopCategoryService } from '../shop-category/shop-category.service';
import { ShopCategoryModule } from '../shop-category/shop-category.module';
import { LanguageModule } from '../language/language.module';
@Module({
imports:[MongooseModule.forFeature([{ name:ShopCategoryDocument.name, schema:ShopCategoryDocumentSchema }]),
forwardRef(() => ShopCategoryModule),LanguageModule],
providers: [ShopCategoryDocumentService],
controllers: [ShopCategoryDocumentController],
})
export class ShopCategoryDocumentModule {}

View File

@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ShopCategoryDocumentService } from './shop-category-document.service';
describe('ShopCategoryDocumentService', () => {
let service: ShopCategoryDocumentService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ShopCategoryDocumentService],
}).compile();
service = module.get<ShopCategoryDocumentService>(
ShopCategoryDocumentService
);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@ -0,0 +1,129 @@
import { forwardRef, Inject, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { ShopCategoryDoc } from '../../schemas/shopCategory.schema';
import { ShopCategoryDocument, ShopCategoryDocumentSchema } from '../../schemas/shopCategoryDocument';
import { Model } from 'mongoose';
import { v4 as uuidv4 } from 'uuid';
import { I18nService } from 'nestjs-i18n';
import { CreateShopCategoryDocumentDto } from './dto/creat-shop-category-document.dto';
import { ApiResponse, LanguageService } from '../language/language.service';
import i18next from 'i18next';
import { UpdateShopCategoryDocumentDto } from './dto/update-shop-category-document.dto';
import { ShopCategoryService } from '../shop-category/shop-category.service';
@Injectable()
export class ShopCategoryDocumentService {
constructor(@InjectModel(ShopCategoryDocument.name) private shopCategoryDocumentModel: Model<ShopCategoryDoc>,
private readonly languageService: LanguageService,
@Inject(forwardRef(() => ShopCategoryService)) private readonly shopCategoryService: ShopCategoryService,
@Inject('I18NEXT') private readonly i18n: typeof i18next) { }
async create(
createShopCategoryDocumentDto: CreateShopCategoryDocumentDto,
lang: string
): Promise<ApiResponse> {
try {
// بررسی زبان
const language = await this.languageService.findOneWithID(
createShopCategoryDocumentDto.Language,
lang
);
if (!language.success) {
return {
success: false,
message: await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' }),
data: null
};
}
const shopCategory = await this.shopCategoryService.findOne(createShopCategoryDocumentDto.ShopCategory, lang)
if (!shopCategory.success) {
return {
success: false,
message: await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' }),
data: null
};
}
const createdDoc = new this.shopCategoryDocumentModel({
Name: createShopCategoryDocumentDto.Name,
Description: createShopCategoryDocumentDto.Description,
Language: language.data._id,
ID: uuidv4(),
Status: true,
Tags: [],
ShopCategory: shopCategory.data._id,
createdAt: new Date(),
updatedAt: new Date(),
});
const result = await createdDoc.save();
const message = await this.i18n.t('created', { lng: lang, ns: 'shopCategoryDocument' });
return { success: true, message, data: result };
} catch (error) {
const message = await this.i18n.t('create_error', { lng: lang, ns: 'shopCategoryDocument' });
return { success: false, message, errors: error.message };
}
}
async update(
id: string,
updateShopCategoryDocumentDto: UpdateShopCategoryDocumentDto,
lang: string
): Promise<ApiResponse> {
try {
const existingDoc = await this.shopCategoryDocumentModel.findOne({ ID: id }).exec();
if (!existingDoc) {
const message = await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' });
return { success: false, message, data: null };
}
if (updateShopCategoryDocumentDto.Language) {
const language = await this.languageService.findOneWithID(updateShopCategoryDocumentDto.Language, 'en');
if (!language.success) {
const message = await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' });
return { success: false, message, data: null };
}
updateShopCategoryDocumentDto.Language = language.data._id;
}
// آپدیت فیلدها
Object.assign(existingDoc, updateShopCategoryDocumentDto, { updatedAt: new Date() });
const updatedDoc = await existingDoc.save();
const message = await this.i18n.t('updated', { lng: lang, ns: 'shopCategoryDocument' });
return { success: true, message, data: updatedDoc };
} catch (error) {
const message = await this.i18n.t('update_error', { lng: lang, ns: 'shopCategoryDocument' });
return { success: false, message, errors: error.message };
}
}
async toggleStatus(
id: string,
lang: string
): Promise<ApiResponse> {
try {
const existingDoc = await this.shopCategoryDocumentModel.findOne({ ID: id }).exec();
if (!existingDoc) {
const message = await this.i18n.t('not_found', { lng: lang, ns: 'shopCategoryDocument' });
return { success: false, message, data: null };
}
existingDoc.Status = !existingDoc.Status;
existingDoc.updatedAt = new Date();
const updatedDoc = await existingDoc.save();
const message = await this.i18n.t('status_toggled', { lng: lang, ns: 'shopCategoryDocument' });
return { success: true, message, data: updatedDoc };
} catch (error) {
const message = await this.i18n.t('update_error', { lng: lang, ns: 'shopCategoryDocument' });
return { success: false, message, errors: error.message };
}
}
}

View File

@ -6,7 +6,7 @@ import { IsMongoId } from 'class-validator';
export class CreateShopCategoryDto { export class CreateShopCategoryDto {
@IsString({ message: i18nValidationMessage('validation.isString') }) @IsString({ message: i18nValidationMessage('validation.isString') })
@IsNotEmpty({ message: i18nValidationMessage('validation.notEmpty') }) @IsNotEmpty({ message: i18nValidationMessage('validation.notEmpty') })
@IsOptional() // اگر اجباری است، این را حذف کنید @IsOptional()
Name?: string; Name?: string;
@IsString({ message: i18nValidationMessage('validation.isString') }) @IsString({ message: i18nValidationMessage('validation.isString') })
@ -21,7 +21,7 @@ export class CreateShopCategoryDto {
@IsOptional() @IsOptional()
ParentCategory?: Types.ObjectId; ParentCategory?: Types.ObjectId;
@IsMongoId({ message: i18nValidationMessage('validation.isMongoId') }) @IsString({ message: i18nValidationMessage('validation.isString') })
@IsOptional() @IsOptional()
Language?: Types.ObjectId; Language?: string
} }

View File

@ -1,12 +1,16 @@
import { Module } from '@nestjs/common'; import { forwardRef, Module } from '@nestjs/common';
import { ShopCategoryController } from './shop-category.controller'; import { ShopCategoryController } from './shop-category.controller';
import { ShopCategoryService } from './shop-category.service'; import { ShopCategoryService } from './shop-category.service';
import { MongooseModule } from '@nestjs/mongoose'; import { MongooseModule } from '@nestjs/mongoose';
import { ShopCategory, ShopCategorySchema } from '../../schemas/shopCategory.schema'; import { ShopCategory, ShopCategorySchema } from '../../schemas/shopCategory.schema';
import { LanguageModule } from '../language/language.module';
import { ShopCategoryDocument } from '../../schemas/shopCategoryDocument';
import { ShopCategoryDocumentModule } from '../shop-category-document/shop-category-document.module';
@Module({ @Module({
imports: [ imports: [
MongooseModule.forFeature([{ name: ShopCategory.name, schema: ShopCategorySchema }]), MongooseModule.forFeature([{ name: ShopCategory.name, schema: ShopCategorySchema }]),
LanguageModule,forwardRef(() => ShopCategoryDocumentModule)
], ],
controllers: [ShopCategoryController], controllers: [ShopCategoryController],
providers: [ShopCategoryService], providers: [ShopCategoryService],

View File

@ -1,47 +1,126 @@
import { Injectable, NotFoundException } from '@nestjs/common'; import { forwardRef, Inject, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose'; import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose'; import { Model } from 'mongoose';
import { ShopCategory,ShopCategoryDoc } from '../../schemas/shopCategory.schema'; import { v4 as uuidv4 } from 'uuid';
import { ShopCategory, ShopCategoryDoc } from '../../schemas/shopCategory.schema';
import { CreateShopCategoryDto } from './dto/create-shop-category.dto'; import { CreateShopCategoryDto } from './dto/create-shop-category.dto';
import { I18n, I18nContext } from 'nestjs-i18n'; import { LanguageService, ApiResponse } from '../language/language.service';
import { ShopCategoryDocumentService } from '../shop-category-document/shop-category-document.service';
import { I18nService } from 'nestjs-i18n';
import i18next from 'i18next';
@Injectable() @Injectable()
export class ShopCategoryService { export class ShopCategoryService {
constructor( constructor(
@InjectModel(ShopCategory.name) private shopCategoryModel: Model<ShopCategoryDoc>, @InjectModel(ShopCategory.name) private shopCategoryModel: Model<ShopCategoryDoc>,
private readonly languageService: LanguageService,
private readonly shopCategoryDocumentService: ShopCategoryDocumentService,
@Inject('I18NEXT') private readonly i18n: typeof i18next
) {} ) {}
async create(createShopCategoryDto: CreateShopCategoryDto): Promise<any> { async create(createShopCategoryDto: CreateShopCategoryDto, lang: string): Promise<ApiResponse> {
try {
const language = await this.languageService.findOneWithID(createShopCategoryDto.Language, lang);
if (!language.success) {
return { success: false, message: await this.i18n.t('not_found', { lng: lang, ns: 'shopCategory' }), data: null };
}
let parent = null;
if (createShopCategoryDto.ParentCategory) {
parent = await this.shopCategoryModel.findOne({ ID: createShopCategoryDto.ParentCategory }).exec();
if (!parent) {
return { success: false, message: await this.i18n.t('parent_not_found', { lng: lang, ns: 'shopCategory' }), data: null };
}
}
} const newShopCategory = new this.shopCategoryModel({
ID: uuidv4(),
Name: createShopCategoryDto.Name,
Description: createShopCategoryDto.Description,
Icon: createShopCategoryDto.Icon,
ParentCategory: parent ? parent._id : null,
Status: true,
Document: null,
createdAt: new Date(),
updatedAt: new Date(),
Tags: []
});
async findAll(): Promise<ShopCategory[]> { const result = await newShopCategory.save();
return this.shopCategoryModel.find()
.populate('Tags')
.populate('ParentCategory')
.populate('Document')
.exec();
}
async findOne(id: string): Promise<ShopCategory> { const createdDocument = await this.shopCategoryDocumentService.create(
const category = await this.shopCategoryModel.findOne({ ID: id }) {
.populate('Tags') Name: createShopCategoryDto.Name,
.populate('ParentCategory') Description: createShopCategoryDto.Description,
.populate('Document') Language: createShopCategoryDto.Language,
.exec(); ShopCategory: result.ID
if (!category) { },
throw new NotFoundException(`ShopCategory with ID ${id} not found`); lang
);
if (!createdDocument.success) {
return { success: false, message: createdDocument.message, data: null, errors: createdDocument.errors };
}
await this.shopCategoryModel.updateOne(
{ _id: result._id },
{ $set: { Document: createdDocument.data._id } }
);
const message = await this.i18n.t('created', { lng: lang, ns: 'shopCategory' });
return { success: true, message, data: { ...result.toObject(), Document: createdDocument.data } };
} catch (error) {
const message = await this.i18n.t('create_error', { lng: lang, ns: 'shopCategory' });
return { success: false, message, errors: error.message };
} }
return category;
} }
async findAll(lang: string): Promise<ApiResponse> {
try {
const categories = await this.shopCategoryModel.find()
.populate('Tags')
.populate('ParentCategory')
.populate('Document')
.exec();
async remove(id: string): Promise<void> { const message = await this.i18n.t('retrieved_all', { lng: lang, ns: 'shopCategory' });
const result = await this.shopCategoryModel.deleteOne({ ID: id }).exec(); return { success: true, message, data: categories };
if (result.deletedCount === 0) { } catch (error) {
throw new NotFoundException(`ShopCategory with ID ${id} not found`); const message = await this.i18n.t('retrieve_error', { lng: lang, ns: 'shopCategory' });
return { success: false, message, errors: error.message };
}
}
async findOne(id: string, lang: string): Promise<ApiResponse> {
try {
const category = await this.shopCategoryModel.findOne({ ID: id })
.exec();
if (!category) {
const message = await this.i18n.t('not_found', { lng: lang, ns: 'shopCategory' });
return { success: false, message, data: null };
}
const message = await this.i18n.t('retrieved', { lng: lang, ns: 'shopCategory' });
return { success: true, message, data: category };
} catch (error) {
const message = await this.i18n.t('retrieve_error', { lng: lang, ns: 'shopCategory' });
return { success: false, message, errors: error.message };
}
}
async remove(id: string, lang: string): Promise<ApiResponse> {
try {
const result = await this.shopCategoryModel.deleteOne({ ID: id }).exec();
if (result.deletedCount === 0) {
const message = await this.i18n.t('not_found', { lng: lang, ns: 'shopCategory' });
return { success: false, message, data: null };
}
const message = await this.i18n.t('deleted', { lng: lang, ns: 'shopCategory' });
return { success: true, message, data: null };
} catch (error) {
const message = await this.i18n.t('delete_error', { lng: lang, ns: 'shopCategory' });
return { success: false, message, errors: error.message };
} }
} }
} }

View File

@ -28,7 +28,7 @@ export class ShopCategory {
Document: Types.ObjectId; Document: Types.ObjectId;
@Prop() @Prop()
Status: string; Status: boolean;
@Prop({ type: Date, default: Date.now }) @Prop({ type: Date, default: Date.now })
createdAt: Date; createdAt: Date;

View File

@ -21,7 +21,7 @@ export class ShopCategoryDocument {
Tags: Types.ObjectId[]; Tags: Types.ObjectId[];
@Prop() @Prop()
Status: string; Status: boolean;
@Prop({ type: Types.ObjectId, ref: 'Language' }) @Prop({ type: Types.ObjectId, ref: 'Language' })
Language: Types.ObjectId; Language: Types.ObjectId;