|
| 1 | +import { Configuration } from './Configuration'; |
| 2 | +import { Gateway } from './Gateway'; |
| 3 | +import { Invoice, InvoiceInput } from './Invoice'; |
| 4 | +import { ApiResponse } from './types'; |
| 5 | +import { buildURL } from './util'; |
| 6 | + |
| 7 | +export class InvoiceGateway { |
| 8 | + gateway: Gateway; |
| 9 | + config: Configuration; |
| 10 | + |
| 11 | + constructor(gateway: Gateway) { |
| 12 | + this.gateway = gateway; |
| 13 | + this.config = this.gateway.config; |
| 14 | + } |
| 15 | + |
| 16 | + async find(invoiceId: string) { |
| 17 | + const endPoint = buildURL('invoices/get'); |
| 18 | + |
| 19 | + const result = await this.gateway.client.post<ApiResponse<Invoice>>( |
| 20 | + endPoint, |
| 21 | + { |
| 22 | + invoiceId: invoiceId, |
| 23 | + }, |
| 24 | + ); |
| 25 | + |
| 26 | + return Object.assign(new Invoice(), result.invoice); |
| 27 | + } |
| 28 | + |
| 29 | + async create(body: InvoiceInput) { |
| 30 | + const endPoint = buildURL('invoices/create'); |
| 31 | + |
| 32 | + const result = await this.gateway.client.post<ApiResponse<Invoice>>( |
| 33 | + endPoint, |
| 34 | + body, |
| 35 | + ); |
| 36 | + |
| 37 | + return Object.assign(new Invoice(), result.invoice); |
| 38 | + } |
| 39 | + |
| 40 | + async update(invoiceId: string, body: InvoiceInput) { |
| 41 | + const endPoint = buildURL('invoices/update'); |
| 42 | + |
| 43 | + const result = await this.gateway.client.post<ApiResponse<Invoice>>( |
| 44 | + endPoint, |
| 45 | + { |
| 46 | + invoiceId: invoiceId, |
| 47 | + ...body, |
| 48 | + }, |
| 49 | + ); |
| 50 | + |
| 51 | + return Object.assign(new Invoice(), result.invoice); |
| 52 | + } |
| 53 | + |
| 54 | + async remove(invoiceIds: string[]) { |
| 55 | + const endPoint = buildURL('invoices/delete'); |
| 56 | + |
| 57 | + const result = await this.gateway.client.post<ApiResponse<Invoice>>( |
| 58 | + endPoint, |
| 59 | + { |
| 60 | + invoiceIds: invoiceIds, |
| 61 | + }, |
| 62 | + ); |
| 63 | + |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + async search(body: any) { |
| 68 | + const endPoint = buildURL('invoices/search'); |
| 69 | + |
| 70 | + const result = await this.gateway.client.post<ApiResponse<Invoice[]>>( |
| 71 | + endPoint, |
| 72 | + body, |
| 73 | + ); |
| 74 | + |
| 75 | + return result.invoices.map((line: Invoice) => Object.assign(new Invoice(), line)); |
| 76 | + } |
| 77 | +} |
0 commit comments