import { Injectable } from '@nestjs/common' import { plainToClass } from 'class-transformer' import { ChallengeWeek, ChallengeShoppingList, ChallengeDay, ChallengeDayPatient } from '../../models' import { HttpService } from '../../services/http' import { BaseRepository } from '../base.repository' const GET_CHALLENGE_WITH_RELATIONSHIPS = ` query getChallengeWithRelationships($countryCode: String!, $patientId: String!) { challenge_week(where: {country_code: {_eq: $countryCode}}) { id bonus_description bonus_ebook challenge_shopping_lists { food { id name_pt name_es friendly_name_pt friendly_name_es code } } challenge_days { id day title description goal tip icon challenge_day_patients(where: {challenge_patient: {patient_id: {_eq: $patientId}}}) { id available_at finished_at is_pending } } } } ` const GET_CHALLENGE_WEEK_DAYS = ` query getChallengeWeekDays($countryCode: String!) { challenge_week(where: {country_code: {_eq: $countryCode}}) { id challenge_days { id day title description goal tip icon challenge_week_id } } } ` @Injectable() export class ChallengeWeekRepository extends BaseRepository { constructor(protected readonly httpService: HttpService) { super(httpService, 'challenge_week') } async getChallengeWithRelationships(patientId: string, countryCode: string): Promise { const { data } = await this.fetchGraphQL({ body: { query: GET_CHALLENGE_WITH_RELATIONSHIPS, variables: { patientId, countryCode } }, }) return (data.challenge_week || []).map(challenge_week => plainToClass(ChallengeWeek, { ...challenge_week, challenge_shopping_lists: (challenge_week.challenge_shopping_lists || []).map(challenge_shopping_lists => plainToClass(ChallengeShoppingList, challenge_shopping_lists), ), challenge_days: (challenge_week.challenge_days || []).map(challenge_days => plainToClass(ChallengeDay, { ...challenge_days, challenge_day_patients: (challenge_days.challenge_day_patients || []).map(challenge_day_patients => plainToClass(ChallengeDayPatient, challenge_day_patients), ), }), ), }), ) } async getChallengeWeekDays(countryCode: string): Promise { const { data } = await this.fetchGraphQL({ body: { query: GET_CHALLENGE_WEEK_DAYS, variables: { countryCode } }, }) return (data.challenge_week || []).map(challenge_week => plainToClass(ChallengeWeek, { ...challenge_week, challenge_days: (challenge_week.challenge_days || []).map(challenge_days => plainToClass(ChallengeDay, challenge_days), ), }), ) } }