120 lines
3.6 KiB
TypeScript
120 lines
3.6 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectModel } from '@nestjs/mongoose';
|
|
import { Discount, DiscountDocument } from 'src/schemas/discount.schema';
|
|
import { Model, Types } from 'mongoose';
|
|
import { CategoryService } from 'src/category/category.service';
|
|
import { ProductService } from 'src/product/product.service';
|
|
import { DiscountTypeService } from 'src/discount-type/discount-type.service';
|
|
import { CreateDiscountDto } from './dto/discount.dto';
|
|
import { CreateSellerResponse } from 'src/interfaces/request';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { ShopService } from 'src/shop/shop.service';
|
|
|
|
@Injectable()
|
|
export class DiscountService {
|
|
constructor(
|
|
@InjectModel(Discount.name)
|
|
private readonly discountModel: Model<DiscountDocument>,
|
|
private readonly shopService: ShopService,
|
|
private readonly productService: ProductService,
|
|
private readonly discountTypeService: DiscountTypeService,
|
|
) {}
|
|
|
|
async creatDiscount(
|
|
creatDiscountDto: CreateDiscountDto,
|
|
user: any,
|
|
): Promise<CreateSellerResponse> {
|
|
const newUuid: string = uuidv4();
|
|
|
|
|
|
console.log("creatDiscountDto")
|
|
console.log(creatDiscountDto)
|
|
const existCategory = await this.discountTypeService.findDiscountType(creatDiscountDto.Type);
|
|
if (existCategory.status !== 200 || !existCategory.data) {
|
|
return {
|
|
message: 'نوع تخفیف ثبت نشده است',
|
|
status: 404,
|
|
data: null,
|
|
};
|
|
}
|
|
|
|
const existShop = await this.shopService.findShopSingle(creatDiscountDto.Shop);
|
|
if (existShop.status !== 200 || !existShop.data) {
|
|
return {
|
|
message: 'فروشگاه ثبت نشده است',
|
|
status: 404,
|
|
data: null,
|
|
};
|
|
}
|
|
|
|
if (!existShop.data.Seller) {
|
|
return {
|
|
message: 'اطلاعات فروشنده فروشگاه ناقص است',
|
|
status: 500,
|
|
data: null,
|
|
};
|
|
}
|
|
|
|
const sellerId = new Types.ObjectId(user);
|
|
const shopSellerId = existShop.data.Seller;
|
|
|
|
if (!sellerId.equals(shopSellerId)) {
|
|
return {
|
|
message: 'این فروشنده مجاز به ثبت تخفیف برای این فروشگاه نیست',
|
|
status: 403,
|
|
data: null,
|
|
};
|
|
}
|
|
|
|
const existProduct = await this.productService.findProductSingle(creatDiscountDto.Product);
|
|
if (existProduct.status !== 200 || !existProduct.data) {
|
|
return {
|
|
message: 'محصول ثبت نشده است',
|
|
status: 404,
|
|
data: null,
|
|
};
|
|
}
|
|
|
|
|
|
if (!existProduct.data.Shop.equals(existShop.data._id)) {
|
|
return {
|
|
message: 'این محصول در این فروشگاه ثبت نشده است',
|
|
status: 400,
|
|
data: null,
|
|
};
|
|
}
|
|
|
|
try {
|
|
const newDiscount = new this.discountModel({
|
|
Shop: existShop.data._id,
|
|
Product: existProduct.data._id,
|
|
Type: existCategory.data._id,
|
|
Description: creatDiscountDto.Description,
|
|
StartDate: creatDiscountDto.Start,
|
|
EndDate: creatDiscountDto.End,
|
|
StartTime: creatDiscountDto.StartTime,
|
|
EndTime: creatDiscountDto.EndTime,
|
|
Radius: Number(creatDiscountDto.Radius),
|
|
QRcode: "",
|
|
Status: true,
|
|
ID: newUuid,
|
|
});
|
|
|
|
await newDiscount.save();
|
|
|
|
return {
|
|
data: newDiscount,
|
|
message: 'تخفیف با موفقیت ایجاد شد',
|
|
status: 200,
|
|
};
|
|
} catch (e) {
|
|
console.error('Error saving discount:', e);
|
|
return {
|
|
data: null,
|
|
message: 'ثبت تخفیف با شکست مواجه شد',
|
|
status: 500,
|
|
};
|
|
}
|
|
}
|
|
}
|