import { Injectable } from '@nestjs/common' import { plainToClass } from 'class-transformer' import { ChallengePatient } from '../../models' import { HttpService } from '../../services/http' import { BaseRepository } from '../base.repository' const CREATE_CHALLENGE_PATIENT = ` mutation createChallengePatient($challengePatient: challenge_patient_insert_input = {}) { insert_challenge_patient_one(object: $challengePatient) { id } } ` const GET_OPEN_CHALLENGE = ` query getOpenChallenge($patientId: String = "") { challenge_patient(where: {patient_id: {_eq: $patientId}, finished_at: {_is_null: true}}) { id } } ` @Injectable() export class ChallengePatientRepository extends BaseRepository { constructor(protected readonly httpService: HttpService) { super(httpService, 'challenge_patient') } async createChallengePatient(challengePatient: ChallengePatient): Promise { const { data } = await this.fetchGraphQL({ body: { query: CREATE_CHALLENGE_PATIENT, variables: { challengePatient } }, }) return data.insert_challenge_patient_one ? data.insert_challenge_patient_one.id : null } async getOpenChallenge(patientId: string): Promise { const { data } = await this.fetchGraphQL({ body: { query: GET_OPEN_CHALLENGE, variables: { patientId } }, }) return data.challenge_patient ? plainToClass(ChallengePatient, data.challenge_patient[0]) : null } }