update food
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { IsBoolean, IsOptional, IsString, IsNumber, IsArray, IsNotEmpty } from 'class-validator';
|
||||
import { IsBoolean, IsOptional, IsString, IsNumber, IsArray, IsNotEmpty, IsEnum } from 'class-validator';
|
||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import { MealType } from '../interface/food.interface';
|
||||
|
||||
export class CreateFoodDto {
|
||||
@IsNotEmpty()
|
||||
@@ -54,6 +55,19 @@ export class CreateFoodDto {
|
||||
@ApiPropertyOptional({ type: [String] })
|
||||
content?: string[];
|
||||
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@IsNumber({}, { each: true })
|
||||
@Type(() => Number)
|
||||
@ApiPropertyOptional({ type: [Number] })
|
||||
weekDays?: number[];
|
||||
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@IsEnum(MealType, { each: true })
|
||||
@ApiPropertyOptional({ enum: MealType, isArray: true })
|
||||
mealTypes?: MealType[];
|
||||
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
@Type(() => Number)
|
||||
@@ -138,6 +152,12 @@ export class CreateFoodDto {
|
||||
@ApiPropertyOptional({ example: 0 })
|
||||
discount?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
@Type(() => Number)
|
||||
@ApiPropertyOptional({ example: 0 })
|
||||
score?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
@ApiPropertyOptional({ example: false })
|
||||
|
||||
@@ -21,7 +21,7 @@ export class FoodService {
|
||||
private readonly restRepository: RestRepository,
|
||||
private readonly em: EntityManager,
|
||||
private readonly cacheService: CacheService,
|
||||
) {}
|
||||
) { }
|
||||
|
||||
async create(restId: string, createFoodDto: CreateFoodDto): Promise<Food> {
|
||||
const { categoryId, ...rest } = createFoodDto;
|
||||
@@ -36,13 +36,6 @@ export class FoodService {
|
||||
|
||||
// prepare data with defaults for required fields so repository.create typing is satisfied
|
||||
const data: RequiredEntityData<Food> = {
|
||||
// boolean/day flags
|
||||
sat: rest.sat ?? false,
|
||||
sun: rest.sun ?? false,
|
||||
mon: rest.mon ?? false,
|
||||
breakfast: rest.breakfast ?? false,
|
||||
noon: rest.noon ?? false,
|
||||
dinner: rest.dinner ?? false,
|
||||
desc: rest.desc,
|
||||
isActive: rest.isActive ?? true,
|
||||
inPlaceServe: rest.inPlaceServe ?? false,
|
||||
@@ -56,6 +49,10 @@ export class FoodService {
|
||||
price: rest.price ?? 0,
|
||||
prepareTime: rest.prepareTime ?? 0,
|
||||
images: rest.images ?? undefined,
|
||||
// new fields
|
||||
weekDays: rest.weekDays ?? [0, 1, 2, 3, 4, 5, 6],
|
||||
mealTypes: rest.mealTypes ?? [],
|
||||
score: rest.score ?? null,
|
||||
restaurant: restaurant,
|
||||
category: category,
|
||||
};
|
||||
|
||||
@@ -13,7 +13,7 @@ export class FoodRatingCronService {
|
||||
private readonly em: EntityManager,
|
||||
private readonly foodRepository: FoodRepository,
|
||||
private readonly reviewRepository: ReviewRepository,
|
||||
) {}
|
||||
) { }
|
||||
|
||||
/**
|
||||
* Cron job that runs at midnight every day (00:00:00 UTC)
|
||||
@@ -55,8 +55,8 @@ export class FoodRatingCronService {
|
||||
averageRating = sum / reviews.length;
|
||||
}
|
||||
|
||||
// Update food rate
|
||||
food.rate = parseFloat(averageRating.toFixed(2));
|
||||
// Update food score
|
||||
food.score = parseFloat(averageRating.toFixed(2));
|
||||
this.em.persist(food);
|
||||
|
||||
updatedCount++;
|
||||
|
||||
@@ -21,7 +21,7 @@ export class ReviewService {
|
||||
private readonly foodRepository: FoodRepository,
|
||||
private readonly userRepository: UserRepository,
|
||||
private readonly em: EntityManager,
|
||||
) {}
|
||||
) { }
|
||||
|
||||
async create(userId: string, createReviewDto: CreateReviewDto): Promise<Review> {
|
||||
const { foodId, orderId, rating, comment, positivePoints, negativePoints } = createReviewDto;
|
||||
@@ -176,7 +176,7 @@ export class ReviewService {
|
||||
if (reviews.length === 0) {
|
||||
const food = await this.foodRepository.findOne({ id: foodId });
|
||||
if (food) {
|
||||
food.rate = 0;
|
||||
food.score = 0;
|
||||
await this.em.persistAndFlush(food);
|
||||
}
|
||||
return;
|
||||
@@ -186,7 +186,7 @@ export class ReviewService {
|
||||
|
||||
const food = await this.foodRepository.findOne({ id: foodId });
|
||||
if (food) {
|
||||
food.rate = parseFloat(averageRating.toFixed(2));
|
||||
food.score = parseFloat(averageRating.toFixed(2));
|
||||
await this.em.persistAndFlush(food);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { MealType } from '../../modules/foods/interface/food.interface';
|
||||
|
||||
export interface FoodData {
|
||||
title: string;
|
||||
desc: string;
|
||||
@@ -9,6 +11,13 @@ export interface FoodData {
|
||||
stockDefault: number;
|
||||
images: string[];
|
||||
content?: string[];
|
||||
weekDays?: number[];
|
||||
mealTypes?: MealType[];
|
||||
discount?: number;
|
||||
score?: number;
|
||||
inPlaceServe?: boolean;
|
||||
pickupServe?: boolean;
|
||||
isSpecialOffer?: boolean;
|
||||
}
|
||||
|
||||
export const foodsData: FoodData[] = [
|
||||
|
||||
@@ -30,18 +30,15 @@ export class FoodsSeeder {
|
||||
restaurant,
|
||||
category,
|
||||
isActive: foodData.isActive,
|
||||
sat: false,
|
||||
sun: false,
|
||||
mon: false,
|
||||
breakfast: false,
|
||||
noon: false,
|
||||
dinner: false,
|
||||
discount: 0,
|
||||
rate: 0,
|
||||
inPlaceServe: false,
|
||||
pickupServe: false,
|
||||
// new fields on Food entity
|
||||
weekDays: foodData.weekDays ?? [0, 1, 2, 3, 4, 5, 6],
|
||||
mealTypes: foodData.mealTypes ?? [],
|
||||
discount: foodData.discount ?? 0,
|
||||
score: foodData.score ?? 0,
|
||||
inPlaceServe: foodData.inPlaceServe ?? false,
|
||||
pickupServe: foodData.pickupServe ?? false,
|
||||
images: foodData.images,
|
||||
isSpecialOffer: false,
|
||||
isSpecialOffer: foodData.isSpecialOffer ?? false,
|
||||
});
|
||||
|
||||
em.persist(food);
|
||||
|
||||
Reference in New Issue
Block a user