fartak/src/discount-type/discount-type.service.ts

64 lines
1.9 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { DiscountType,DiscountTypeDocument} from '../schemas/discountType.schema';
import { Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
import { CreatDiscountTypeDto } from './dto/discountType.dto';
import { CreateSellerResponse } from 'src/interfaces/request';
import { v4 as uuidv4 } from 'uuid';
import mongoose from 'mongoose';
@Injectable()
export class DiscountTypeService {
constructor(@InjectModel(DiscountType.name) private discountTypeModel: Model<DiscountTypeDocument>){}
async creatDiscountType (creatDiscountType:CreatDiscountTypeDto) : Promise<CreateSellerResponse> {
const newUuid: string = uuidv4();
try{
const newDiscountType = new this.discountTypeModel({
Name:creatDiscountType.Name,
Description:creatDiscountType.Description,
ID:newUuid
})
const result = newDiscountType.save()
return{
"message":"دسته بندی تخفیف جدید با موفقیت ایجاد شد . ",
"status":200,
"data":newDiscountType
}
}catch(e){
console.log(e)
return{
"message":"دسته بندی تخفیف جدید ذخیره نشد",
"status":401,
"data":null
}
}
}
async findDiscountType(id:string) :Promise <CreateSellerResponse> {
const category = await this.discountTypeModel.findOne({ID:id})
if(category){
return {
"message":"این دسته بندی تخفبف موجود است",
"status":200,
"data":category
}
}else{
return {
"message":"این دسته بندی تخفیف وجود ندارد",
"status":404,
"data":null
}
}
}
}