cozy-harvest-lib 9.26.0 → 9.26.1

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.26.1](https://github.com/cozy/cozy-libs/compare/cozy-harvest-lib@9.26.0...cozy-harvest-lib@9.26.1) (2022-08-22)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * RefreshContracts now updates contracts in realtime ([463e716](https://github.com/cozy/cozy-libs/commit/463e7169d78ce633308229257bd262d01842fdc1))
12
+
13
+
14
+
15
+
16
+
6
17
  # [9.26.0](https://github.com/cozy/cozy-libs/compare/cozy-harvest-lib@9.25.0...cozy-harvest-lib@9.26.0) (2022-08-22)
7
18
 
8
19
 
@@ -42,38 +42,13 @@ var BIContractActivationWindow = function BIContractActivationWindow(_ref) {
42
42
  };
43
43
 
44
44
  useEffect(function () {
45
- function refreshContracts() {
46
- return _refreshContracts.apply(this, arguments);
47
- }
48
-
49
- function _refreshContracts() {
50
- _refreshContracts = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
51
- return _regeneratorRuntime.wrap(function _callee$(_context) {
52
- while (1) {
53
- switch (_context.prev = _context.next) {
54
- case 0:
55
- _context.next = 2;
56
- return konnectorPolicy.refreshContracts({
57
- client: client,
58
- account: account,
59
- konnector: konnector
60
- });
61
-
62
- case 2:
63
- setShouldRefreshContracts(false);
64
-
65
- case 3:
66
- case "end":
67
- return _context.stop();
68
- }
69
- }
70
- }, _callee);
71
- }));
72
- return _refreshContracts.apply(this, arguments);
73
- }
74
-
75
45
  if (shouldRefreshContracts) {
76
- refreshContracts();
46
+ setShouldRefreshContracts(false);
47
+ konnectorPolicy.refreshContracts({
48
+ client: client,
49
+ account: account,
50
+ konnector: konnector
51
+ });
77
52
  }
78
53
  }, [account, client, konnectorPolicy, shouldRefreshContracts, konnector]);
79
54
  useEffect(function () {
@@ -82,13 +57,13 @@ var BIContractActivationWindow = function BIContractActivationWindow(_ref) {
82
57
  }
83
58
 
84
59
  function _handleLinkFetch() {
85
- _handleLinkFetch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
60
+ _handleLinkFetch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
86
61
  var result;
87
- return _regeneratorRuntime.wrap(function _callee2$(_context2) {
62
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
88
63
  while (1) {
89
- switch (_context2.prev = _context2.next) {
64
+ switch (_context.prev = _context.next) {
90
65
  case 0:
91
- _context2.next = 2;
66
+ _context.next = 2;
92
67
  return konnectorPolicy.fetchExtraOAuthUrlParams({
93
68
  client: client,
94
69
  account: account,
@@ -96,15 +71,15 @@ var BIContractActivationWindow = function BIContractActivationWindow(_ref) {
96
71
  });
97
72
 
98
73
  case 2:
99
- result = _context2.sent;
74
+ result = _context.sent;
100
75
  setExtraParams(result);
101
76
 
102
77
  case 4:
103
78
  case "end":
104
- return _context2.stop();
79
+ return _context.stop();
105
80
  }
106
81
  }
107
- }, _callee2);
82
+ }, _callee);
108
83
  }));
109
84
  return _handleLinkFetch.apply(this, arguments);
110
85
  }
@@ -422,9 +422,7 @@ export var getCozyBankIds = function getCozyBankIds(_ref10) {
422
422
 
423
423
  export var refreshContracts = /*#__PURE__*/function () {
424
424
  var _ref12 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(_ref11) {
425
- var _account$relationship, _account$relationship2;
426
-
427
- var client, konnector, account, biConfig, code, config, connectionId, _yield$getBIConnectio, contracts, contractsById, currentContractsList, _iterator, _step, currentContract, disabledValue;
425
+ var client, konnector, account, biConfig, code, config, connectionId, _yield$getBIConnectio, contracts, contractsById, _yield$client$query, currentContractsList, _iterator, _step, currentContract, disabledValue, hasChanged;
428
426
 
429
427
  return _regeneratorRuntime.wrap(function _callee5$(_context5) {
430
428
  while (1) {
@@ -451,31 +449,67 @@ export var refreshContracts = /*#__PURE__*/function () {
451
449
  contractsById = contracts.reduce(function (memo, contract) {
452
450
  return _objectSpread(_objectSpread({}, memo), {}, _defineProperty({}, contract.id + '', contract.disabled));
453
451
  }, {});
454
- currentContractsList = (account === null || account === void 0 ? void 0 : (_account$relationship = account.relationships) === null || _account$relationship === void 0 ? void 0 : (_account$relationship2 = _account$relationship.contracts) === null || _account$relationship2 === void 0 ? void 0 : _account$relationship2.data) || [];
452
+ _context5.next = 13;
453
+ return client.query(Q('io.cozy.bank.accounts').where({
454
+ 'relationships.connection.data._id': account._id
455
+ }).include(['owners']).indexFields(['relationships.connection.data._id']));
456
+
457
+ case 13:
458
+ _yield$client$query = _context5.sent;
459
+ currentContractsList = _yield$client$query.data;
455
460
  _iterator = _createForOfIteratorHelper(currentContractsList);
461
+ _context5.prev = 16;
456
462
 
457
- try {
458
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
459
- currentContract = _step.value;
460
- disabledValue = convertBIDateToStandardDate(contractsById[currentContract.metadata.vendorId]);
461
- currentContract.metadata.imported = !disabledValue;
462
- currentContract.metadata.disabledAt = disabledValue;
463
- }
464
- } catch (err) {
465
- _iterator.e(err);
466
- } finally {
467
- _iterator.f();
463
+ _iterator.s();
464
+
465
+ case 18:
466
+ if ((_step = _iterator.n()).done) {
467
+ _context5.next = 29;
468
+ break;
468
469
  }
469
470
 
470
- _context5.next = 16;
471
- return client.save(account);
471
+ currentContract = _step.value;
472
+ disabledValue = convertBIDateToStandardDate(contractsById[currentContract.vendorId]);
473
+ hasChanged = currentContract.metadata.disabledAt !== disabledValue;
472
474
 
473
- case 16:
475
+ if (!hasChanged) {
476
+ _context5.next = 27;
477
+ break;
478
+ }
479
+
480
+ currentContract.metadata.disabledAt = disabledValue;
481
+ currentContract.metadata.imported = !disabledValue; // FIXME bulk save via client.collection().updateAll does not show update of accounts in realtime
482
+
483
+ _context5.next = 27;
484
+ return client.save(currentContract);
485
+
486
+ case 27:
487
+ _context5.next = 18;
488
+ break;
489
+
490
+ case 29:
491
+ _context5.next = 34;
492
+ break;
493
+
494
+ case 31:
495
+ _context5.prev = 31;
496
+ _context5.t0 = _context5["catch"](16);
497
+
498
+ _iterator.e(_context5.t0);
499
+
500
+ case 34:
501
+ _context5.prev = 34;
502
+
503
+ _iterator.f();
504
+
505
+ return _context5.finish(34);
506
+
507
+ case 37:
474
508
  case "end":
475
509
  return _context5.stop();
476
510
  }
477
511
  }
478
- }, _callee5);
512
+ }, _callee5, null, [[16, 31, 34, 37]]);
479
513
  }));
480
514
 
481
515
  return function refreshContracts(_x5) {
@@ -620,7 +654,7 @@ export var createTemporaryToken = /*#__PURE__*/function () {
620
654
  var _ref16 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(_ref15) {
621
655
  var _tokenCache;
622
656
 
623
- var client, konnector, account, tokenCache, cozyBankIds, _yield$client$query, biUser, _tokenCache2, biMapping;
657
+ var client, konnector, account, tokenCache, cozyBankIds, _yield$client$query2, biUser, _tokenCache2, biMapping;
624
658
 
625
659
  return _regeneratorRuntime.wrap(function _callee6$(_context6) {
626
660
  while (1) {
@@ -643,8 +677,8 @@ export var createTemporaryToken = /*#__PURE__*/function () {
643
677
  return client.query(Q('io.cozy.accounts').getById('bi-aggregator-user'));
644
678
 
645
679
  case 8:
646
- _yield$client$query = _context6.sent;
647
- biUser = _yield$client$query.data;
680
+ _yield$client$query2 = _context6.sent;
681
+ biUser = _yield$client$query2.data;
648
682
 
649
683
  if (!isCacheExpired(tokenCache, biUser)) {
650
684
  _context6.next = 15;
@@ -344,7 +344,7 @@ describe('isBiWebViewConnector', function () {
344
344
  });
345
345
  describe('refreshContracts', function () {
346
346
  it('update current contracts with values from BI', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7() {
347
- var client, accountWithContracts;
347
+ var client;
348
348
  return _regeneratorRuntime.wrap(function _callee7$(_context7) {
349
349
  while (1) {
350
350
  switch (_context7.prev = _context7.next) {
@@ -353,7 +353,7 @@ describe('refreshContracts', function () {
353
353
  uri: 'http://testcozy.mycozy.cloud'
354
354
  });
355
355
  client.save = jest.fn();
356
- client.query = jest.fn().mockResolvedValue({
356
+ client.query = jest.fn().mockResolvedValueOnce({
357
357
  data: {
358
358
  mode: 'prod',
359
359
  timestamp: Date.now(),
@@ -362,37 +362,31 @@ describe('refreshContracts', function () {
362
362
  clientId: 'test-client-id',
363
363
  code: 'bi-temporary-access-token-145613'
364
364
  }
365
- });
366
- accountWithContracts = {
367
- _id: 'testaccount',
368
- relationships: {
369
- contracts: {
370
- data: [{
371
- metadata: {
372
- vendorId: '1',
373
- imported: true
374
- }
375
- }, {
376
- metadata: {
377
- vendorId: '2',
378
- imported: false,
379
- disabledAt: '2022-05-24T12:00:00'
380
- }
381
- }, {
382
- metadata: {
383
- vendorId: '3',
384
- imported: true
385
- }
386
- }, {
387
- metadata: {
388
- vendorId: '4',
389
- imported: false,
390
- disabledAt: '2022-05-23T13:00:00'
391
- }
392
- }]
365
+ }).mockResolvedValue({
366
+ data: [{
367
+ vendorId: '1',
368
+ metadata: {
369
+ imported: true
393
370
  }
394
- }
395
- };
371
+ }, {
372
+ vendorId: '2',
373
+ metadata: {
374
+ imported: false,
375
+ disabledAt: '2022-08-05 12:00:00'
376
+ }
377
+ }, {
378
+ vendorId: '3',
379
+ metadata: {
380
+ imported: true
381
+ }
382
+ }, {
383
+ vendorId: '4',
384
+ metadata: {
385
+ imported: false,
386
+ disabledAt: '2022-08-05 12:01:00'
387
+ }
388
+ }]
389
+ });
396
390
  getBIConnectionAccountsList.mockResolvedValue({
397
391
  accounts: [{
398
392
  id: 1,
@@ -408,46 +402,45 @@ describe('refreshContracts', function () {
408
402
  disabled: null
409
403
  }]
410
404
  });
411
- _context7.next = 7;
405
+ _context7.next = 6;
412
406
  return refreshContracts({
413
407
  client: client,
414
408
  konnector: konnector,
415
- account: accountWithContracts
409
+ account: {}
416
410
  });
417
411
 
418
- case 7:
419
- expect(client.save).toHaveBeenCalledWith({
420
- _id: 'testaccount',
421
- relationships: {
422
- contracts: {
423
- data: [{
424
- metadata: {
425
- vendorId: '1',
426
- imported: false,
427
- disabledAt: '2022-05-25T12:00:00'
428
- }
429
- }, {
430
- metadata: {
431
- vendorId: '2',
432
- imported: true
433
- }
434
- }, {
435
- metadata: {
436
- vendorId: '3',
437
- imported: false,
438
- disabledAt: '2022-05-25T12:01:00'
439
- }
440
- }, {
441
- metadata: {
442
- vendorId: '4',
443
- imported: true
444
- }
445
- }]
446
- }
447
- }
448
- });
449
-
450
- case 8:
412
+ case 6:
413
+ expect(client.save).toHaveBeenCalledTimes(4);
414
+ expect(client.save).toHaveBeenNthCalledWith(1, {
415
+ metadata: {
416
+ disabledAt: '2022-05-25T12:00:00',
417
+ imported: false
418
+ },
419
+ vendorId: '1'
420
+ });
421
+ expect(client.save).toHaveBeenNthCalledWith(2, {
422
+ metadata: {
423
+ disabledAt: undefined,
424
+ imported: true
425
+ },
426
+ vendorId: '2'
427
+ });
428
+ expect(client.save).toHaveBeenNthCalledWith(3, {
429
+ metadata: {
430
+ disabledAt: '2022-05-25T12:01:00',
431
+ imported: false
432
+ },
433
+ vendorId: '3'
434
+ });
435
+ expect(client.save).toHaveBeenNthCalledWith(4, {
436
+ metadata: {
437
+ disabledAt: undefined,
438
+ imported: true
439
+ },
440
+ vendorId: '4'
441
+ });
442
+
443
+ case 11:
451
444
  case "end":
452
445
  return _context7.stop();
453
446
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-harvest-lib",
3
- "version": "9.26.0",
3
+ "version": "9.26.1",
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": "c161a9100ebf53d77cc5b7226d124ee954dfc875"
93
+ "gitHead": "cd08838aeee723ac6dba639509cee4acbbd29f25"
94
94
  }
@@ -31,12 +31,9 @@ const BIContractActivationWindow = ({
31
31
  }
32
32
 
33
33
  useEffect(() => {
34
- async function refreshContracts() {
35
- await konnectorPolicy.refreshContracts({ client, account, konnector })
36
- setShouldRefreshContracts(false)
37
- }
38
34
  if (shouldRefreshContracts) {
39
- refreshContracts()
35
+ setShouldRefreshContracts(false)
36
+ konnectorPolicy.refreshContracts({ client, account, konnector })
40
37
  }
41
38
  }, [account, client, konnectorPolicy, shouldRefreshContracts, konnector])
42
39
 
@@ -306,15 +306,24 @@ export const refreshContracts = async ({ client, konnector, account }) => {
306
306
  (memo, contract) => ({ ...memo, [contract.id + '']: contract.disabled }),
307
307
  {}
308
308
  )
309
- const currentContractsList = account?.relationships?.contracts?.data || []
309
+ const { data: currentContractsList } = await client.query(
310
+ Q('io.cozy.bank.accounts')
311
+ .where({ 'relationships.connection.data._id': account._id })
312
+ .include(['owners'])
313
+ .indexFields(['relationships.connection.data._id'])
314
+ )
310
315
  for (const currentContract of currentContractsList) {
311
316
  const disabledValue = convertBIDateToStandardDate(
312
- contractsById[currentContract.metadata.vendorId]
317
+ contractsById[currentContract.vendorId]
313
318
  )
314
- currentContract.metadata.imported = !disabledValue
315
- currentContract.metadata.disabledAt = disabledValue
319
+ const hasChanged = currentContract.metadata.disabledAt !== disabledValue
320
+ if (hasChanged) {
321
+ currentContract.metadata.disabledAt = disabledValue
322
+ currentContract.metadata.imported = !disabledValue
323
+ // FIXME bulk save via client.collection().updateAll does not show update of accounts in realtime
324
+ await client.save(currentContract)
325
+ }
316
326
  }
317
- await client.save(account)
318
327
  }
319
328
 
320
329
  function convertBIDateToStandardDate(biDate) {
@@ -212,41 +212,32 @@ describe('refreshContracts', () => {
212
212
  uri: 'http://testcozy.mycozy.cloud'
213
213
  })
214
214
  client.save = jest.fn()
215
- client.query = jest.fn().mockResolvedValue({
216
- data: {
217
- mode: 'prod',
218
- timestamp: Date.now(),
219
- biMapping: {},
220
- url: 'https://cozy.biapi.pro/2.0',
221
- clientId: 'test-client-id',
222
- code: 'bi-temporary-access-token-145613'
223
- }
224
- })
225
- const accountWithContracts = {
226
- _id: 'testaccount',
227
- relationships: {
228
- contracts: {
229
- data: [
230
- { metadata: { vendorId: '1', imported: true } },
231
- {
232
- metadata: {
233
- vendorId: '2',
234
- imported: false,
235
- disabledAt: '2022-05-24T12:00:00'
236
- }
237
- },
238
- { metadata: { vendorId: '3', imported: true } },
239
- {
240
- metadata: {
241
- vendorId: '4',
242
- imported: false,
243
- disabledAt: '2022-05-23T13:00:00'
244
- }
245
- }
246
- ]
215
+ client.query = jest
216
+ .fn()
217
+ .mockResolvedValueOnce({
218
+ data: {
219
+ mode: 'prod',
220
+ timestamp: Date.now(),
221
+ biMapping: {},
222
+ url: 'https://cozy.biapi.pro/2.0',
223
+ clientId: 'test-client-id',
224
+ code: 'bi-temporary-access-token-145613'
247
225
  }
248
- }
249
- }
226
+ })
227
+ .mockResolvedValue({
228
+ data: [
229
+ { vendorId: '1', metadata: { imported: true } },
230
+ {
231
+ vendorId: '2',
232
+ metadata: { imported: false, disabledAt: '2022-08-05 12:00:00' }
233
+ },
234
+ { vendorId: '3', metadata: { imported: true } },
235
+ {
236
+ vendorId: '4',
237
+ metadata: { imported: false, disabledAt: '2022-08-05 12:01:00' }
238
+ }
239
+ ]
240
+ })
250
241
  getBIConnectionAccountsList.mockResolvedValue({
251
242
  accounts: [
252
243
  { id: 1, disabled: '2022-05-25 12:00:00' },
@@ -256,31 +247,23 @@ describe('refreshContracts', () => {
256
247
  ]
257
248
  })
258
249
 
259
- await refreshContracts({ client, konnector, account: accountWithContracts })
260
- expect(client.save).toHaveBeenCalledWith({
261
- _id: 'testaccount',
262
- relationships: {
263
- contracts: {
264
- data: [
265
- {
266
- metadata: {
267
- vendorId: '1',
268
- imported: false,
269
- disabledAt: '2022-05-25T12:00:00'
270
- }
271
- },
272
- { metadata: { vendorId: '2', imported: true } },
273
- {
274
- metadata: {
275
- vendorId: '3',
276
- imported: false,
277
- disabledAt: '2022-05-25T12:01:00'
278
- }
279
- },
280
- { metadata: { vendorId: '4', imported: true } }
281
- ]
282
- }
283
- }
250
+ await refreshContracts({ client, konnector, account: {} })
251
+ expect(client.save).toHaveBeenCalledTimes(4)
252
+ expect(client.save).toHaveBeenNthCalledWith(1, {
253
+ metadata: { disabledAt: '2022-05-25T12:00:00', imported: false },
254
+ vendorId: '1'
255
+ })
256
+ expect(client.save).toHaveBeenNthCalledWith(2, {
257
+ metadata: { disabledAt: undefined, imported: true },
258
+ vendorId: '2'
259
+ })
260
+ expect(client.save).toHaveBeenNthCalledWith(3, {
261
+ metadata: { disabledAt: '2022-05-25T12:01:00', imported: false },
262
+ vendorId: '3'
263
+ })
264
+ expect(client.save).toHaveBeenNthCalledWith(4, {
265
+ metadata: { disabledAt: undefined, imported: true },
266
+ vendorId: '4'
284
267
  })
285
268
  })
286
269
  })