import { GetAccountAccountInfoResponse, GetAccountAddress, ContactResponse, Contact, } from './types/Address'; import { InvoiceResponse, Invoice, GetInvoicesResponse, GetInvoices, } from './types/Invoice'; import { Payment, PaymentResponse } from './types/Payment'; import { BillingAccountInfoResponse, BillingAddress, } from './types/PaymentMethod'; export const mapBillingAccountInfoResponse = ({ billing_city: billingCity, billing_country: billingCountry, billing_state: billingState, billing_street: billingStreet, billing_postal_code: billingPostalCode, }: Pick< BillingAccountInfoResponse, | 'billing_city' | 'billing_country' | 'billing_state' | 'billing_street' | 'billing_postal_code' >): BillingAddress => ({ billingCity, billingCountry, billingState, billingStreet, billingPostalCode, }); const mapPaymentsResponse = ({ is_active: isActive, last_payment_status: lastPaymentStatus, payment_date: paymentDate, payment_method_id: paymentMethodId, ...otherValues }: PaymentResponse): Payment => ({ isActive, lastPaymentStatus, paymentDate, paymentMethodId, ...otherValues, }); const mapInvoiceResponse = ({ bill_to_info, content_document_id: contentDocumentId, currency_iso_code: currencyISOCode, due_date: dueDate, issue_date: issueDate, invoice_status: invoiceStatus, payments, payment_status: paymentStatus, tax_amount: taxAmount, sub_total: subTotal, ...otherValues }: InvoiceResponse): Invoice => ({ billToInfo: mapBillingAccountInfoResponse(bill_to_info), currencyISOCode, contentDocumentId, dueDate, issueDate, invoiceStatus, payments: payments?.map((payment) => mapPaymentsResponse(payment)), paymentStatus, subTotal, taxAmount, ...otherValues, }); const mapContactResponse = ({ first_name: firstName, last_name: lastName, postal_code: postalCode, street_address: streetAddress, ...otherValues }: ContactResponse): Contact => ({ firstName, lastName, postalCode, streetAddress, ...otherValues, }); export const mapGetInvoicesResponse = ({ response: { first_name: firstName, last_name: lastName, invoices, payment_status: paymentStatus, }, ...otherValues }: GetInvoicesResponse): GetInvoices => ({ response: { firstName, lastName, invoices: invoices ? invoices?.map((invoice) => mapInvoiceResponse(invoice)) : [], paymentStatus, }, ...otherValues, }); export const mapGetAccountAddress = ({ response: { account_csm_email: accountCsmEmail, account_manager_email: accountManagerEmail, bill_to_contact, billing_city: billingCity, billing_country: billingCountry, billing_state: billingState, billing_street: billingStreet, billing_postal_code: billingPostalCode, dashboard_email: dashboardEmail, is_email_delivery_on: isEmailDeliveryOn, primary_contact, shipping_city: shippingCity, shipping_country: shippingCountry, shipping_postal_code: shippingPostalCode, shipping_street: shippingStreet, shipping_state: shippingState, ...otherAddressValues }, ...otherValues }: GetAccountAccountInfoResponse): GetAccountAddress => ({ response: { accountCsmEmail, accountManagerEmail, billToContact: mapContactResponse(bill_to_contact), billingCity, billingCountry, billingState, billingStreet, billingPostalCode, dashboardEmail, isEmailDeliveryOn, primaryContact: mapContactResponse(primary_contact), shippingCity, shippingCountry, shippingStreet, shippingState, shippingPostalCode, ...otherAddressValues, }, ...otherValues, }); export const appendQueryParams = ( url: string, params?: Record, ) => { // Remove undefined values. const paramsFiltered: Record = {}; for (const key in params) { if (params[key] !== undefined) { paramsFiltered[key] = params[key]; } } const queryParams = new URLSearchParams(paramsFiltered).toString(); if (queryParams) url = `${url}?${queryParams}`; return url; };