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 { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||||
import { Type } from 'class-transformer';
|
import { Type } from 'class-transformer';
|
||||||
|
import { MealType } from '../interface/food.interface';
|
||||||
|
|
||||||
export class CreateFoodDto {
|
export class CreateFoodDto {
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@@ -54,6 +55,19 @@ export class CreateFoodDto {
|
|||||||
@ApiPropertyOptional({ type: [String] })
|
@ApiPropertyOptional({ type: [String] })
|
||||||
content?: 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()
|
@IsOptional()
|
||||||
@IsNumber()
|
@IsNumber()
|
||||||
@Type(() => Number)
|
@Type(() => Number)
|
||||||
@@ -138,6 +152,12 @@ export class CreateFoodDto {
|
|||||||
@ApiPropertyOptional({ example: 0 })
|
@ApiPropertyOptional({ example: 0 })
|
||||||
discount?: number;
|
discount?: number;
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@IsNumber()
|
||||||
|
@Type(() => Number)
|
||||||
|
@ApiPropertyOptional({ example: 0 })
|
||||||
|
score?: number;
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsBoolean()
|
@IsBoolean()
|
||||||
@ApiPropertyOptional({ example: false })
|
@ApiPropertyOptional({ example: false })
|
||||||
|
|||||||
@@ -36,13 +36,6 @@ export class FoodService {
|
|||||||
|
|
||||||
// prepare data with defaults for required fields so repository.create typing is satisfied
|
// prepare data with defaults for required fields so repository.create typing is satisfied
|
||||||
const data: RequiredEntityData<Food> = {
|
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,
|
desc: rest.desc,
|
||||||
isActive: rest.isActive ?? true,
|
isActive: rest.isActive ?? true,
|
||||||
inPlaceServe: rest.inPlaceServe ?? false,
|
inPlaceServe: rest.inPlaceServe ?? false,
|
||||||
@@ -56,6 +49,10 @@ export class FoodService {
|
|||||||
price: rest.price ?? 0,
|
price: rest.price ?? 0,
|
||||||
prepareTime: rest.prepareTime ?? 0,
|
prepareTime: rest.prepareTime ?? 0,
|
||||||
images: rest.images ?? undefined,
|
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,
|
restaurant: restaurant,
|
||||||
category: category,
|
category: category,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -55,8 +55,8 @@ export class FoodRatingCronService {
|
|||||||
averageRating = sum / reviews.length;
|
averageRating = sum / reviews.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update food rate
|
// Update food score
|
||||||
food.rate = parseFloat(averageRating.toFixed(2));
|
food.score = parseFloat(averageRating.toFixed(2));
|
||||||
this.em.persist(food);
|
this.em.persist(food);
|
||||||
|
|
||||||
updatedCount++;
|
updatedCount++;
|
||||||
|
|||||||
@@ -176,7 +176,7 @@ export class ReviewService {
|
|||||||
if (reviews.length === 0) {
|
if (reviews.length === 0) {
|
||||||
const food = await this.foodRepository.findOne({ id: foodId });
|
const food = await this.foodRepository.findOne({ id: foodId });
|
||||||
if (food) {
|
if (food) {
|
||||||
food.rate = 0;
|
food.score = 0;
|
||||||
await this.em.persistAndFlush(food);
|
await this.em.persistAndFlush(food);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@@ -186,7 +186,7 @@ export class ReviewService {
|
|||||||
|
|
||||||
const food = await this.foodRepository.findOne({ id: foodId });
|
const food = await this.foodRepository.findOne({ id: foodId });
|
||||||
if (food) {
|
if (food) {
|
||||||
food.rate = parseFloat(averageRating.toFixed(2));
|
food.score = parseFloat(averageRating.toFixed(2));
|
||||||
await this.em.persistAndFlush(food);
|
await this.em.persistAndFlush(food);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { MealType } from '../../modules/foods/interface/food.interface';
|
||||||
|
|
||||||
export interface FoodData {
|
export interface FoodData {
|
||||||
title: string;
|
title: string;
|
||||||
desc: string;
|
desc: string;
|
||||||
@@ -9,6 +11,13 @@ export interface FoodData {
|
|||||||
stockDefault: number;
|
stockDefault: number;
|
||||||
images: string[];
|
images: string[];
|
||||||
content?: string[];
|
content?: string[];
|
||||||
|
weekDays?: number[];
|
||||||
|
mealTypes?: MealType[];
|
||||||
|
discount?: number;
|
||||||
|
score?: number;
|
||||||
|
inPlaceServe?: boolean;
|
||||||
|
pickupServe?: boolean;
|
||||||
|
isSpecialOffer?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const foodsData: FoodData[] = [
|
export const foodsData: FoodData[] = [
|
||||||
|
|||||||
@@ -30,18 +30,15 @@ export class FoodsSeeder {
|
|||||||
restaurant,
|
restaurant,
|
||||||
category,
|
category,
|
||||||
isActive: foodData.isActive,
|
isActive: foodData.isActive,
|
||||||
sat: false,
|
// new fields on Food entity
|
||||||
sun: false,
|
weekDays: foodData.weekDays ?? [0, 1, 2, 3, 4, 5, 6],
|
||||||
mon: false,
|
mealTypes: foodData.mealTypes ?? [],
|
||||||
breakfast: false,
|
discount: foodData.discount ?? 0,
|
||||||
noon: false,
|
score: foodData.score ?? 0,
|
||||||
dinner: false,
|
inPlaceServe: foodData.inPlaceServe ?? false,
|
||||||
discount: 0,
|
pickupServe: foodData.pickupServe ?? false,
|
||||||
rate: 0,
|
|
||||||
inPlaceServe: false,
|
|
||||||
pickupServe: false,
|
|
||||||
images: foodData.images,
|
images: foodData.images,
|
||||||
isSpecialOffer: false,
|
isSpecialOffer: foodData.isSpecialOffer ?? false,
|
||||||
});
|
});
|
||||||
|
|
||||||
em.persist(food);
|
em.persist(food);
|
||||||
|
|||||||
Reference in New Issue
Block a user