import { Injectable } from '@nestjs/common' import { FoodPlan, MealGroup } from '../models' import { HttpService } from '../services/http' import { BaseRepository } from './base.repository' const MEAL_ITEM_COLUMNS = ` id amount carbohydrates description gram_amount kcal lipids measure proteins recipe_url meal_id food_id meal_template_id homemade_measure_id substitute_meal_item_id ingredients_meal_item_id ` const GET_BY_APPOINTMENT_ID = ` query findFoodPlan($appointment_id: uuid) { food_plan(where: {appointment_id: {_eq: $appointment_id}}, limit: 1) { id published_by_id } } ` const GET_WITH_RELATIONSHIPS = ` query food_plan($id: uuid!) { food_plan_by_pk(id: $id) { id goal_id meal_groups { orientations meal_type_id food_record_id id food_plan_id time meals { items { ${MEAL_ITEM_COLUMNS} } } type { id title_pt } } } } ` @Injectable() export class FoodPlanRepository extends BaseRepository { constructor(protected readonly httpService: HttpService) { super(httpService, 'food_plan') } async getByAppointmentId(appointmentId: string): Promise { const { data } = await this.fetchGraphQL({ body: { query: GET_BY_APPOINTMENT_ID, variables: { appointment_id: appointmentId } }, }) return data.food_plan ? FoodPlan.toInstance(data.food_plan) : null } async getWithRelationships(id: string): Promise { const { data } = await this.fetchGraphQL({ body: { query: GET_WITH_RELATIONSHIPS, variables: { id } }, }) return data.food_plan_by_pk ? FoodPlan.toInstance({ ...data.food_plan_by_pk, meal_groups: (data.food_plan_by_pk.meal_groups || []).map(meal_group => MealGroup.toInstance(meal_group)), }) : null } }