import ApiClient from './ApiClient'; import { appendQueryParams, mapGetInvoicesResponse, } from './accountManagementUtils'; import { ChargeInvoiceRequest, ChargeInvoiceResponse, DownloadInvoiceRequest, DownloadInvoiceResponse, GetInvoices, GetInvoicesQueryParams, GetInvoicesRequest, GetInvoicesResponse, GetOutstandingBalance, GetOutstandingBalanceRequest, GetOutstandingBalanceResponse, ToggleInvoices, ToggleInvoicesRequest, ToggleInvoicesResponse, } from './types/Invoice'; import urls from './urls'; export const mapGetOutstandingBalanceResponse = ({ response: { has_outstanding_invoices: hasOutstandingInvoices, bill_to_contact_email: billToContactEmail, }, ...otherValues }: GetOutstandingBalanceResponse): GetOutstandingBalance => ({ response: { hasOutstandingInvoices, billToContactEmail, }, ...otherValues, }); export default class InvoiceClient { async getInvoices( this: ApiClient, params?: GetInvoicesQueryParams, ): Promise { let url = urls.getInvoices(); if (params) url = appendQueryParams(url, params); const response = await this.requestProtected< GetInvoicesRequest, GetInvoicesResponse >({ method: 'GET', url, }); return mapGetInvoicesResponse(response); } downloadInvoice( this: ApiClient, data: DownloadInvoiceRequest, ): Promise { return this.requestProtected< DownloadInvoiceRequest, DownloadInvoiceResponse >({ method: 'GET', url: urls.downloadInvoice(data.content_document_id), }); } async toggleInvoiceEmail( this: ApiClient, params: ToggleInvoicesRequest, ): Promise { const data = await this.requestProtected< ToggleInvoicesRequest, ToggleInvoicesResponse >({ body: params, method: 'POST', url: urls.toggleInvoiceEmail(), }); const { response: { is_email_delivery_on: isEmailDeliveryOn }, ...otherValues } = data; return { response: { isEmailDeliveryOn, }, ...otherValues, }; } async getOutstandingBalance(this: ApiClient): Promise { const response = await this.requestProtected< GetOutstandingBalanceRequest, GetOutstandingBalanceResponse >({ method: 'GET', url: urls.outstandingBalance(), }); return mapGetOutstandingBalanceResponse(response); } chargeInvoice(this: ApiClient, data: ChargeInvoiceRequest) { return this.requestProtected({ body: data, method: 'POST', url: urls.chargeInvoice(), }); } }