Skip to content

Commit 1a21aab

Browse files
committed
Add automations methods
1 parent 0d14657 commit 1a21aab

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

src/Afip.js

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function Afip(options = {}){
2525
/**
2626
* SDK version
2727
**/
28-
this.sdk_version_number = '1.1.1';
28+
this.sdk_version_number = '1.2.0';
2929

3030
/**
3131
* X.509 certificate in PEM format
@@ -51,9 +51,8 @@ function Afip(options = {}){
5151
// Create an Afip instance if it is not
5252
if (!(this instanceof Afip)) {return new Afip(options)}
5353

54-
if (!options.hasOwnProperty('CUIT')) {throw new Error("CUIT field is required in options array");}
55-
5654
// Define default options
55+
if (!options.hasOwnProperty('CUIT')) {options['CUIT'] = undefined;}
5756
if (!options.hasOwnProperty('production')) {options['production'] = false;}
5857
if (!options.hasOwnProperty('cert')) {options['cert'] = undefined;}
5958
if (!options.hasOwnProperty('key')) {options['key'] = undefined;}
@@ -169,7 +168,10 @@ Afip.prototype.WebService = function (service, options = {}) {
169168
}
170169

171170
/**
172-
* Create AFIP cert
171+
* Create AFIP cert.
172+
*
173+
* **This method is deprecated and will be removed in future major versions.**
174+
* @deprecated Use CreateAutomation instead
173175
*
174176
* @param {string} username Username used in AFIP page
175177
* @param {string} password Password used in AFIP page
@@ -210,6 +212,9 @@ Afip.prototype.CreateCert = async function(username, password, alias) {
210212
/**
211213
* Create authorization to use a web service
212214
*
215+
* **This method is deprecated and will be removed in future major versions.**
216+
* @deprecated Use CreateAutomation instead
217+
*
213218
* @param {string} username Username used in AFIP page
214219
* @param {string} password Password used in AFIP page
215220
* @param {string} alias Cert alias
@@ -245,5 +250,51 @@ Afip.prototype.CreateWSAuth = async function(username, password, alias, wsid) {
245250
await (new Promise(resolve => setTimeout(resolve, 5000)));
246251
}
247252

253+
throw new Error('Error: Waiting for too long');
254+
}
255+
256+
/**
257+
* Create automation
258+
*
259+
* @param {string} automation Name of the automation
260+
* @param {array} params Parameters to send to the automation
261+
* @param {boolean} wait Wait for the automation to finish (default true)
262+
**/
263+
Afip.prototype.CreateAutomation = async function(automation, params, wait = true) {
264+
// Prepare data to for request
265+
const data = { automation, params };
266+
267+
// Execute request
268+
const result = await this.AdminClient.post('v1/automations', data);
269+
270+
if (!wait || result.data.status === 'complete') {
271+
return result.data;
272+
}
273+
274+
return this.GetAutomationDetails(result.data.id, wait);
275+
}
276+
277+
/**
278+
* Create automation
279+
*
280+
* @param {string} $id Id of the automation
281+
* @param {boolean} $wait Wait for the automation to finish (default false)
282+
**/
283+
Afip.prototype.GetAutomationDetails = async function(id, wait = false) {
284+
// Wait for max 120 seconds
285+
let retry = 24;
286+
287+
while (retry-- >= 0) {
288+
// Execute request
289+
const result = await this.AdminClient.get(`v1/automations/${id}`);
290+
291+
if (!wait || result.data.status === 'complete') {
292+
return result.data;
293+
}
294+
295+
// Wait 5 seconds
296+
await (new Promise(resolve => setTimeout(resolve, 5000)));
297+
}
298+
248299
throw new Error('Error: Waiting for too long');
249300
}

0 commit comments

Comments
 (0)