-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoiceocean-api.js
185 lines (156 loc) · 4.76 KB
/
invoiceocean-api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import fetch from "node-fetch";
export class InvoiceOceanApi {
constructor(INVOICEOCEAN_DOMAIN, INVOICEOCEAN_API_TOKEN) {
this.INVOICEOCEAN_DOMAIN = INVOICEOCEAN_DOMAIN;
this.INVOICEOCEAN_API_TOKEN = INVOICEOCEAN_API_TOKEN;
}
async fetchClients() {
const res = await fetch(
`https://${
this.INVOICEOCEAN_DOMAIN
}.invoiceocean.com/clients.json?api_token=${
this.INVOICEOCEAN_API_TOKEN
}&page=1`,
);
return await res.json();
}
async fetchClient(id) {
const res = await fetch(
`https://${
this.INVOICEOCEAN_DOMAIN
}.invoiceocean.com/{id}.json?&api_token=${this.INVOICEOCEAN_API_TOKEN}`,
);
return await res.json();
}
async fetchInvoices() {
const res = await fetch(
`https://${
this.INVOICEOCEAN_DOMAIN
}.invoiceocean.com/invoices.json?api_token=${
this.INVOICEOCEAN_API_TOKEN
}`,
);
return await res.json();
}
async fetchInvoice(id) {
const res = await fetch(
`https://${
this.INVOICEOCEAN_DOMAIN
}.invoiceocean.com/invoices/${id}.json?api_token=${
this.INVOICEOCEAN_API_TOKEN
}`,
);
return await res.json();
}
async downloadInvoiceToStream(id, destinationStream) {
const res = await fetch(
`https://${
this.INVOICEOCEAN_DOMAIN
}.invoiceocean.com/invoices/${id}.pdf?api_token=${
this.INVOICEOCEAN_API_TOKEN
}`,
);
await res.body.pipe(destinationStream);
}
async createClientIfNotExists(client) {
const clients = await this.fetchClients();
// Consider a client to exist
const existingClients = clients.filter(c => c.name === client.name);
if (existingClients.length > 1) {
console.log("existingClients", existingClients);
throw new Error(
"More than one existing clients found - adjust in InvoiceOcean.com and try again",
);
}
if (existingClients.length === 1) {
console.log(
"Found existing client, not creating a new one (nor updating the existing)",
);
return existingClients[0];
}
return await this.createClient(client);
}
async createClient(client) {
const payload = {
api_token: this.INVOICEOCEAN_API_TOKEN,
client: client,
};
const res = await fetch(
`https://${this.INVOICEOCEAN_DOMAIN}.invoiceocean.com/clients.json`,
{
method: "post",
body: JSON.stringify(payload),
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
},
);
const json = await res.json();
if (json.code && json.code === "error") {
console.error("InvoiceOcean API error - createClient: ", json);
throw new Error(`InvoiceOcean API error - createClient: ${json.message}`);
}
return json;
}
async createInvoiceIfNotExists(invoice) {
const invoices = await this.fetchInvoices();
// console.log("invoices", invoices);
/*
const detailedInvoices = await Promise.all(
invoices.map(invoice => {
return invoiceOcean.fetchInvoice(invoice.id);
}),
);
console.log("detailedInvoices", detailedInvoices);
*/
// Consider a invoice to exist if the number is the same
const existingInvoices = invoices.filter(i => i.number === invoice.number);
if (existingInvoices.length > 1) {
console.log("existingInvoices", existingInvoices);
throw new Error(
"More than one existing invoices found - adjust in InvoiceOcean.com and try again",
);
}
if (existingInvoices.length === 1) {
console.log(
"Found existing invoice, not creating a new one (nor updating the existing)",
);
return existingInvoices[0];
}
return await this.createInvoice(invoice);
}
async createInvoice(invoice) {
if (invoice.client) {
const client = await this.createClientIfNotExists(invoice.client);
invoice.client_id = client.id;
invoice.buyer_name = client.name;
delete invoice.client;
}
const payload = {
api_token: this.INVOICEOCEAN_API_TOKEN,
invoice: invoice,
};
console.log("Creating invoice using the following payload: ");
console.dir(payload);
const res = await fetch(
`https://${this.INVOICEOCEAN_DOMAIN}.invoiceocean.com/invoices.json`,
{
method: "post",
body: JSON.stringify(payload),
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
},
);
const json = await res.json();
if (json.code && json.code === "error") {
console.error("InvoiceOcean API error - createInvoice: ", json);
throw new Error(
`InvoiceOcean API error - createInvoice: ${json.message}`,
);
}
return json;
}
}