import { Injectable } from '@nestjs/common' import { isEmpty } from 'lodash' import { DiscountCoupon } from '../models' import { HttpService } from '../services/http' import { BaseRepository } from './base.repository' const FIND_ATTRIBUTES = ` id name code discount_type discount_amount begins_at ends_at created_at updated_at created_by_id updated_by_id partner_id enabled country_codes ` const GET_BY_CODE = ` query discount_coupon($code: String!) { discount_coupon(where: {code: {_ilike: $code}}, limit: 1) { ${FIND_ATTRIBUTES} plan_discount_coupons { plan_id } } } ` @Injectable() export class DiscountCouponRepository extends BaseRepository { constructor(protected readonly httpService: HttpService) { super(httpService, 'plan') } async getByCode(code: string): Promise { const { data } = await this.fetchGraphQL({ body: { query: GET_BY_CODE, variables: { code } }, }) return isEmpty(data) || isEmpty(data.discount_coupon) ? null : DiscountCoupon.toInstance(data.discount_coupon.shift()) } }