|
| 1 | +import { InvoiceFactory } from "./factories/InvoiceFactory"; |
| 2 | +import { startNockRec, testingApiClient } from "./helpers/integrationTestsHelpers"; |
| 3 | +import * as assert from "assert"; |
| 4 | +import { RecipientFactory } from "./factories/RecipientFactory"; |
| 5 | +import { InvoicePaymentFactory } from "./factories/InvoicePaymentFactory"; |
| 6 | +import { InvoiceLineFactory } from "./factories/InvoiceLineFactory"; |
| 7 | +import { InvoicePayment, InvoicePaymentInput, InvoicePaymentRecord } from "../../lib/InvoicePayment"; |
| 8 | + |
| 9 | +let recipientFactory: RecipientFactory; |
| 10 | +let invoiceFactory: InvoiceFactory; |
| 11 | +let invoiceLineFactory: InvoiceLineFactory; |
| 12 | +let invoicePaymentFactory: InvoicePaymentFactory; |
| 13 | + |
| 14 | +before(async () => { |
| 15 | + recipientFactory = new RecipientFactory(); |
| 16 | + invoiceFactory = new InvoiceFactory(); |
| 17 | + invoiceLineFactory = new InvoiceLineFactory(); |
| 18 | + invoicePaymentFactory = new InvoicePaymentFactory(); |
| 19 | +}); |
| 20 | + |
| 21 | +describe("Invoice Payment", () => { |
| 22 | + it("creates an invoice payment", async () => { |
| 23 | + const nockDone = await startNockRec("invoice-payment-create.json"); |
| 24 | + |
| 25 | + const recipient = await recipientFactory.createResource(); |
| 26 | + const invoice = await invoiceFactory.createResource({ recipientId: recipient.id }); |
| 27 | + await invoiceLineFactory.createResource({ invoice: { id: invoice.id } }); |
| 28 | + const invoicePayment = await invoicePaymentFactory.createResource( |
| 29 | + { |
| 30 | + ids: [ |
| 31 | + { |
| 32 | + invoiceId: invoice.id, |
| 33 | + amount: { |
| 34 | + currency: "USD", |
| 35 | + value: "100", |
| 36 | + }, |
| 37 | + }, |
| 38 | + ], |
| 39 | + }, |
| 40 | + ); |
| 41 | + |
| 42 | + nockDone(); |
| 43 | + |
| 44 | + assert.ok(invoicePayment instanceof InvoicePayment); |
| 45 | + assert.strictEqual(invoicePayment.invoicePayments[0].amount.value, "100.00"); |
| 46 | + assert.strictEqual(invoicePayment.invoicePayments[0].amount.currency, "USD"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("updates an invoice payment", async () => { |
| 50 | + const nockDone = await startNockRec("invoice-payment-update.json"); |
| 51 | + |
| 52 | + const recipient = await recipientFactory.createResource(); |
| 53 | + const invoice = await invoiceFactory.createResource({ recipientId: recipient.id }); |
| 54 | + const invoiceLines = await invoiceLineFactory.createResource({ invoice: { id: invoice.id } }); |
| 55 | + const invoicePayment = await invoicePaymentFactory.createResource( |
| 56 | + { |
| 57 | + ids: [{ invoiceId: invoice.id }], |
| 58 | + }, |
| 59 | + ); |
| 60 | + |
| 61 | + const updatedInvoicePayment = await testingApiClient.invoicePayment.update( |
| 62 | + { paymentId: invoicePayment.paymentId, |
| 63 | + invoiceLineId: invoicePayment.invoicePayments[0].invoiceLineId, |
| 64 | + amount: { |
| 65 | + currency: "USD", |
| 66 | + value: "200", |
| 67 | + }, |
| 68 | + }, |
| 69 | + ); |
| 70 | + |
| 71 | + nockDone(); |
| 72 | + |
| 73 | + assert.ok(updatedInvoicePayment); |
| 74 | + }); |
| 75 | + |
| 76 | + it("deletes an invoice payment", async () => { |
| 77 | + const nockDone = await startNockRec("invoice-payment-delete.json"); |
| 78 | + |
| 79 | + const recipient = await recipientFactory.createResource(); |
| 80 | + const invoice = await invoiceFactory.createResource({ recipientId: recipient.id }); |
| 81 | + await invoiceLineFactory.createResource({ invoice: { id: invoice.id } }); |
| 82 | + const invoicePayment = await invoicePaymentFactory.createResource( |
| 83 | + { |
| 84 | + ids: [{ invoiceId: invoice.id }], |
| 85 | + }, |
| 86 | + ); |
| 87 | + |
| 88 | + const deletedInvoicePayment = await testingApiClient.invoicePayment.delete( |
| 89 | + { paymentId: invoicePayment.paymentId, |
| 90 | + invoiceLineIds: invoicePayment.invoicePayments.map((ip: InvoicePaymentRecord) => ip.invoiceLineId), |
| 91 | + }, |
| 92 | + ); |
| 93 | + |
| 94 | + nockDone(); |
| 95 | + |
| 96 | + assert.ok(deletedInvoicePayment); |
| 97 | + }); |
| 98 | + |
| 99 | + it("searches for invoice payments by invoiceIds", async () => { |
| 100 | + const nockDone = await startNockRec("invoice-payment-search.json"); |
| 101 | + |
| 102 | + const recipient = await recipientFactory.createResource(); |
| 103 | + const invoice = await invoiceFactory.createResource({ recipientId: recipient.id }); |
| 104 | + await invoiceLineFactory.createResource({ invoice: { id: invoice.id } }); |
| 105 | + const invoicePayment = await invoicePaymentFactory.createResource( |
| 106 | + { |
| 107 | + ids: [{ invoiceId: invoice.id }], |
| 108 | + }, |
| 109 | + ); |
| 110 | + |
| 111 | + const searchResults = await testingApiClient.invoicePayment.search( |
| 112 | + { invoiceIds: [invoice.id] }, |
| 113 | + ); |
| 114 | + |
| 115 | + nockDone(); |
| 116 | + |
| 117 | + assert.ok(searchResults); |
| 118 | + assert.strictEqual(searchResults.length, 1); |
| 119 | + assert.strictEqual(searchResults[0].amount.value, "100.00"); |
| 120 | + }); |
| 121 | + |
| 122 | + it("searches for invoice payments by paymentIds", async () => { |
| 123 | + const nockDone = await startNockRec("invoice-payment-search-by-payment-ids.json"); |
| 124 | + |
| 125 | + const recipient = await recipientFactory.createResource(); |
| 126 | + const invoice = await invoiceFactory.createResource({ recipientId: recipient.id }); |
| 127 | + await invoiceLineFactory.createResource({ invoice: { id: invoice.id } }); |
| 128 | + const invoicePayment = await invoicePaymentFactory.createResource( |
| 129 | + { |
| 130 | + ids: [{ invoiceId: invoice.id }], |
| 131 | + }, |
| 132 | + ); |
| 133 | + |
| 134 | + const searchResults = await testingApiClient.invoicePayment.search( |
| 135 | + { paymentIds: [invoicePayment.paymentId] }, |
| 136 | + ); |
| 137 | + |
| 138 | + nockDone(); |
| 139 | + |
| 140 | + assert.ok(searchResults); |
| 141 | + assert.strictEqual(searchResults.length, 1); |
| 142 | + assert.strictEqual(searchResults[0].amount.value, "100.00"); |
| 143 | + }); |
| 144 | +}); |
0 commit comments