|
@@ -0,0 +1,48 @@
|
|
|
|
|
+import { Injectable } from '@nestjs/common';
|
|
|
|
|
+import { CreateFeedingPlanRequest } from '@/pet-feeder/dto/create-feeding-plan.request';
|
|
|
|
|
+import { FeedingPlan } from '@/pet-feeder/entity/feeding-plan.entity';
|
|
|
|
|
+import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
|
+import { Repository } from 'typeorm';
|
|
|
|
|
+import { FeedingPlanProduct } from '@/pet-feeder/entity/feeding-plan-product.entity';
|
|
|
|
|
+import { PetService } from '@/pet-feeder/service/pet.service';
|
|
|
|
|
+import { ProductService } from '@/pet-feeder/service/product.service';
|
|
|
|
|
+
|
|
|
|
|
+@Injectable()
|
|
|
|
|
+export class FeedingPlanService {
|
|
|
|
|
+ constructor(
|
|
|
|
|
+ @InjectRepository(FeedingPlan)
|
|
|
|
|
+ private readonly feedingPlanRepository: Repository<FeedingPlan>,
|
|
|
|
|
+
|
|
|
|
|
+ @InjectRepository(FeedingPlanProduct)
|
|
|
|
|
+ private readonly feedingPlanProductRepository: Repository<FeedingPlanProduct>,
|
|
|
|
|
+
|
|
|
|
|
+ private readonly petService: PetService,
|
|
|
|
|
+
|
|
|
|
|
+ private readonly productService: ProductService,
|
|
|
|
|
+ ) {}
|
|
|
|
|
+
|
|
|
|
|
+ async create(
|
|
|
|
|
+ createFeedingPlanRequest: CreateFeedingPlanRequest,
|
|
|
|
|
+ ): Promise<FeedingPlan> {
|
|
|
|
|
+ const feedingPlan = this.feedingPlanRepository.create({
|
|
|
|
|
+ feedingGoal: createFeedingPlanRequest.feedingGoal,
|
|
|
|
|
+ targetWeight: createFeedingPlanRequest.targetWeight,
|
|
|
|
|
+ });
|
|
|
|
|
+ feedingPlan.pet = await this.petService.get(createFeedingPlanRequest.petId);
|
|
|
|
|
+
|
|
|
|
|
+ const feedingPlanProducts: FeedingPlanProduct[] = [];
|
|
|
|
|
+
|
|
|
|
|
+ await Promise.all(
|
|
|
|
|
+ createFeedingPlanRequest.products.map(async (product) => {
|
|
|
|
|
+ const existProduct = await this.productService.get(product.id);
|
|
|
|
|
+ const feedingPlanProduct = this.feedingPlanProductRepository.create({
|
|
|
|
|
+ product: existProduct,
|
|
|
|
|
+ dailyUsageWeight: product.dailyUsageWeight,
|
|
|
|
|
+ });
|
|
|
|
|
+ feedingPlanProducts.push(feedingPlanProduct);
|
|
|
|
|
+ }),
|
|
|
|
|
+ );
|
|
|
|
|
+ feedingPlan.feedingPlanProducts = feedingPlanProducts;
|
|
|
|
|
+ return this.feedingPlanRepository.save(feedingPlan);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|