cozy-harvest-lib 17.2.2 → 17.3.0

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
+ # [17.3.0](https://github.com/cozy/cozy-libs/compare/cozy-harvest-lib@17.2.2...cozy-harvest-lib@17.3.0) (2023-09-28)
7
+
8
+
9
+ ### Features
10
+
11
+ * Add createAccountSilently ([7236d8f](https://github.com/cozy/cozy-libs/commit/7236d8fa40e6e5fa633f17e88b481b08b0369ed6))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [17.2.2](https://github.com/cozy/cozy-libs/compare/cozy-harvest-lib@17.2.1...cozy-harvest-lib@17.2.2) (2023-08-31)
7
18
 
8
19
  **Note:** Version bump only for package cozy-harvest-lib
@@ -56,8 +56,8 @@ var isStepEvent = function isStepEvent(eventName) {
56
56
  *
57
57
  * @param {Object} options
58
58
  * @param {import('cozy-client/types/types').IOCozyAccount} options.account - Existing io.cozy.account or object
59
- * @param {Object} options.cipher - Vault cipher if vault has been unlocked
60
- * @param {String} options.cipher.id - Vault cipher id
59
+ * @param {Object} [options.cipher] - Vault cipher if vault has been unlocked
60
+ * @param {String} [options.cipher.id] - Vault cipher id
61
61
  * @param {ConnectionFlow} options.flow - Current connection flow
62
62
  * @param {CozyClient} options.client - A CozyClient instance
63
63
  * @param {import('cozy-client/types/types').IOCozyKonnector} options.konnector - Konnector to which the account is linked
@@ -752,6 +752,114 @@ export var ConnectionFlow = /*#__PURE__*/function () {
752
752
 
753
753
  return handleFormSubmit;
754
754
  }()
755
+ /**
756
+ * - Creates io.cozy.accounts
757
+ * - Saves account
758
+ * - Ensures trigger is existing for account
759
+ */
760
+
761
+ }, {
762
+ key: "createAccountSilently",
763
+ value: function () {
764
+ var _createAccountSilently = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(options) {
765
+ var konnector, userCredentials, t, account, trigger, client, konnectorPolicy;
766
+ return _regeneratorRuntime.wrap(function _callee7$(_context7) {
767
+ while (1) {
768
+ switch (_context7.prev = _context7.next) {
769
+ case 0:
770
+ konnector = options.konnector, userCredentials = options.userCredentials, t = options.t;
771
+ _context7.prev = 1;
772
+ account = options.account, trigger = options.trigger;
773
+ client = this.client;
774
+ this.setState({
775
+ status: CREATING_ACCOUNT,
776
+ firstRun: true // when the user submits new authentication information, this is considered a first run
777
+
778
+ });
779
+ this.trigger = trigger;
780
+ this.account = account;
781
+ this.t = t;
782
+ assert(client, 'No client');
783
+ konnectorPolicy = this.getKonnectorPolicy(); // @ts-ignore
784
+
785
+ logger.log("ConnectionFlow: Handling submit, with konnector policy ".concat(konnectorPolicy.name)); // @ts-ignore
786
+
787
+ logger.info('ConnectionFlow: Bypassing cipher creation because of vault may be unlocked');
788
+
789
+ if (!konnectorPolicy.needsAccountAndTriggerCreation) {
790
+ _context7.next = 24;
791
+ break;
792
+ }
793
+
794
+ // @ts-ignore
795
+ logger.debug('ConnectionFlow: Creating/updating account...', account);
796
+ _context7.next = 16;
797
+ return createOrUpdateAccount({
798
+ account: account,
799
+ client: client,
800
+ flow: this,
801
+ konnector: konnector,
802
+ konnectorPolicy: konnectorPolicy,
803
+ userCredentials: userCredentials
804
+ });
805
+
806
+ case 16:
807
+ account = _context7.sent;
808
+ this.account = account; // @ts-ignore
809
+
810
+ logger.info("ConnectionFlow: Saved account ".concat(account._id)); // @ts-ignore
811
+
812
+ logger.debug('ConnectionFlow: Ensuring trigger...');
813
+ _context7.next = 22;
814
+ return ensureTrigger(client, {
815
+ trigger: trigger,
816
+ account: account,
817
+ konnector: konnector,
818
+ t: t
819
+ });
820
+
821
+ case 22:
822
+ this.trigger = _context7.sent;
823
+ // @ts-ignore
824
+ logger.info("Trigger is ".concat(this.trigger._id));
825
+
826
+ case 24:
827
+ this.setState({
828
+ accountError: null
829
+ });
830
+ _context7.next = 34;
831
+ break;
832
+
833
+ case 27:
834
+ _context7.prev = 27;
835
+ _context7.t0 = _context7["catch"](1);
836
+ // @ts-ignore
837
+ logger.error(_context7.t0);
838
+ this.setState({
839
+ accountError: _context7.t0
840
+ });
841
+ this.triggerEvent(ERROR_EVENT, _context7.t0);
842
+ sentryHub.withScope(function (scope) {
843
+ scope.setTag('konnector', konnector.slug); // Capture the original exception instead of the user one
844
+
845
+ sentryHub.captureException(_context7.t0.original || _context7.t0);
846
+ });
847
+ throw _context7.t0;
848
+
849
+ case 34:
850
+ case "end":
851
+ return _context7.stop();
852
+ }
853
+ }
854
+ }, _callee7, this, [[1, 27]]);
855
+ }));
856
+
857
+ function createAccountSilently(_x7) {
858
+ return _createAccountSilently.apply(this, arguments);
859
+ }
860
+
861
+ return createAccountSilently;
862
+ }()
755
863
  /**
756
864
  * Ensures a trigger is created and launched
757
865
  *
@@ -767,16 +875,16 @@ export var ConnectionFlow = /*#__PURE__*/function () {
767
875
  }, {
768
876
  key: "ensureTriggerAndLaunch",
769
877
  value: function () {
770
- var _ensureTriggerAndLaunch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(client, _ref3) {
878
+ var _ensureTriggerAndLaunch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(client, _ref3) {
771
879
  var account, konnector, trigger, t;
772
- return _regeneratorRuntime.wrap(function _callee7$(_context7) {
880
+ return _regeneratorRuntime.wrap(function _callee8$(_context8) {
773
881
  while (1) {
774
- switch (_context7.prev = _context7.next) {
882
+ switch (_context8.prev = _context8.next) {
775
883
  case 0:
776
884
  account = _ref3.account, konnector = _ref3.konnector, trigger = _ref3.trigger, t = _ref3.t;
777
885
  // @ts-ignore
778
886
  logger.debug('ConnectionFlow: Ensuring trigger...');
779
- _context7.next = 4;
887
+ _context8.next = 4;
780
888
  return ensureTrigger(client, {
781
889
  trigger: trigger,
782
890
  account: account,
@@ -785,10 +893,10 @@ export var ConnectionFlow = /*#__PURE__*/function () {
785
893
  });
786
894
 
787
895
  case 4:
788
- this.trigger = _context7.sent;
896
+ this.trigger = _context8.sent;
789
897
  // @ts-ignore
790
898
  logger.info("Trigger is ".concat(this.trigger._id));
791
- _context7.next = 8;
899
+ _context8.next = 8;
792
900
  return this.ensureDefaultFolderPathInAccount(client, {
793
901
  trigger: this.trigger,
794
902
  account: account,
@@ -798,18 +906,18 @@ export var ConnectionFlow = /*#__PURE__*/function () {
798
906
  case 8:
799
907
  // @ts-ignore
800
908
  this.emit(UPDATE_EVENT);
801
- _context7.next = 11;
909
+ _context8.next = 11;
802
910
  return this.launch();
803
911
 
804
912
  case 11:
805
913
  case "end":
806
- return _context7.stop();
914
+ return _context8.stop();
807
915
  }
808
916
  }
809
- }, _callee7, this);
917
+ }, _callee8, this);
810
918
  }));
811
919
 
812
- function ensureTriggerAndLaunch(_x7, _x8) {
920
+ function ensureTriggerAndLaunch(_x8, _x9) {
813
921
  return _ensureTriggerAndLaunch.apply(this, arguments);
814
922
  }
815
923
 
@@ -890,27 +998,27 @@ export var ConnectionFlow = /*#__PURE__*/function () {
890
998
  }, {
891
999
  key: "refetchTrigger",
892
1000
  value: function () {
893
- var _refetchTrigger = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8() {
1001
+ var _refetchTrigger = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9() {
894
1002
  var trigger;
895
- return _regeneratorRuntime.wrap(function _callee8$(_context8) {
1003
+ return _regeneratorRuntime.wrap(function _callee9$(_context9) {
896
1004
  while (1) {
897
- switch (_context8.prev = _context8.next) {
1005
+ switch (_context9.prev = _context9.next) {
898
1006
  case 0:
899
1007
  if (this.trigger) {
900
- _context8.next = 2;
1008
+ _context9.next = 2;
901
1009
  break;
902
1010
  }
903
1011
 
904
- return _context8.abrupt("return", null);
1012
+ return _context9.abrupt("return", null);
905
1013
 
906
1014
  case 2:
907
1015
  // @ts-ignore
908
1016
  logger.debug("ConnectionFlow: Refetching trigger ".concat(this.trigger._id));
909
- _context8.next = 5;
1017
+ _context9.next = 5;
910
1018
  return fetchTrigger(this.client, this.trigger._id);
911
1019
 
912
1020
  case 5:
913
- trigger = _context8.sent;
1021
+ trigger = _context9.sent;
914
1022
  // @ts-ignore
915
1023
  logger.debug("Refetched trigger", trigger);
916
1024
  this.trigger = trigger;
@@ -920,10 +1028,10 @@ export var ConnectionFlow = /*#__PURE__*/function () {
920
1028
 
921
1029
  case 10:
922
1030
  case "end":
923
- return _context8.stop();
1031
+ return _context9.stop();
924
1032
  }
925
1033
  }
926
- }, _callee8, this);
1034
+ }, _callee9, this);
927
1035
  }));
928
1036
 
929
1037
  function refetchTrigger() {
@@ -950,69 +1058,69 @@ export var ConnectionFlow = /*#__PURE__*/function () {
950
1058
  }, {
951
1059
  key: "ensureDefaultFolderPathInAccount",
952
1060
  value: function () {
953
- var _ensureDefaultFolderPathInAccount = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9(client, _ref4) {
1061
+ var _ensureDefaultFolderPathInAccount = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10(client, _ref4) {
954
1062
  var _trigger$message2;
955
1063
 
956
1064
  var trigger, account, konnector, folderId, folder, result, savedAccount;
957
- return _regeneratorRuntime.wrap(function _callee9$(_context9) {
1065
+ return _regeneratorRuntime.wrap(function _callee10$(_context10) {
958
1066
  while (1) {
959
- switch (_context9.prev = _context9.next) {
1067
+ switch (_context10.prev = _context10.next) {
960
1068
  case 0:
961
1069
  trigger = _ref4.trigger, account = _ref4.account, konnector = _ref4.konnector;
962
1070
  // @ts-ignore the message property does not exist in TriggersDoctype
963
1071
  folderId = trigger === null || trigger === void 0 ? void 0 : (_trigger$message2 = trigger.message) === null || _trigger$message2 === void 0 ? void 0 : _trigger$message2.folder_to_save;
964
1072
 
965
1073
  if (folderId) {
966
- _context9.next = 4;
1074
+ _context10.next = 4;
967
1075
  break;
968
1076
  }
969
1077
 
970
- return _context9.abrupt("return", account);
1078
+ return _context10.abrupt("return", account);
971
1079
 
972
1080
  case 4:
973
- _context9.prev = 4;
974
- _context9.next = 7;
1081
+ _context10.prev = 4;
1082
+ _context10.next = 7;
975
1083
  return client.query(Q('io.cozy.files').getById(folderId));
976
1084
 
977
1085
  case 7:
978
- result = _context9.sent;
1086
+ result = _context10.sent;
979
1087
  folder = result.data;
980
1088
 
981
1089
  if (!(folder.path !== account.defaultFolderPath)) {
982
- _context9.next = 15;
1090
+ _context10.next = 15;
983
1091
  break;
984
1092
  }
985
1093
 
986
1094
  account.defaultFolderPath = folder.path;
987
- _context9.next = 13;
1095
+ _context10.next = 13;
988
1096
  return _saveAccount(client, konnector, account);
989
1097
 
990
1098
  case 13:
991
- savedAccount = _context9.sent;
992
- return _context9.abrupt("return", savedAccount);
1099
+ savedAccount = _context10.sent;
1100
+ return _context10.abrupt("return", savedAccount);
993
1101
 
994
1102
  case 15:
995
- _context9.next = 20;
1103
+ _context10.next = 20;
996
1104
  break;
997
1105
 
998
1106
  case 17:
999
- _context9.prev = 17;
1000
- _context9.t0 = _context9["catch"](4);
1107
+ _context10.prev = 17;
1108
+ _context10.t0 = _context10["catch"](4);
1001
1109
  // @ts-ignore
1002
- logger.warn("ConnectionFlow.ensureDefaultFolderPath: folder ".concat(folderId, " does not exist. Could not ensure defaultFolderPath. ").concat(_context9.t0.message));
1110
+ logger.warn("ConnectionFlow.ensureDefaultFolderPath: folder ".concat(folderId, " does not exist. Could not ensure defaultFolderPath. ").concat(_context10.t0.message));
1003
1111
 
1004
1112
  case 20:
1005
- return _context9.abrupt("return", account);
1113
+ return _context10.abrupt("return", account);
1006
1114
 
1007
1115
  case 21:
1008
1116
  case "end":
1009
- return _context9.stop();
1117
+ return _context10.stop();
1010
1118
  }
1011
1119
  }
1012
- }, _callee9, null, [[4, 17]]);
1120
+ }, _callee10, null, [[4, 17]]);
1013
1121
  }));
1014
1122
 
1015
- function ensureDefaultFolderPathInAccount(_x9, _x10) {
1123
+ function ensureDefaultFolderPathInAccount(_x10, _x11) {
1016
1124
  return _ensureDefaultFolderPathInAccount.apply(this, arguments);
1017
1125
  }
1018
1126
 
@@ -1041,7 +1149,7 @@ export var ConnectionFlow = /*#__PURE__*/function () {
1041
1149
  }, {
1042
1150
  key: "launch",
1043
1151
  value: function () {
1044
- var _launch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10() {
1152
+ var _launch = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee11() {
1045
1153
  var _this3 = this;
1046
1154
 
1047
1155
  var _ref5,
@@ -1051,13 +1159,13 @@ export var ConnectionFlow = /*#__PURE__*/function () {
1051
1159
  error,
1052
1160
  konnectorPolicy,
1053
1161
  computedAutoSuccessTimer,
1054
- _args10 = arguments;
1162
+ _args11 = arguments;
1055
1163
 
1056
- return _regeneratorRuntime.wrap(function _callee10$(_context10) {
1164
+ return _regeneratorRuntime.wrap(function _callee11$(_context11) {
1057
1165
  while (1) {
1058
- switch (_context10.prev = _context10.next) {
1166
+ switch (_context11.prev = _context11.next) {
1059
1167
  case 0:
1060
- _ref5 = _args10.length > 0 && _args10[0] !== undefined ? _args10[0] : {}, _ref5$autoSuccessTime = _ref5.autoSuccessTimer, autoSuccessTimer = _ref5$autoSuccessTime === void 0 ? true : _ref5$autoSuccessTime;
1168
+ _ref5 = _args11.length > 0 && _args11[0] !== undefined ? _args11[0] : {}, _ref5$autoSuccessTime = _ref5.autoSuccessTimer, autoSuccessTimer = _ref5$autoSuccessTime === void 0 ? true : _ref5$autoSuccessTime;
1061
1169
  _this$getState = this.getState(), error = _this$getState.error;
1062
1170
 
1063
1171
  if (this.isUserActionError(error)) {
@@ -1078,15 +1186,15 @@ export var ConnectionFlow = /*#__PURE__*/function () {
1078
1186
  });
1079
1187
 
1080
1188
  if (!this.trigger) {
1081
- _context10.next = 11;
1189
+ _context11.next = 11;
1082
1190
  break;
1083
1191
  }
1084
1192
 
1085
- _context10.next = 10;
1193
+ _context11.next = 10;
1086
1194
  return prepareTriggerAccount(this.client, this.trigger);
1087
1195
 
1088
1196
  case 10:
1089
- this.account = _context10.sent;
1197
+ this.account = _context11.sent;
1090
1198
 
1091
1199
  case 11:
1092
1200
  if (konnectorPolicy.onLaunch) {
@@ -1107,31 +1215,31 @@ export var ConnectionFlow = /*#__PURE__*/function () {
1107
1215
  }
1108
1216
 
1109
1217
  if (konnectorPolicy.needsTriggerLaunch) {
1110
- _context10.next = 14;
1218
+ _context11.next = 14;
1111
1219
  break;
1112
1220
  }
1113
1221
 
1114
- return _context10.abrupt("return");
1222
+ return _context11.abrupt("return");
1115
1223
 
1116
1224
  case 14:
1117
1225
  this.realtime.subscribe('updated', ACCOUNTS_DOCTYPE, this.account._id, this.handleAccountUpdated); // @ts-ignore
1118
1226
 
1119
1227
  logger.info("ConnectionFlow: Subscribed to ".concat(ACCOUNTS_DOCTYPE, ":").concat(this.account._id));
1120
- _context10.next = 18;
1228
+ _context11.next = 18;
1121
1229
  return launchTrigger(this.client, this.trigger);
1122
1230
 
1123
1231
  case 18:
1124
- this.job = _context10.sent;
1232
+ this.job = _context11.sent;
1125
1233
  this.watchJob({
1126
1234
  autoSuccessTimer: computedAutoSuccessTimer
1127
1235
  });
1128
1236
 
1129
1237
  case 20:
1130
1238
  case "end":
1131
- return _context10.stop();
1239
+ return _context11.stop();
1132
1240
  }
1133
1241
  }
1134
- }, _callee10, this);
1242
+ }, _callee11, this);
1135
1243
  }));
1136
1244
 
1137
1245
  function launch() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-harvest-lib",
3
- "version": "17.2.2",
3
+ "version": "17.3.0",
4
4
  "description": "Provides logic, modules and components for Cozy's harvest applications.",
5
5
  "main": "dist/index.js",
6
6
  "author": "Cozy",
@@ -112,5 +112,5 @@
112
112
  "react-router-dom": ">=4.3.1"
113
113
  },
114
114
  "sideEffects": false,
115
- "gitHead": "003501e10490f2c107c8a9f081bc1be30851dc37"
115
+ "gitHead": "613e1fad6cdd407f406bc33b8d0683a3af7d2f7a"
116
116
  }
@@ -72,8 +72,8 @@ const isStepEvent = eventName => stepEvents.includes(eventName)
72
72
  *
73
73
  * @param {Object} options
74
74
  * @param {import('cozy-client/types/types').IOCozyAccount} options.account - Existing io.cozy.account or object
75
- * @param {Object} options.cipher - Vault cipher if vault has been unlocked
76
- * @param {String} options.cipher.id - Vault cipher id
75
+ * @param {Object} [options.cipher] - Vault cipher if vault has been unlocked
76
+ * @param {String} [options.cipher.id] - Vault cipher id
77
77
  * @param {ConnectionFlow} options.flow - Current connection flow
78
78
  * @param {CozyClient} options.client - A CozyClient instance
79
79
  * @param {import('cozy-client/types/types').IOCozyKonnector} options.konnector - Konnector to which the account is linked
@@ -544,6 +544,84 @@ export class ConnectionFlow {
544
544
  }
545
545
  }
546
546
 
547
+ /**
548
+ * - Creates io.cozy.accounts
549
+ * - Saves account
550
+ * - Ensures trigger is existing for account
551
+ */
552
+ async createAccountSilently(options) {
553
+ const { konnector, userCredentials, t } = options
554
+ try {
555
+ let { account, trigger } = options
556
+
557
+ const client = this.client
558
+
559
+ this.setState({
560
+ status: CREATING_ACCOUNT,
561
+ firstRun: true // when the user submits new authentication information, this is considered a first run
562
+ })
563
+ this.trigger = trigger
564
+ this.account = account
565
+
566
+ this.t = t
567
+
568
+ assert(client, 'No client')
569
+ const konnectorPolicy = this.getKonnectorPolicy()
570
+ // @ts-ignore
571
+ logger.log(
572
+ `ConnectionFlow: Handling submit, with konnector policy ${konnectorPolicy.name}`
573
+ )
574
+
575
+ // @ts-ignore
576
+ logger.info(
577
+ 'ConnectionFlow: Bypassing cipher creation because of vault may be unlocked'
578
+ )
579
+
580
+ if (konnectorPolicy.needsAccountAndTriggerCreation) {
581
+ // @ts-ignore
582
+ logger.debug('ConnectionFlow: Creating/updating account...', account)
583
+ account = await createOrUpdateAccount({
584
+ account,
585
+ client,
586
+ flow: this,
587
+ konnector,
588
+ konnectorPolicy,
589
+ userCredentials
590
+ })
591
+
592
+ this.account = account
593
+
594
+ // @ts-ignore
595
+ logger.info(`ConnectionFlow: Saved account ${account._id}`)
596
+
597
+ // @ts-ignore
598
+ logger.debug('ConnectionFlow: Ensuring trigger...')
599
+ this.trigger = await ensureTrigger(client, {
600
+ trigger,
601
+ account,
602
+ konnector,
603
+ t
604
+ })
605
+ // @ts-ignore
606
+ logger.info(`Trigger is ${this.trigger._id}`)
607
+ }
608
+
609
+ this.setState({ accountError: null })
610
+ } catch (e) {
611
+ // @ts-ignore
612
+ logger.error(e)
613
+ this.setState({ accountError: e })
614
+ this.triggerEvent(ERROR_EVENT, e)
615
+ sentryHub.withScope(scope => {
616
+ scope.setTag('konnector', konnector.slug)
617
+
618
+ // Capture the original exception instead of the user one
619
+ sentryHub.captureException(e.original || e)
620
+ })
621
+ throw e
622
+ }
623
+ }
624
+
547
625
  /**
548
626
  * Ensures a trigger is created and launched
549
627
  *