@@ -25,7 +25,7 @@ function Afip(options = {}){
25
25
/**
26
26
* SDK version
27
27
**/
28
- this . sdk_version_number = '1.1.1 ' ;
28
+ this . sdk_version_number = '1.2.0 ' ;
29
29
30
30
/**
31
31
* X.509 certificate in PEM format
@@ -51,9 +51,8 @@ function Afip(options = {}){
51
51
// Create an Afip instance if it is not
52
52
if ( ! ( this instanceof Afip ) ) { return new Afip ( options ) }
53
53
54
- if ( ! options . hasOwnProperty ( 'CUIT' ) ) { throw new Error ( "CUIT field is required in options array" ) ; }
55
-
56
54
// Define default options
55
+ if ( ! options . hasOwnProperty ( 'CUIT' ) ) { options [ 'CUIT' ] = undefined ; }
57
56
if ( ! options . hasOwnProperty ( 'production' ) ) { options [ 'production' ] = false ; }
58
57
if ( ! options . hasOwnProperty ( 'cert' ) ) { options [ 'cert' ] = undefined ; }
59
58
if ( ! options . hasOwnProperty ( 'key' ) ) { options [ 'key' ] = undefined ; }
@@ -169,7 +168,10 @@ Afip.prototype.WebService = function (service, options = {}) {
169
168
}
170
169
171
170
/**
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
173
175
*
174
176
* @param {string } username Username used in AFIP page
175
177
* @param {string } password Password used in AFIP page
@@ -210,6 +212,9 @@ Afip.prototype.CreateCert = async function(username, password, alias) {
210
212
/**
211
213
* Create authorization to use a web service
212
214
*
215
+ * **This method is deprecated and will be removed in future major versions.**
216
+ * @deprecated Use CreateAutomation instead
217
+ *
213
218
* @param {string } username Username used in AFIP page
214
219
* @param {string } password Password used in AFIP page
215
220
* @param {string } alias Cert alias
@@ -245,5 +250,51 @@ Afip.prototype.CreateWSAuth = async function(username, password, alias, wsid) {
245
250
await ( new Promise ( resolve => setTimeout ( resolve , 5000 ) ) ) ;
246
251
}
247
252
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
+
248
299
throw new Error ( 'Error: Waiting for too long' ) ;
249
300
}
0 commit comments