|
|
|
|
@ -1,7 +1,10 @@
|
|
|
|
|
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 {
|
|
|
|
|
ShopCategoryDocument,
|
|
|
|
|
ShopCategoryDocumentSchema,
|
|
|
|
|
} from '../../schemas/shopCategoryDocument';
|
|
|
|
|
import { Model } from 'mongoose';
|
|
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
|
import { I18nService } from 'nestjs-i18n';
|
|
|
|
|
@ -13,117 +16,217 @@ 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
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 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 result = await createdDoc.save();
|
|
|
|
|
const message = await this.i18n.t('created', {
|
|
|
|
|
lng: lang,
|
|
|
|
|
ns: 'shopCategoryDocument',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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 };
|
|
|
|
|
}
|
|
|
|
|
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 };
|
|
|
|
|
}
|
|
|
|
|
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 };
|
|
|
|
|
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 };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
updateShopCategoryDocumentDto.Language = language.data._id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const updatedDoc = await existingDoc.save();
|
|
|
|
|
// آپدیت فیلدها
|
|
|
|
|
Object.assign(existingDoc, updateShopCategoryDocumentDto, {
|
|
|
|
|
updatedAt: new Date(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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 };
|
|
|
|
|
}
|
|
|
|
|
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 };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findAll(lang: string): Promise<ApiResponse> {
|
|
|
|
|
try {
|
|
|
|
|
const documents = await this.shopCategoryDocumentModel
|
|
|
|
|
.find()
|
|
|
|
|
.populate('Tags')
|
|
|
|
|
.populate('ShopCategory')
|
|
|
|
|
.populate('Language')
|
|
|
|
|
.exec();
|
|
|
|
|
|
|
|
|
|
const message = await this.i18n.t('retrieved_all', {
|
|
|
|
|
lng: lang,
|
|
|
|
|
ns: 'shopCategoryDocument',
|
|
|
|
|
});
|
|
|
|
|
return { success: true, message, data: documents };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const message = await this.i18n.t('retrieve_error', {
|
|
|
|
|
lng: lang,
|
|
|
|
|
ns: 'shopCategoryDocument',
|
|
|
|
|
});
|
|
|
|
|
return { success: false, message, errors: error.message };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async findOne(id: string, lang: string): Promise<ApiResponse> {
|
|
|
|
|
try {
|
|
|
|
|
const document = await this.shopCategoryDocumentModel
|
|
|
|
|
.findOne({ ID: id })
|
|
|
|
|
.populate('Tags')
|
|
|
|
|
.populate('ShopCategory')
|
|
|
|
|
.populate('Language')
|
|
|
|
|
.exec();
|
|
|
|
|
|
|
|
|
|
if (!document) {
|
|
|
|
|
const message = await this.i18n.t('not_found', {
|
|
|
|
|
lng: lang,
|
|
|
|
|
ns: 'shopCategoryDocument',
|
|
|
|
|
});
|
|
|
|
|
return { success: false, message, data: null };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const message = await this.i18n.t('retrieved', {
|
|
|
|
|
lng: lang,
|
|
|
|
|
ns: 'shopCategoryDocument',
|
|
|
|
|
});
|
|
|
|
|
return { success: true, message, data: document };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
const message = await this.i18n.t('retrieve_error', {
|
|
|
|
|
lng: lang,
|
|
|
|
|
ns: 'shopCategoryDocument',
|
|
|
|
|
});
|
|
|
|
|
return { success: false, message, errors: error.message };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|