|
|
@@ -38,11 +38,61 @@ export class FeedingPlanService {
|
|
|
const feedingPlanProduct = this.feedingPlanProductRepository.create({
|
|
|
product: existProduct,
|
|
|
dailyUsageWeight: product.dailyUsageWeight,
|
|
|
+ percentage: product.percentage,
|
|
|
});
|
|
|
- feedingPlanProducts.push(feedingPlanProduct);
|
|
|
+ feedingPlanProducts.push(
|
|
|
+ await this.feedingPlanProductRepository.save(feedingPlanProduct),
|
|
|
+ );
|
|
|
}),
|
|
|
);
|
|
|
feedingPlan.feedingPlanProducts = feedingPlanProducts;
|
|
|
return this.feedingPlanRepository.save(feedingPlan);
|
|
|
}
|
|
|
+
|
|
|
+ async update(
|
|
|
+ id: string,
|
|
|
+ updateFeedingPlanRequest: CreateFeedingPlanRequest,
|
|
|
+ ): Promise<FeedingPlan> {
|
|
|
+ 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<FeedingPlan> {
|
|
|
+ const feedingPlan = await this.feedingPlanRepository.findOneBy({ id });
|
|
|
+ if (!feedingPlan) {
|
|
|
+ throw new Error('Feeding plan not found');
|
|
|
+ }
|
|
|
+ return feedingPlan;
|
|
|
+ }
|
|
|
}
|