cozy-harvest-lib 9.23.2 → 9.23.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [9.23.3](https://github.com/cozy/cozy-libs/compare/cozy-harvest-lib@9.23.2...cozy-harvest-lib@9.23.3) (2022-07-27)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * Add ts-check on biWebview ([1920474](https://github.com/cozy/cozy-libs/commit/1920474b85ad342adbd12a363612918db1665bca))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [9.23.2](https://github.com/cozy/cozy-libs/compare/cozy-harvest-lib@9.23.1...cozy-harvest-lib@9.23.2) (2022-07-26)
7
18
 
8
19
 
@@ -16,6 +16,7 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (O
16
16
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
17
17
 
18
18
  import _regeneratorRuntime from "@babel/runtime/regenerator";
19
+ // @ts-check
19
20
 
20
21
  /**
21
22
  * Interface between Budget Insight and Cozy using BI's webview
@@ -25,13 +26,20 @@ import _regeneratorRuntime from "@babel/runtime/regenerator";
25
26
  import { getBIConnection, getBIConnectionAccountsList } from './bi-http';
26
27
  import assert from '../assert';
27
28
  import logger from '../logger';
28
- import { Q } from 'cozy-client';
29
+ import { Q } from 'cozy-client'; // @ts-ignore (its a peerDep and I don't know how to configure ts for that)
30
+
29
31
  import flag from 'cozy-flags';
30
32
  import { setBIConnectionId, saveBIConfig, findAccountWithBiConnection, convertBIErrortoKonnectorJobError, isBudgetInsightConnector, getBIConnectionIdFromAccount } from './budget-insight';
31
33
  import { KonnectorJobError } from '../helpers/konnectors';
32
34
  import { waitForRealtimeEvent } from './jobUtils';
33
35
  import '../types';
34
36
  import { LOGIN_SUCCESS_EVENT } from '../models/flowEvents';
37
+ /**
38
+ * @typedef {import("../models/ConnectionFlow").default} ConnectionFlow
39
+ * @typedef {object} CozyClient
40
+
41
+ */
42
+
35
43
  var TEMP_TOKEN_TIMOUT_S = 60;
36
44
  export var ACCOUNTS_DOCTYPE = 'io.cozy.accounts';
37
45
  export var isBiWebViewConnector = function isBiWebViewConnector(konnector) {
@@ -39,13 +47,13 @@ export var isBiWebViewConnector = function isBiWebViewConnector(konnector) {
39
47
  };
40
48
  /**
41
49
  * Runs multiple checks on the bi connection referenced in the given account
42
- *
43
- * @param {io.cozy.accounts} options.account The account content
44
- * @param {ConnectionFlow} options.flow
45
- * @param {io.cozy.konnectors} options.konnector connector manifest content
50
+ * @param {object} options
51
+ * @param {IoCozyAccount} options.account The account content
52
+ * @param {KonnectorManifest} options.konnector konnector manifest content
53
+ * @param {ConnectionFlow} options.flow The flow
46
54
  * @param {CozyClient} options.client CozyClient object
47
55
  *
48
- * @return {Integer} Connection Id
56
+ * @return {Promise<biConnection>} Connection Id
49
57
  */
50
58
 
51
59
  export var checkBIConnection = /*#__PURE__*/function () {
@@ -89,28 +97,27 @@ export var checkBIConnection = /*#__PURE__*/function () {
89
97
  sameAccount = _context.sent;
90
98
 
91
99
  if (!sameAccount) {
92
- _context.next = 22;
100
+ _context.next = 21;
93
101
  break;
94
102
  }
95
103
 
96
104
  err = new KonnectorJobError('ACCOUNT_WITH_SAME_IDENTIFIER_ALREADY_DEFINED');
97
- err.accountId = sameAccount._id;
98
105
  throw err;
99
106
 
100
- case 22:
107
+ case 21:
101
108
  return _context.abrupt("return", connection);
102
109
 
103
- case 25:
104
- _context.prev = 25;
110
+ case 24:
111
+ _context.prev = 24;
105
112
  _context.t0 = _context["catch"](1);
106
113
  return _context.abrupt("return", convertBIErrortoKonnectorJobError(_context.t0));
107
114
 
108
- case 28:
115
+ case 27:
109
116
  case "end":
110
117
  return _context.stop();
111
118
  }
112
119
  }
113
- }, _callee, null, [[1, 25]]);
120
+ }, _callee, null, [[1, 24]]);
114
121
  }));
115
122
 
116
123
  return function checkBIConnection(_x) {
@@ -156,12 +163,14 @@ export var fetchContractSynchronizationUrl = /*#__PURE__*/function () {
156
163
  /**
157
164
  * Handles webview connection
158
165
  *
159
- * @param {io.cozy.accounts} options.account The account content
160
- * @param {ConnectionFlow} options.flow
161
- * @param {io.cozy.konnectors} options.konnector connector manifest content
166
+ * @param {object} options
167
+ * @param {IoCozyAccount} options.account The account content
168
+ * @param {KonnectorManifest} options.konnector konnector manifest content
169
+ * @param {ConnectionFlow} options.flow The flow
162
170
  * @param {CozyClient} options.client CozyClient object
163
- *
164
- * @return {Integer} Connection Id
171
+ * @param {Boolean} options.reconnect If this is a reconnexion
172
+ * @param {Function} options.t Translation fonction
173
+ * @return {Promise<Number>} Connection Id
165
174
  */
166
175
 
167
176
  export var handleOAuthAccount = /*#__PURE__*/function () {
@@ -232,7 +241,7 @@ export var handleOAuthAccount = /*#__PURE__*/function () {
232
241
  /**
233
242
  * Return the bi aggregator parent relationship configuration for a given konnector
234
243
  *
235
- * @param {io.cozy.konnectors} konnector connector manifest content
244
+ * @param {KonnectorManifest} konnector connector manifest content
236
245
  *
237
246
  * @return {Object}
238
247
  */
@@ -261,9 +270,9 @@ var getBiAggregatorParentRelationship = function getBiAggregatorParentRelationsh
261
270
  * Gets BI webview connection id which is returned in the account by the stack
262
271
  * via oauth callback url
263
272
  *
264
- * @param {io.cozy.accounts} The account content created by the stack
273
+ * @param {IoCozyAccount} account The account content created by the stack
265
274
  *
266
- * @return {Integer} Connection Id
275
+ * @return {Number|null} Connection Id or null if no connexion
267
276
  */
268
277
 
269
278
 
@@ -274,13 +283,13 @@ var getWebviewBIConnectionId = function getWebviewBIConnectionId(account) {
274
283
  };
275
284
  /**
276
285
  * Hook from ConnectionFlow after account creation
277
- *
278
- * @param {io.cozy.accounts} options.account - created account
279
- * @param {io.cozy.konnectors} options.konnector - manifest of the konnector for which the account is created
286
+ * @param {object} options
287
+ * @param {IoCozyAccount} options.account - created account
288
+ * @param {KonnectorManifest} options.konnector konnector manifest content
280
289
  * @param {ConnectionFlow} options.flow - current ConnectionFlow instance
281
290
  * @param {CozyClient} options.client - current CozyClient instance
282
291
  *
283
- * @returns {Promise<io.cozy.accounts>}
292
+ * @returns {Promise<IoCozyAccount>}
284
293
  */
285
294
 
286
295
 
@@ -334,59 +343,43 @@ export var onBIAccountCreation = /*#__PURE__*/function () {
334
343
  }();
335
344
  /**
336
345
  * Create OAuth extra parameters specific to reconnect webview
337
- *
338
- * @param {Array<Number>} options.biBankIds - connector bank ids (for webview connectors)
346
+ * @param {object} options
347
+ * @param {Array<String>} options.biBankIds - connector bank ids (for webview connectors)
339
348
  * @param {String} options.token - BI temporary token
340
349
  * @param {Number} options.connId - BI bi connection id
341
350
  * @return {Object}
342
351
  */
343
352
 
344
- var getReconnectExtraOAuthUrlParams = /*#__PURE__*/function () {
345
- var _ref10 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(_ref9) {
346
- var biBankIds, token, connId;
347
- return _regeneratorRuntime.wrap(function _callee5$(_context5) {
348
- while (1) {
349
- switch (_context5.prev = _context5.next) {
350
- case 0:
351
- biBankIds = _ref9.biBankIds, token = _ref9.token, connId = _ref9.connId;
352
- return _context5.abrupt("return", {
353
- id_connector: biBankIds,
354
- code: token,
355
- connection_id: connId
356
- });
357
-
358
- case 2:
359
- case "end":
360
- return _context5.stop();
361
- }
362
- }
363
- }, _callee5);
364
- }));
365
-
366
- return function getReconnectExtraOAuthUrlParams(_x5) {
367
- return _ref10.apply(this, arguments);
353
+ var getReconnectExtraOAuthUrlParams = function getReconnectExtraOAuthUrlParams(_ref9) {
354
+ var biBankIds = _ref9.biBankIds,
355
+ token = _ref9.token,
356
+ connId = _ref9.connId;
357
+ return {
358
+ id_connector: biBankIds,
359
+ code: token,
360
+ connection_id: connId
368
361
  };
369
- }();
362
+ };
370
363
  /**
371
364
  * Create OAuth extra parameters
372
- *
365
+ * @param {object} options
373
366
  * @param {CozyClient} options.client - CozyClient instance
374
- * @param {io.cozy.konnectors} options.konnector connector manifest content
375
- * @param {io.cozy.accounts} options.account The account content
367
+ * @param {KonnectorManifest} options.konnector konnector manifest content
368
+ * @param {IoCozyAccount} options.account The account content
376
369
  * @return {Promise<Object>}
377
370
  */
378
371
 
379
372
 
380
373
  export var fetchExtraOAuthUrlParams = /*#__PURE__*/function () {
381
- var _ref12 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(_ref11) {
374
+ var _ref11 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(_ref10) {
382
375
  var client, konnector, account, _yield$createTemporar2, token, biBankIds, connId, isReconnect;
383
376
 
384
- return _regeneratorRuntime.wrap(function _callee6$(_context6) {
377
+ return _regeneratorRuntime.wrap(function _callee5$(_context5) {
385
378
  while (1) {
386
- switch (_context6.prev = _context6.next) {
379
+ switch (_context5.prev = _context5.next) {
387
380
  case 0:
388
- client = _ref11.client, konnector = _ref11.konnector, account = _ref11.account;
389
- _context6.next = 3;
381
+ client = _ref10.client, konnector = _ref10.konnector, account = _ref10.account;
382
+ _context5.next = 3;
390
383
  return createTemporaryToken({
391
384
  client: client,
392
385
  konnector: konnector,
@@ -394,54 +387,54 @@ export var fetchExtraOAuthUrlParams = /*#__PURE__*/function () {
394
387
  });
395
388
 
396
389
  case 3:
397
- _yield$createTemporar2 = _context6.sent;
390
+ _yield$createTemporar2 = _context5.sent;
398
391
  token = _yield$createTemporar2.code;
399
392
  biBankIds = _yield$createTemporar2.biBankIds;
400
393
  connId = getBIConnectionIdFromAccount(account);
401
394
  isReconnect = Boolean(connId);
402
395
 
403
396
  if (!isReconnect) {
404
- _context6.next = 12;
397
+ _context5.next = 12;
405
398
  break;
406
399
  }
407
400
 
408
- return _context6.abrupt("return", getReconnectExtraOAuthUrlParams({
401
+ return _context5.abrupt("return", getReconnectExtraOAuthUrlParams({
409
402
  biBankIds: biBankIds,
410
403
  token: token,
411
404
  connId: connId
412
405
  }));
413
406
 
414
407
  case 12:
415
- return _context6.abrupt("return", {
408
+ return _context5.abrupt("return", {
416
409
  id_connector: biBankIds,
417
410
  token: token
418
411
  });
419
412
 
420
413
  case 13:
421
414
  case "end":
422
- return _context6.stop();
415
+ return _context5.stop();
423
416
  }
424
417
  }
425
- }, _callee6);
418
+ }, _callee5);
426
419
  }));
427
420
 
428
- return function fetchExtraOAuthUrlParams(_x6) {
429
- return _ref12.apply(this, arguments);
421
+ return function fetchExtraOAuthUrlParams(_x5) {
422
+ return _ref11.apply(this, arguments);
430
423
  };
431
424
  }();
432
425
  /**
433
426
  * Finds the current bankIid in a given konnector or account
434
- *
435
- * @param {io.cozy.accounts} options.account The account content
436
- * @param {io.cozy.konnectors} options.konnector connector manifest content
427
+ * @param {object} options
428
+ * @param {IoCozyAccount} options.account The account content
429
+ * @param {KonnectorManifest} options.konnector konnector manifest content
437
430
  * @return {Array<String>} - list of bank ids
438
431
  */
439
432
 
440
- export var getCozyBankIds = function getCozyBankIds(_ref13) {
433
+ export var getCozyBankIds = function getCozyBankIds(_ref12) {
441
434
  var _konnector$parameters, _account$auth, _konnector$fields, _konnector$fields$ban;
442
435
 
443
- var konnector = _ref13.konnector,
444
- account = _ref13.account;
436
+ var konnector = _ref12.konnector,
437
+ account = _ref12.account;
445
438
  var cozyBankId = (konnector === null || konnector === void 0 ? void 0 : (_konnector$parameters = konnector.parameters) === null || _konnector$parameters === void 0 ? void 0 : _konnector$parameters.bankId) || (account === null || account === void 0 ? void 0 : (_account$auth = account.auth) === null || _account$auth === void 0 ? void 0 : _account$auth.bankId);
446
439
 
447
440
  if (cozyBankId) {
@@ -460,24 +453,24 @@ export var getCozyBankIds = function getCozyBankIds(_ref13) {
460
453
  };
461
454
  /**
462
455
  * Update imported state of contracts in the given account, according to current state of the accounts on the BI side.
463
- *
456
+ * @param {object} options
464
457
  * @param {CozyClient} options.client - CozyClient instance
465
- * @param {io.cozy.accounts} options.account The account content
466
- * @param {io.cozy.konnectors} options.konnector connector manifest content
458
+ * @param {IoCozyAccount} options.account The account content
459
+ * @param {KonnectorManifest} options.konnector konnector manifest content
467
460
  */
468
461
 
469
462
  export var refreshContracts = /*#__PURE__*/function () {
470
- var _ref15 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(_ref14) {
463
+ var _ref14 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(_ref13) {
471
464
  var _account$relationship, _account$relationship2;
472
465
 
473
466
  var client, konnector, account, biConfig, code, config, connectionId, _yield$getBIConnectio, contracts, contractsById, currentContractsList, _iterator, _step, currentContract, disabledValue;
474
467
 
475
- return _regeneratorRuntime.wrap(function _callee7$(_context7) {
468
+ return _regeneratorRuntime.wrap(function _callee6$(_context6) {
476
469
  while (1) {
477
- switch (_context7.prev = _context7.next) {
470
+ switch (_context6.prev = _context6.next) {
478
471
  case 0:
479
- client = _ref14.client, konnector = _ref14.konnector, account = _ref14.account;
480
- _context7.next = 3;
472
+ client = _ref13.client, konnector = _ref13.konnector, account = _ref13.account;
473
+ _context6.next = 3;
481
474
  return createTemporaryToken({
482
475
  client: client,
483
476
  konnector: konnector,
@@ -485,14 +478,14 @@ export var refreshContracts = /*#__PURE__*/function () {
485
478
  });
486
479
 
487
480
  case 3:
488
- biConfig = _context7.sent;
481
+ biConfig = _context6.sent;
489
482
  code = biConfig.code, config = _objectWithoutProperties(biConfig, _excluded2);
490
483
  connectionId = getWebviewBIConnectionId(account);
491
- _context7.next = 8;
484
+ _context6.next = 8;
492
485
  return getBIConnectionAccountsList(config, connectionId, code);
493
486
 
494
487
  case 8:
495
- _yield$getBIConnectio = _context7.sent;
488
+ _yield$getBIConnectio = _context6.sent;
496
489
  contracts = _yield$getBIConnectio.accounts;
497
490
  contractsById = contracts.reduce(function (memo, contract) {
498
491
  return _objectSpread(_objectSpread({}, memo), {}, _defineProperty({}, contract.id + '', contract.disabled));
@@ -513,19 +506,19 @@ export var refreshContracts = /*#__PURE__*/function () {
513
506
  _iterator.f();
514
507
  }
515
508
 
516
- _context7.next = 16;
509
+ _context6.next = 16;
517
510
  return client.save(account);
518
511
 
519
512
  case 16:
520
513
  case "end":
521
- return _context7.stop();
514
+ return _context6.stop();
522
515
  }
523
516
  }
524
- }, _callee7);
517
+ }, _callee6);
525
518
  }));
526
519
 
527
- return function refreshContracts(_x7) {
528
- return _ref15.apply(this, arguments);
520
+ return function refreshContracts(_x6) {
521
+ return _ref14.apply(this, arguments);
529
522
  };
530
523
  }();
531
524
 
@@ -536,49 +529,47 @@ function convertBIDateToStandardDate(biDate) {
536
529
  * Get the BI temporary token cache from Cozy doctype
537
530
  *
538
531
  * @param {CozyClient} options.client CozyClient instance
539
- * @return {createTemporaryTokenResponse}
532
+ * @return {Promise<createTemporaryTokenResponse>}
540
533
  */
541
534
 
542
535
 
543
- function getBiTemporaryTokenFromCache(_x8) {
536
+ function getBiTemporaryTokenFromCache(_x7) {
544
537
  return _getBiTemporaryTokenFromCache.apply(this, arguments);
545
538
  }
546
539
  /**
547
540
  * Check if BI temporary token cache is expired or not
548
- *
549
- * @param {createTemporaryTokenResponse} options.tokenCache
541
+ * @param {createTemporaryTokenResponse} tokenCache
542
+ * @param {Object} biUser the bi user
550
543
  * @return {Boolean}
551
544
  */
552
545
 
553
546
 
554
547
  function _getBiTemporaryTokenFromCache() {
555
- _getBiTemporaryTokenFromCache = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9(_ref16) {
548
+ _getBiTemporaryTokenFromCache = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(_ref15) {
556
549
  var client, queryResult;
557
- return _regeneratorRuntime.wrap(function _callee9$(_context9) {
550
+ return _regeneratorRuntime.wrap(function _callee8$(_context8) {
558
551
  while (1) {
559
- switch (_context9.prev = _context9.next) {
552
+ switch (_context8.prev = _context8.next) {
560
553
  case 0:
561
- client = _ref16.client;
562
- _context9.next = 3;
554
+ client = _ref15.client;
555
+ _context8.next = 3;
563
556
  return client.query(Q('io.cozy.bank.settings').getById('bi'));
564
557
 
565
558
  case 3:
566
- queryResult = _context9.sent;
567
- return _context9.abrupt("return", (queryResult === null || queryResult === void 0 ? void 0 : queryResult.data) || {});
559
+ queryResult = _context8.sent;
560
+ return _context8.abrupt("return", (queryResult === null || queryResult === void 0 ? void 0 : queryResult.data) || {});
568
561
 
569
562
  case 5:
570
563
  case "end":
571
- return _context9.stop();
564
+ return _context8.stop();
572
565
  }
573
566
  }
574
- }, _callee9);
567
+ }, _callee8);
575
568
  }));
576
569
  return _getBiTemporaryTokenFromCache.apply(this, arguments);
577
570
  }
578
571
 
579
- export function isCacheExpired(_ref17) {
580
- var tokenCache = _ref17.tokenCache,
581
- biUser = _ref17.biUser;
572
+ export function isCacheExpired(tokenCache, biUser) {
582
573
  var cacheAge = Date.now() - Number(tokenCache === null || tokenCache === void 0 ? void 0 : tokenCache.timestamp);
583
574
  logger.debug('tokenCache age', cacheAge / 1000 / 60, 'minutes');
584
575
  var MAX_TOKEN_CACHE_AGE = 29 * 60 * 1000;
@@ -596,37 +587,37 @@ export function isCacheExpired(_ref17) {
596
587
  }
597
588
  /**
598
589
  * Update the BI temporary token cache from BI itself
599
- *
590
+ * @param {object} options
600
591
  * @param {CozyClient} options.client CozyClient instance
601
- * @param {io.cozy.konnectors} options.konnector
602
- * @param {Array<Number>} options.cozyBankIds List of cozy bank identifiers
592
+ * @param {KonnectorManifest} options.konnector konnector manifest content
593
+ * @param {Array<String>} options.cozyBankIds List of cozy bank identifiers
603
594
  * @param {createTemporaryTokenResponse} options.tokenCache Previous version of BI temporary token cache
604
- * @return {createTemporaryTokenResponse}
595
+ * @return {Promise<createTemporaryTokenResponse>}
605
596
  */
606
597
 
607
- function updateCache(_x9) {
598
+ function updateCache(_x8) {
608
599
  return _updateCache.apply(this, arguments);
609
600
  }
610
601
  /**
611
602
  * Gets a temporary token corresponding to the current BI user
612
- *
603
+ * @param {object} options
613
604
  * @param {CozyClient} options.client - CozyClient instance
614
- * @param {io.cozy.konnectors} options.konnector connector manifest content
615
- * @param {io.cozy.accounts} options.account The account content
605
+ * @param {KonnectorManifest} options.konnector konnector manifest content
606
+ * @param {IoCozyAccount} options.account The account content
616
607
  *
617
- * @returns {createTemporaryTokenResponse}
608
+ * @returns {Promise<createTemporaryTokenResponse>}
618
609
  */
619
610
 
620
611
 
621
612
  function _updateCache() {
622
- _updateCache = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10(_ref18) {
613
+ _updateCache = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9(_ref16) {
623
614
  var client, konnector, tokenCache, cozyBankIds, jobResponse, event, saveResult;
624
- return _regeneratorRuntime.wrap(function _callee10$(_context10) {
615
+ return _regeneratorRuntime.wrap(function _callee9$(_context9) {
625
616
  while (1) {
626
- switch (_context10.prev = _context10.next) {
617
+ switch (_context9.prev = _context9.next) {
627
618
  case 0:
628
- client = _ref18.client, konnector = _ref18.konnector, tokenCache = _ref18.tokenCache, cozyBankIds = _ref18.cozyBankIds;
629
- _context10.next = 3;
619
+ client = _ref16.client, konnector = _ref16.konnector, tokenCache = _ref16.tokenCache, cozyBankIds = _ref16.cozyBankIds;
620
+ _context9.next = 3;
630
621
  return client.stackClient.jobs.create('konnector', {
631
622
  mode: 'getTemporaryToken',
632
623
  konnector: konnector.slug,
@@ -634,13 +625,13 @@ function _updateCache() {
634
625
  }, {}, true);
635
626
 
636
627
  case 3:
637
- jobResponse = _context10.sent;
638
- _context10.next = 6;
628
+ jobResponse = _context9.sent;
629
+ _context9.next = 6;
639
630
  return waitForRealtimeEvent(client, jobResponse.data.attributes, 'result', TEMP_TOKEN_TIMOUT_S * 1000);
640
631
 
641
632
  case 6:
642
- event = _context10.sent;
643
- _context10.next = 9;
633
+ event = _context9.sent;
634
+ _context9.next = 9;
644
635
  return client.save(_objectSpread(_objectSpread({
645
636
  _type: 'io.cozy.bank.settings',
646
637
  _id: 'bi'
@@ -651,59 +642,56 @@ function _updateCache() {
651
642
  }, event.data.result));
652
643
 
653
644
  case 9:
654
- saveResult = _context10.sent;
655
- return _context10.abrupt("return", saveResult.data);
645
+ saveResult = _context9.sent;
646
+ return _context9.abrupt("return", saveResult.data);
656
647
 
657
648
  case 11:
658
649
  case "end":
659
- return _context10.stop();
650
+ return _context9.stop();
660
651
  }
661
652
  }
662
- }, _callee10);
653
+ }, _callee9);
663
654
  }));
664
655
  return _updateCache.apply(this, arguments);
665
656
  }
666
657
 
667
658
  export var createTemporaryToken = /*#__PURE__*/function () {
668
- var _ref20 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(_ref19) {
659
+ var _ref18 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(_ref17) {
669
660
  var _tokenCache;
670
661
 
671
662
  var client, konnector, account, tokenCache, cozyBankIds, _yield$client$query, biUser, _tokenCache2, biMapping;
672
663
 
673
- return _regeneratorRuntime.wrap(function _callee8$(_context8) {
664
+ return _regeneratorRuntime.wrap(function _callee7$(_context7) {
674
665
  while (1) {
675
- switch (_context8.prev = _context8.next) {
666
+ switch (_context7.prev = _context7.next) {
676
667
  case 0:
677
- client = _ref19.client, konnector = _ref19.konnector, account = _ref19.account;
668
+ client = _ref17.client, konnector = _ref17.konnector, account = _ref17.account;
678
669
  assert(konnector.slug, 'createTemporaryToken: konnector passed in options has no slug');
679
- _context8.next = 4;
670
+ _context7.next = 4;
680
671
  return getBiTemporaryTokenFromCache({
681
672
  client: client
682
673
  });
683
674
 
684
675
  case 4:
685
- tokenCache = _context8.sent;
676
+ tokenCache = _context7.sent;
686
677
  cozyBankIds = getCozyBankIds({
687
678
  konnector: konnector,
688
679
  account: account
689
680
  });
690
- _context8.next = 8;
681
+ _context7.next = 8;
691
682
  return client.query(Q('io.cozy.accounts').getById('bi-aggregator-user'));
692
683
 
693
684
  case 8:
694
- _yield$client$query = _context8.sent;
685
+ _yield$client$query = _context7.sent;
695
686
  biUser = _yield$client$query.data;
696
687
 
697
- if (!isCacheExpired({
698
- tokenCache: tokenCache,
699
- biUser: biUser
700
- })) {
701
- _context8.next = 15;
688
+ if (!isCacheExpired(tokenCache, biUser)) {
689
+ _context7.next = 15;
702
690
  break;
703
691
  }
704
692
 
705
693
  logger.debug('temporaryToken cache is expired. Updating');
706
- _context8.next = 14;
694
+ _context7.next = 14;
707
695
  return updateCache({
708
696
  client: client,
709
697
  konnector: konnector,
@@ -712,7 +700,7 @@ export var createTemporaryToken = /*#__PURE__*/function () {
712
700
  });
713
701
 
714
702
  case 14:
715
- tokenCache = _context8.sent;
703
+ tokenCache = _context7.sent;
716
704
 
717
705
  case 15:
718
706
  assert(cozyBankIds.length, 'createTemporaryToken: Could not determine cozyBankIds from account or konnector');
@@ -721,18 +709,18 @@ export var createTemporaryToken = /*#__PURE__*/function () {
721
709
  tokenCache.biBankIds = _toConsumableArray(new Set(cozyBankIds.map(function (id) {
722
710
  return biMapping[id];
723
711
  })));
724
- return _context8.abrupt("return", tokenCache);
712
+ return _context7.abrupt("return", tokenCache);
725
713
 
726
714
  case 20:
727
715
  case "end":
728
- return _context8.stop();
716
+ return _context7.stop();
729
717
  }
730
718
  }
731
- }, _callee8);
719
+ }, _callee7);
732
720
  }));
733
721
 
734
- return function createTemporaryToken(_x10) {
735
- return _ref20.apply(this, arguments);
722
+ return function createTemporaryToken(_x9) {
723
+ return _ref18.apply(this, arguments);
736
724
  };
737
725
  }();
738
726
  export var konnectorPolicy = {
@@ -548,10 +548,7 @@ describe('isCacheExpired', function () {
548
548
  var biUser = {
549
549
  userId: 666
550
550
  };
551
- expect(isCacheExpired({
552
- tokenCache: tokenCache,
553
- biUser: biUser
554
- })).toBe(false);
551
+ expect(isCacheExpired(tokenCache, biUser)).toBe(false);
555
552
  });
556
553
  it('should be marked as expired when userId did not change and cache is old', function () {
557
554
  var tokenCache = {
@@ -561,10 +558,7 @@ describe('isCacheExpired', function () {
561
558
  var biUser = {
562
559
  userId: 666
563
560
  };
564
- expect(isCacheExpired({
565
- tokenCache: tokenCache,
566
- biUser: biUser
567
- })).toBe(true);
561
+ expect(isCacheExpired(tokenCache, biUser)).toBe(true);
568
562
  });
569
563
  it('should be marked as expired when userId did change and cache is old', function () {
570
564
  var tokenCache = {
@@ -574,10 +568,7 @@ describe('isCacheExpired', function () {
574
568
  var biUser = {
575
569
  userId: 667
576
570
  };
577
- expect(isCacheExpired({
578
- tokenCache: tokenCache,
579
- biUser: biUser
580
- })).toBe(true);
571
+ expect(isCacheExpired(tokenCache, biUser)).toBe(true);
581
572
  });
582
573
  it('should be marked as expired when userId did change and cache is not old', function () {
583
574
  var tokenCache = {
@@ -587,10 +578,7 @@ describe('isCacheExpired', function () {
587
578
  var biUser = {
588
579
  userId: 667
589
580
  };
590
- expect(isCacheExpired({
591
- tokenCache: tokenCache,
592
- biUser: biUser
593
- })).toBe(true);
581
+ expect(isCacheExpired(tokenCache, biUser)).toBe(true);
594
582
  });
595
583
  it('should be marked as expired when cache has no user id', function () {
596
584
  var tokenCache = {
@@ -599,10 +587,7 @@ describe('isCacheExpired', function () {
599
587
  var biUser = {
600
588
  userId: 666
601
589
  };
602
- expect(isCacheExpired({
603
- tokenCache: tokenCache,
604
- biUser: biUser
605
- })).toBe(true);
590
+ expect(isCacheExpired(tokenCache, biUser)).toBe(true);
606
591
  });
607
592
  });
608
593
  describe('fetchExtraOAuthUrlParams', function () {
package/dist/types.js CHANGED
@@ -7,6 +7,12 @@
7
7
  * @property {Array<String>} bankIds - Cozy bank ids corresponding to the current connector
8
8
  * @property {Array<String>} biBankIds - BI bank ids corresponding to the bankIds translated by the connector
9
9
  * @property {Object} biMapping - Association table of any cozy bank id to corresponding bi bank id
10
+ * @property {String?} _rev
11
+ * @property {String} publicKey
12
+ * @property {String} clientId
13
+ * @property {String} mode
14
+ * @property {timestamp?} timestamp
15
+ * @property {String} userId - BI User id used to create the tmp token
10
16
  */
11
17
 
12
18
  /**
@@ -20,4 +26,21 @@
20
26
  * @property {boolean} active - Whether this connection is active and will be automatically synced.
21
27
  * @property {string|null} last_push - Date string: Last successfull push
22
28
  * @property {string|null} next_try - Date string: Date of next synchronization.
29
+ */
30
+
31
+ /**
32
+ * @typedef {import("../../node_modules/cozy-client/types/types").CozyClientDocument} CozyClientDocument
33
+ */
34
+
35
+ /**
36
+ * @typedef KonnectorManifest
37
+ * @property {String} slug
38
+ * @property {object} parameters
39
+ * @property {object} fields
40
+ * @property {object} aggregator
41
+ */
42
+
43
+ /**
44
+ * @typedef {CozyClientDocument} IoCozyAccount
45
+ *
23
46
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-harvest-lib",
3
- "version": "9.23.2",
3
+ "version": "9.23.3",
4
4
  "description": "Provides logic, modules and components for Cozy's harvest applications.",
5
5
  "main": "dist/index.js",
6
6
  "author": "Cozy",
@@ -90,5 +90,5 @@
90
90
  "react-router-dom": "^5.0.1"
91
91
  },
92
92
  "sideEffects": false,
93
- "gitHead": "c5c6e0d609fafbc41a4b8cbfa8e1d4582544bbdd"
93
+ "gitHead": "86510d84bad90202b632c59b387c6a8aa58c0705"
94
94
  }
@@ -1,3 +1,4 @@
1
+ // @ts-check
1
2
  /**
2
3
  * Interface between Budget Insight and Cozy using BI's webview
3
4
  *
@@ -8,7 +9,9 @@ import { getBIConnection, getBIConnectionAccountsList } from './bi-http'
8
9
  import assert from '../assert'
9
10
  import logger from '../logger'
10
11
  import { Q } from 'cozy-client'
12
+ // @ts-ignore (its a peerDep and I don't know how to configure ts for that)
11
13
  import flag from 'cozy-flags'
14
+
12
15
  import {
13
16
  setBIConnectionId,
14
17
  saveBIConfig,
@@ -22,6 +25,12 @@ import { waitForRealtimeEvent } from './jobUtils'
22
25
  import '../types'
23
26
  import { LOGIN_SUCCESS_EVENT } from '../models/flowEvents'
24
27
 
28
+ /**
29
+ * @typedef {import("../models/ConnectionFlow").default} ConnectionFlow
30
+ * @typedef {object} CozyClient
31
+
32
+ */
33
+
25
34
  const TEMP_TOKEN_TIMOUT_S = 60
26
35
  export const ACCOUNTS_DOCTYPE = 'io.cozy.accounts'
27
36
 
@@ -30,13 +39,13 @@ export const isBiWebViewConnector = konnector =>
30
39
 
31
40
  /**
32
41
  * Runs multiple checks on the bi connection referenced in the given account
33
- *
34
- * @param {io.cozy.accounts} options.account The account content
35
- * @param {ConnectionFlow} options.flow
36
- * @param {io.cozy.konnectors} options.konnector connector manifest content
42
+ * @param {object} options
43
+ * @param {IoCozyAccount} options.account The account content
44
+ * @param {KonnectorManifest} options.konnector konnector manifest content
45
+ * @param {ConnectionFlow} options.flow The flow
37
46
  * @param {CozyClient} options.client CozyClient object
38
47
  *
39
- * @return {Integer} Connection Id
48
+ * @return {Promise<biConnection>} Connection Id
40
49
  */
41
50
  export const checkBIConnection = async ({
42
51
  account,
@@ -74,7 +83,6 @@ export const checkBIConnection = async ({
74
83
  const err = new KonnectorJobError(
75
84
  'ACCOUNT_WITH_SAME_IDENTIFIER_ALREADY_DEFINED'
76
85
  )
77
- err.accountId = sameAccount._id
78
86
  throw err
79
87
  }
80
88
  return connection
@@ -100,12 +108,14 @@ export const fetchContractSynchronizationUrl = async ({
100
108
  /**
101
109
  * Handles webview connection
102
110
  *
103
- * @param {io.cozy.accounts} options.account The account content
104
- * @param {ConnectionFlow} options.flow
105
- * @param {io.cozy.konnectors} options.konnector connector manifest content
111
+ * @param {object} options
112
+ * @param {IoCozyAccount} options.account The account content
113
+ * @param {KonnectorManifest} options.konnector konnector manifest content
114
+ * @param {ConnectionFlow} options.flow The flow
106
115
  * @param {CozyClient} options.client CozyClient object
107
- *
108
- * @return {Integer} Connection Id
116
+ * @param {Boolean} options.reconnect If this is a reconnexion
117
+ * @param {Function} options.t Translation fonction
118
+ * @return {Promise<Number>} Connection Id
109
119
  */
110
120
  export const handleOAuthAccount = async ({
111
121
  account,
@@ -151,7 +161,7 @@ export const handleOAuthAccount = async ({
151
161
  /**
152
162
  * Return the bi aggregator parent relationship configuration for a given konnector
153
163
  *
154
- * @param {io.cozy.konnectors} konnector connector manifest content
164
+ * @param {KonnectorManifest} konnector connector manifest content
155
165
  *
156
166
  * @return {Object}
157
167
  */
@@ -176,9 +186,9 @@ const getBiAggregatorParentRelationship = konnector => {
176
186
  * Gets BI webview connection id which is returned in the account by the stack
177
187
  * via oauth callback url
178
188
  *
179
- * @param {io.cozy.accounts} The account content created by the stack
189
+ * @param {IoCozyAccount} account The account content created by the stack
180
190
  *
181
- * @return {Integer} Connection Id
191
+ * @return {Number|null} Connection Id or null if no connexion
182
192
  */
183
193
  const getWebviewBIConnectionId = account => {
184
194
  return Number(account?.oauth?.query?.connection_id?.[0] || null)
@@ -186,13 +196,13 @@ const getWebviewBIConnectionId = account => {
186
196
 
187
197
  /**
188
198
  * Hook from ConnectionFlow after account creation
189
- *
190
- * @param {io.cozy.accounts} options.account - created account
191
- * @param {io.cozy.konnectors} options.konnector - manifest of the konnector for which the account is created
199
+ * @param {object} options
200
+ * @param {IoCozyAccount} options.account - created account
201
+ * @param {KonnectorManifest} options.konnector konnector manifest content
192
202
  * @param {ConnectionFlow} options.flow - current ConnectionFlow instance
193
203
  * @param {CozyClient} options.client - current CozyClient instance
194
204
  *
195
- * @returns {Promise<io.cozy.accounts>}
205
+ * @returns {Promise<IoCozyAccount>}
196
206
  */
197
207
  export const onBIAccountCreation = async ({
198
208
  account: fullAccount,
@@ -227,17 +237,13 @@ export const onBIAccountCreation = async ({
227
237
 
228
238
  /**
229
239
  * Create OAuth extra parameters specific to reconnect webview
230
- *
231
- * @param {Array<Number>} options.biBankIds - connector bank ids (for webview connectors)
240
+ * @param {object} options
241
+ * @param {Array<String>} options.biBankIds - connector bank ids (for webview connectors)
232
242
  * @param {String} options.token - BI temporary token
233
243
  * @param {Number} options.connId - BI bi connection id
234
244
  * @return {Object}
235
245
  */
236
- const getReconnectExtraOAuthUrlParams = async ({
237
- biBankIds,
238
- token,
239
- connId
240
- }) => {
246
+ const getReconnectExtraOAuthUrlParams = ({ biBankIds, token, connId }) => {
241
247
  return {
242
248
  id_connector: biBankIds,
243
249
  code: token,
@@ -247,10 +253,10 @@ const getReconnectExtraOAuthUrlParams = async ({
247
253
 
248
254
  /**
249
255
  * Create OAuth extra parameters
250
- *
256
+ * @param {object} options
251
257
  * @param {CozyClient} options.client - CozyClient instance
252
- * @param {io.cozy.konnectors} options.konnector connector manifest content
253
- * @param {io.cozy.accounts} options.account The account content
258
+ * @param {KonnectorManifest} options.konnector konnector manifest content
259
+ * @param {IoCozyAccount} options.account The account content
254
260
  * @return {Promise<Object>}
255
261
  */
256
262
  export const fetchExtraOAuthUrlParams = async ({
@@ -284,9 +290,9 @@ export const fetchExtraOAuthUrlParams = async ({
284
290
 
285
291
  /**
286
292
  * Finds the current bankIid in a given konnector or account
287
- *
288
- * @param {io.cozy.accounts} options.account The account content
289
- * @param {io.cozy.konnectors} options.konnector connector manifest content
293
+ * @param {object} options
294
+ * @param {IoCozyAccount} options.account The account content
295
+ * @param {KonnectorManifest} options.konnector konnector manifest content
290
296
  * @return {Array<String>} - list of bank ids
291
297
  */
292
298
  export const getCozyBankIds = ({ konnector, account }) => {
@@ -305,10 +311,10 @@ export const getCozyBankIds = ({ konnector, account }) => {
305
311
 
306
312
  /**
307
313
  * Update imported state of contracts in the given account, according to current state of the accounts on the BI side.
308
- *
314
+ * @param {object} options
309
315
  * @param {CozyClient} options.client - CozyClient instance
310
- * @param {io.cozy.accounts} options.account The account content
311
- * @param {io.cozy.konnectors} options.konnector connector manifest content
316
+ * @param {IoCozyAccount} options.account The account content
317
+ * @param {KonnectorManifest} options.konnector konnector manifest content
312
318
  */
313
319
  export const refreshContracts = async ({ client, konnector, account }) => {
314
320
  const biConfig = await createTemporaryToken({
@@ -347,7 +353,7 @@ function convertBIDateToStandardDate(biDate) {
347
353
  * Get the BI temporary token cache from Cozy doctype
348
354
  *
349
355
  * @param {CozyClient} options.client CozyClient instance
350
- * @return {createTemporaryTokenResponse}
356
+ * @return {Promise<createTemporaryTokenResponse>}
351
357
  */
352
358
  async function getBiTemporaryTokenFromCache({ client }) {
353
359
  const queryResult = await client.query(
@@ -358,11 +364,11 @@ async function getBiTemporaryTokenFromCache({ client }) {
358
364
 
359
365
  /**
360
366
  * Check if BI temporary token cache is expired or not
361
- *
362
- * @param {createTemporaryTokenResponse} options.tokenCache
367
+ * @param {createTemporaryTokenResponse} tokenCache
368
+ * @param {Object} biUser the bi user
363
369
  * @return {Boolean}
364
370
  */
365
- export function isCacheExpired({ tokenCache, biUser }) {
371
+ export function isCacheExpired(tokenCache, biUser) {
366
372
  const cacheAge = Date.now() - Number(tokenCache?.timestamp)
367
373
  logger.debug('tokenCache age', cacheAge / 1000 / 60, 'minutes')
368
374
  const MAX_TOKEN_CACHE_AGE = 29 * 60 * 1000
@@ -381,12 +387,12 @@ export function isCacheExpired({ tokenCache, biUser }) {
381
387
 
382
388
  /**
383
389
  * Update the BI temporary token cache from BI itself
384
- *
390
+ * @param {object} options
385
391
  * @param {CozyClient} options.client CozyClient instance
386
- * @param {io.cozy.konnectors} options.konnector
387
- * @param {Array<Number>} options.cozyBankIds List of cozy bank identifiers
392
+ * @param {KonnectorManifest} options.konnector konnector manifest content
393
+ * @param {Array<String>} options.cozyBankIds List of cozy bank identifiers
388
394
  * @param {createTemporaryTokenResponse} options.tokenCache Previous version of BI temporary token cache
389
- * @return {createTemporaryTokenResponse}
395
+ * @return {Promise<createTemporaryTokenResponse>}
390
396
  */
391
397
  async function updateCache({ client, konnector, tokenCache, cozyBankIds }) {
392
398
  const jobResponse = await client.stackClient.jobs.create(
@@ -417,12 +423,12 @@ async function updateCache({ client, konnector, tokenCache, cozyBankIds }) {
417
423
 
418
424
  /**
419
425
  * Gets a temporary token corresponding to the current BI user
420
- *
426
+ * @param {object} options
421
427
  * @param {CozyClient} options.client - CozyClient instance
422
- * @param {io.cozy.konnectors} options.konnector connector manifest content
423
- * @param {io.cozy.accounts} options.account The account content
428
+ * @param {KonnectorManifest} options.konnector konnector manifest content
429
+ * @param {IoCozyAccount} options.account The account content
424
430
  *
425
- * @returns {createTemporaryTokenResponse}
431
+ * @returns {Promise<createTemporaryTokenResponse>}
426
432
  */
427
433
  export const createTemporaryToken = async ({ client, konnector, account }) => {
428
434
  assert(
@@ -437,7 +443,7 @@ export const createTemporaryToken = async ({ client, konnector, account }) => {
437
443
  Q('io.cozy.accounts').getById('bi-aggregator-user')
438
444
  )
439
445
 
440
- if (isCacheExpired({ tokenCache, biUser })) {
446
+ if (isCacheExpired(tokenCache, biUser)) {
441
447
  logger.debug('temporaryToken cache is expired. Updating')
442
448
  tokenCache = await updateCache({
443
449
  client,
@@ -335,27 +335,27 @@ describe('isCacheExpired', () => {
335
335
  it('should not be marked as expired when userId did not change and cache is not old', () => {
336
336
  const tokenCache = { timestamp: Date.now(), userId: 666 }
337
337
  const biUser = { userId: 666 }
338
- expect(isCacheExpired({ tokenCache, biUser })).toBe(false)
338
+ expect(isCacheExpired(tokenCache, biUser)).toBe(false)
339
339
  })
340
340
  it('should be marked as expired when userId did not change and cache is old', () => {
341
341
  const tokenCache = { timestamp: 0, userId: 666 }
342
342
  const biUser = { userId: 666 }
343
- expect(isCacheExpired({ tokenCache, biUser })).toBe(true)
343
+ expect(isCacheExpired(tokenCache, biUser)).toBe(true)
344
344
  })
345
345
  it('should be marked as expired when userId did change and cache is old', () => {
346
346
  const tokenCache = { timestamp: 0, userId: 666 }
347
347
  const biUser = { userId: 667 }
348
- expect(isCacheExpired({ tokenCache, biUser })).toBe(true)
348
+ expect(isCacheExpired(tokenCache, biUser)).toBe(true)
349
349
  })
350
350
  it('should be marked as expired when userId did change and cache is not old', () => {
351
351
  const tokenCache = { timestamp: Date.now(), userId: 666 }
352
352
  const biUser = { userId: 667 }
353
- expect(isCacheExpired({ tokenCache, biUser })).toBe(true)
353
+ expect(isCacheExpired(tokenCache, biUser)).toBe(true)
354
354
  })
355
355
  it('should be marked as expired when cache has no user id', () => {
356
356
  const tokenCache = { timestamp: Date.now() }
357
357
  const biUser = { userId: 666 }
358
- expect(isCacheExpired({ tokenCache, biUser })).toBe(true)
358
+ expect(isCacheExpired(tokenCache, biUser)).toBe(true)
359
359
  })
360
360
  })
361
361
 
package/src/types.js CHANGED
@@ -7,6 +7,12 @@
7
7
  * @property {Array<String>} bankIds - Cozy bank ids corresponding to the current connector
8
8
  * @property {Array<String>} biBankIds - BI bank ids corresponding to the bankIds translated by the connector
9
9
  * @property {Object} biMapping - Association table of any cozy bank id to corresponding bi bank id
10
+ * @property {String?} _rev
11
+ * @property {String} publicKey
12
+ * @property {String} clientId
13
+ * @property {String} mode
14
+ * @property {timestamp?} timestamp
15
+ * @property {String} userId - BI User id used to create the tmp token
10
16
  */
11
17
 
12
18
  /**
@@ -21,3 +27,19 @@
21
27
  * @property {string|null} last_push - Date string: Last successfull push
22
28
  * @property {string|null} next_try - Date string: Date of next synchronization.
23
29
  */
30
+
31
+ /**
32
+ * @typedef {import("../../node_modules/cozy-client/types/types").CozyClientDocument} CozyClientDocument
33
+ */
34
+ /**
35
+ * @typedef KonnectorManifest
36
+ * @property {String} slug
37
+ * @property {object} parameters
38
+ * @property {object} fields
39
+ * @property {object} aggregator
40
+ */
41
+
42
+ /**
43
+ * @typedef {CozyClientDocument} IoCozyAccount
44
+ *
45
+ */