Skip to content

Commit 8e8e99f

Browse files
authored
[DEVREL-95] Support multiple batch deletion
2 parents a6d861d + 9920d2f commit 8e8e99f

15 files changed

+2673
-14
lines changed

lib/Gateway.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { OfflinePaymentGateway } from "./OfflinePaymentGateway";
88
import { InvoiceGateway } from "./InvoiceGateway";
99
import { Client } from "./Client";
1010
import { InvoiceLineGateway } from "./InvoiceLineGateway";
11+
import { InvoicePaymentGateway } from "./InvoicePaymentGateway";
1112

1213
export class Gateway {
1314
config: Configuration;
@@ -20,6 +21,7 @@ export class Gateway {
2021
offlinePayment: OfflinePaymentGateway;
2122
invoice: InvoiceGateway;
2223
invoiceLine: InvoiceLineGateway;
24+
invoicePayment: InvoicePaymentGateway;
2325

2426
/**
2527
* This should be called by the connect() method to setup a client gateway
@@ -37,5 +39,6 @@ export class Gateway {
3739
this.offlinePayment = new OfflinePaymentGateway(this);
3840
this.invoice = new InvoiceGateway(this);
3941
this.invoiceLine = new InvoiceLineGateway(this);
42+
this.invoicePayment = new InvoicePaymentGateway(this);
4043
}
4144
}

lib/Invoice.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ export class Invoice {
2626
tags: string[] = [];
2727
totalAmount: Amount = {
2828
currency: "",
29-
value: 0,
29+
value: "0",
3030
};
3131
paidAmount: Amount = {
3232
currency: "",
33-
value: 0,
33+
value: "0",
3434
};
3535
dueAmount: Amount = {
3636
currency: "",
37-
value: 0,
37+
value: "0",
3838
};
3939
}

lib/InvoiceLine.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,26 @@ export class InvoiceLine {
3232
forceUsTaxActivity: boolean = false;
3333
unitAmount: Amount = {
3434
currency: '',
35-
value: 0,
35+
value: "0",
3636
};
3737
discountAmount: Amount = {
3838
currency: '',
39-
value: 0,
39+
value: "0",
4040
};
4141
taxAmount: Amount = {
4242
currency: '',
43-
value: 0,
43+
value: "0",
4444
};
4545
totalAmount: Amount = {
4646
currency: '',
47-
value: 0,
47+
value: "0",
4848
};
4949
dueAmount: Amount = {
5050
currency: '',
51-
value: 0,
51+
value: "0",
5252
};
5353
paidAmount: Amount = {
5454
currency: '',
55-
value: 0,
55+
value: "0",
5656
};
5757
}

lib/InvoiceLineGateway.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class InvoiceLineGateway {
1717
async create(invoiceId: string, invoiceLines: InvoiceLineInput[]) {
1818
const endPoint = buildURL('invoices/create-lines');
1919

20-
const result = await this.gateway.client.post<ApiResponse<Invoice>>(
20+
const result = await this.gateway.client.post<ApiResponse<InvoiceLine>>(
2121
endPoint,
2222
{
2323
invoiceId: invoiceId,
@@ -31,7 +31,7 @@ export class InvoiceLineGateway {
3131
async update(invoiceId: string, body: any) {
3232
const endPoint = buildURL('invoices/update-lines');
3333

34-
const result = await this.gateway.client.post<ApiResponse<Invoice>>(
34+
const result = await this.gateway.client.post<ApiResponse<InvoiceLine>>(
3535
endPoint,
3636
{
3737
invoiceId: invoiceId,
@@ -45,7 +45,7 @@ export class InvoiceLineGateway {
4545
async delete(invoiceId: string, lineIds: string[]) {
4646
const endPoint = buildURL('invoices/delete-lines');
4747

48-
const result = await this.gateway.client.post<ApiResponse<Invoice>>(
48+
const result = await this.gateway.client.post<ApiResponse<InvoiceLine>>(
4949
endPoint,
5050
{
5151
invoiceId: invoiceId,

lib/InvoicePayment.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Amount } from "./types";
2+
3+
export interface InvoicePaymentInput {
4+
batchId?: string;
5+
ids: InvoicePaymentInputRecord[];
6+
}
7+
8+
interface InvoicePaymentInputRecord {
9+
invoiceId?: string;
10+
invoiceLineId?: string;
11+
paymentId?: string;
12+
amount: Amount;
13+
}
14+
15+
export class InvoicePayment {
16+
batchId: string = '';
17+
paymentId: string = '';
18+
invoicePayments: InvoicePaymentRecord[] = [];
19+
}
20+
21+
export class InvoicePaymentRecord {
22+
invoiceId: string = '';
23+
invoiceLineId: string = '';
24+
paymentId: string = '';
25+
amount: Amount = {
26+
currency: '',
27+
value: '',
28+
};
29+
}

lib/InvoicePaymentGateway.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { Gateway } from "./Gateway";
2+
import { Configuration } from "./Configuration";
3+
import { buildURL } from "./util";
4+
import { ApiResponse } from "./types";
5+
import { InvoicePayment, InvoicePaymentInput, InvoicePaymentRecord } from "./InvoicePayment";
6+
7+
export class InvoicePaymentGateway {
8+
gateway: Gateway;
9+
config: Configuration;
10+
11+
constructor(gateway: Gateway) {
12+
this.gateway = gateway;
13+
this.config = gateway.config;
14+
}
15+
16+
async create(invoicePayment: InvoicePaymentInput) {
17+
const endPoint = buildURL('invoices/payment/create');
18+
19+
const result = await this.gateway.client.post<ApiResponse<InvoicePayment>>(
20+
endPoint,
21+
invoicePayment,
22+
);
23+
24+
return Object.assign(new InvoicePayment(), result.invoicePayment);
25+
}
26+
27+
async update(payload: any) {
28+
const endPoint = buildURL('invoices/payment/update');
29+
30+
const result = await this.gateway.client.post<{ok: boolean}>(
31+
endPoint,
32+
payload,
33+
);
34+
35+
return result.ok;
36+
}
37+
38+
async delete(payload: any) {
39+
const endPoint = buildURL('invoices/payment/delete');
40+
41+
const result = await this.gateway.client.post<{ok: boolean}>(
42+
endPoint,
43+
payload,
44+
);
45+
46+
return result.ok;
47+
}
48+
49+
async search(payload?: any) {
50+
const endPoint = buildURL('invoices/payment/search');
51+
52+
const result = await this.gateway.client.post<ApiResponse<InvoicePayment[]>>(
53+
endPoint,
54+
payload,
55+
);
56+
57+
return result.invoicePayments.map((ip: InvoicePaymentRecord) => Object.assign(new InvoicePaymentRecord(), ip));
58+
}
59+
}

lib/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,6 @@ export interface ApiResponse<T> {
609609
}
610610

611611
export type Amount = {
612-
value: number;
612+
value: string;
613613
currency: string;
614614
};
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
});

test/integration/factories/InvoiceLineFactory.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ApiFactory } from "./ApiFactory";
2-
import { Amount } from "../../../lib/types";
32
import { InvoiceLineCategory } from "../../../lib/InvoiceLine";
43

54
export class InvoiceLineFactory extends ApiFactory {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { ApiFactory } from "./ApiFactory";
2+
3+
export class InvoicePaymentFactory extends ApiFactory {
4+
public async createResource(attrs: any = {}) {
5+
return this.apiClient.invoicePayment.create(attrs);
6+
}
7+
}

0 commit comments

Comments
 (0)