test
This commit is contained in:
parent
9eaf8ddfc5
commit
22d4306b2a
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"nxConsole.generateAiAgentRules": true
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ import { AppController } from './app.controller';
|
|||
import { AppService } from './app.service';
|
||||
import { LanguageController } from './language/language.controller';
|
||||
import { I18nextModule } from './i18next/i18next.module';
|
||||
import { ShopCategoryDocumentModule } from './shop-category-document/shop-category-document.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
|
|
@ -28,6 +29,7 @@ import { I18nextModule } from './i18next/i18next.module';
|
|||
ShopCategoryModule,
|
||||
LanguageModule,
|
||||
I18nextModule,
|
||||
ShopCategoryDocumentModule,
|
||||
],
|
||||
controllers: [AppController, LanguageController],
|
||||
providers: [AppService],
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ export class LanguageService {
|
|||
}
|
||||
|
||||
/** Find One by ID */
|
||||
async findOneWithID(id: string, lang: string): Promise<ApiResponse<Language>> {
|
||||
async findOneWithID(id: string, lang: string): Promise<ApiResponse<any>> {
|
||||
try {
|
||||
const language = await this.languageModel.findOne({ ID: id }).exec();
|
||||
if (!language) {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
import { Controller } from '@nestjs/common';
|
||||
|
||||
@Controller('shop-category-document')
|
||||
export class ShopCategoryDocumentController {}
|
||||
|
|
@ -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 {}
|
||||
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
|
@ -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 };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@ import { IsMongoId } from 'class-validator';
|
|||
export class CreateShopCategoryDto {
|
||||
@IsString({ message: i18nValidationMessage('validation.isString') })
|
||||
@IsNotEmpty({ message: i18nValidationMessage('validation.notEmpty') })
|
||||
@IsOptional() // اگر اجباری است، این را حذف کنید
|
||||
@IsOptional()
|
||||
Name?: string;
|
||||
|
||||
@IsString({ message: i18nValidationMessage('validation.isString') })
|
||||
|
|
@ -21,7 +21,7 @@ export class CreateShopCategoryDto {
|
|||
@IsOptional()
|
||||
ParentCategory?: Types.ObjectId;
|
||||
|
||||
@IsMongoId({ message: i18nValidationMessage('validation.isMongoId') })
|
||||
@IsString({ message: i18nValidationMessage('validation.isString') })
|
||||
@IsOptional()
|
||||
Language?: Types.ObjectId;
|
||||
Language?: string
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,16 @@
|
|||
import { Module } from '@nestjs/common';
|
||||
import { forwardRef, Module } from '@nestjs/common';
|
||||
import { ShopCategoryController } from './shop-category.controller';
|
||||
import { ShopCategoryService } from './shop-category.service';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
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({
|
||||
imports: [
|
||||
MongooseModule.forFeature([{ name: ShopCategory.name, schema: ShopCategorySchema }]),
|
||||
LanguageModule,forwardRef(() => ShopCategoryDocumentModule)
|
||||
],
|
||||
controllers: [ShopCategoryController],
|
||||
providers: [ShopCategoryService],
|
||||
|
|
|
|||
|
|
@ -1,47 +1,126 @@
|
|||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { forwardRef, Inject, Injectable } from '@nestjs/common';
|
||||
import { InjectModel } from '@nestjs/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 { 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()
|
||||
export class ShopCategoryService {
|
||||
constructor(
|
||||
@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[]> {
|
||||
return this.shopCategoryModel.find()
|
||||
.populate('Tags')
|
||||
.populate('ParentCategory')
|
||||
.populate('Document')
|
||||
.exec();
|
||||
}
|
||||
const result = await newShopCategory.save();
|
||||
|
||||
async findOne(id: string): Promise<ShopCategory> {
|
||||
const category = await this.shopCategoryModel.findOne({ ID: id })
|
||||
.populate('Tags')
|
||||
.populate('ParentCategory')
|
||||
.populate('Document')
|
||||
.exec();
|
||||
if (!category) {
|
||||
throw new NotFoundException(`ShopCategory with ID ${id} not found`);
|
||||
}
|
||||
return category;
|
||||
}
|
||||
const createdDocument = await this.shopCategoryDocumentService.create(
|
||||
{
|
||||
Name: createShopCategoryDto.Name,
|
||||
Description: createShopCategoryDto.Description,
|
||||
Language: createShopCategoryDto.Language,
|
||||
ShopCategory: result.ID
|
||||
},
|
||||
lang
|
||||
);
|
||||
|
||||
if (!createdDocument.success) {
|
||||
return { success: false, message: createdDocument.message, data: null, errors: createdDocument.errors };
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<void> {
|
||||
const result = await this.shopCategoryModel.deleteOne({ ID: id }).exec();
|
||||
if (result.deletedCount === 0) {
|
||||
throw new NotFoundException(`ShopCategory with ID ${id} not found`);
|
||||
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 };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async findAll(lang: string): Promise<ApiResponse> {
|
||||
try {
|
||||
const categories = await this.shopCategoryModel.find()
|
||||
.populate('Tags')
|
||||
.populate('ParentCategory')
|
||||
.populate('Document')
|
||||
.exec();
|
||||
|
||||
const message = await this.i18n.t('retrieved_all', { lng: lang, ns: 'shopCategory' });
|
||||
return { success: true, message, data: categories };
|
||||
} catch (error) {
|
||||
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 };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ export class ShopCategory {
|
|||
Document: Types.ObjectId;
|
||||
|
||||
@Prop()
|
||||
Status: string;
|
||||
Status: boolean;
|
||||
|
||||
@Prop({ type: Date, default: Date.now })
|
||||
createdAt: Date;
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ export class ShopCategoryDocument {
|
|||
Tags: Types.ObjectId[];
|
||||
|
||||
@Prop()
|
||||
Status: string;
|
||||
Status: boolean;
|
||||
|
||||
@Prop({ type: Types.ObjectId, ref: 'Language' })
|
||||
Language: Types.ObjectId;
|
||||
|
|
|
|||
Loading…
Reference in New Issue