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, @InjectRepository(FeedingPlanProduct) private readonly feedingPlanProductRepository: Repository, private readonly petService: PetService, private readonly productService: ProductService, ) {} async create( createFeedingPlanRequest: CreateFeedingPlanRequest, ): Promise { 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, percentage: product.percentage, }); feedingPlanProducts.push( await this.feedingPlanProductRepository.save(feedingPlanProduct), ); }), ); feedingPlan.feedingPlanProducts = feedingPlanProducts; return this.feedingPlanRepository.save(feedingPlan); } async update( id: string, updateFeedingPlanRequest: CreateFeedingPlanRequest, ): Promise { const feedingPlan = await this.get(id); feedingPlan.feedingGoal = updateFeedingPlanRequest.feedingGoal; feedingPlan.targetWeight = updateFeedingPlanRequest.targetWeight; const feedingPlanProducts: FeedingPlanProduct[] = []; await Promise.all( updateFeedingPlanRequest.products.map(async (product) => { const existProduct = await this.productService.get(product.id); const exitFeedingPlanProduct = await this.feedingPlanProductRepository.findOneBy({ product: existProduct, }); if (exitFeedingPlanProduct) { exitFeedingPlanProduct.dailyUsageWeight = product.dailyUsageWeight; exitFeedingPlanProduct.percentage = product.percentage; feedingPlanProducts.push( await this.feedingPlanProductRepository.save( exitFeedingPlanProduct, ), ); } else { const feedingPlanProduct = this.feedingPlanProductRepository.create({ product: existProduct, dailyUsageWeight: product.dailyUsageWeight, percentage: product.percentage, }); feedingPlanProducts.push( await this.feedingPlanProductRepository.save(feedingPlanProduct), ); } }), ); return this.feedingPlanRepository.save(feedingPlan); } async get(id: string): Promise { const feedingPlan = await this.feedingPlanRepository.findOneBy({ id }); if (!feedingPlan) { throw new Error('Feeding plan not found'); } return feedingPlan; } }