cozy-client 27.24.0 → 27.25.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.
@@ -2537,7 +2537,7 @@ var CozyClient = /*#__PURE__*/function () {
2537
2537
  CozyClient.hooks = CozyClient.hooks || {};
2538
2538
  CozyClient.fetchPolicies = _policies.default; //COZY_CLIENT_VERSION_PACKAGE in replaced by babel. See babel config
2539
2539
 
2540
- CozyClient.version = "27.24.0";
2540
+ CozyClient.version = "27.25.0";
2541
2541
 
2542
2542
  _microee.default.mixin(CozyClient);
2543
2543
 
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.sendMeasureToDACC = exports.checkMeasureParams = void 0;
9
+
10
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
+
12
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
+
14
+ var _cozyLogger = _interopRequireDefault(require("cozy-logger"));
15
+
16
+ var _types = require("../types");
17
+
18
+ /**
19
+ * Throw an errror if a DACC parameter is incorrect.
20
+ *
21
+ * @param { DACCMeasure} measure - The DACC measure
22
+ */
23
+ var checkMeasureParams = function checkMeasureParams(measure) {
24
+ var createdBy = measure.createdBy,
25
+ measureName = measure.measureName,
26
+ startDate = measure.startDate,
27
+ value = measure.value,
28
+ group1 = measure.group1,
29
+ group2 = measure.group2,
30
+ group3 = measure.group3;
31
+
32
+ if (!createdBy || typeof createdBy !== 'string') {
33
+ throw new Error('Missing or wrong type parameter: createdBy');
34
+ }
35
+
36
+ if (!measureName || typeof measureName !== 'string') {
37
+ throw new Error('Missing or wrong type parameter: measureName');
38
+ }
39
+
40
+ if (!startDate) {
41
+ throw new Error('Missing parameter: startDate');
42
+ }
43
+
44
+ var parsedDate = new Date(Date.parse(startDate));
45
+
46
+ if (!parsedDate.toISOString().startsWith(startDate)) {
47
+ throw new Error('Date should be in YYYY-MM-DD format');
48
+ }
49
+
50
+ if (!value || typeof value !== 'number') {
51
+ throw new Error('Missing or wrong type parameter: value');
52
+ }
53
+
54
+ if (group1 && (typeof group1 !== 'object' || Object.keys(group1).length === 0) || group2 && (typeof group2 !== 'object' || Object.keys(group2).length === 0) || group3 && (typeof group3 !== 'object' || Object.keys(group3).length === 0)) {
55
+ throw new Error('Groups should be key-value objects');
56
+ }
57
+
58
+ if (group3 && (!group2 || !group1) || group2 && !group1) {
59
+ throw new Error('Group order must be respected');
60
+ }
61
+ };
62
+ /**
63
+ * Send measures to a DACC through a remote doctype
64
+ *
65
+ * @param {object} client - The CozyClient instance
66
+ * @param {string} remoteDoctype - The remote doctype to use
67
+ * @param {DACCMeasure} measure - The DACC measure
68
+ */
69
+
70
+
71
+ exports.checkMeasureParams = checkMeasureParams;
72
+
73
+ var sendMeasureToDACC = /*#__PURE__*/function () {
74
+ var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(client, remoteDoctype, measure) {
75
+ return _regenerator.default.wrap(function _callee$(_context) {
76
+ while (1) {
77
+ switch (_context.prev = _context.next) {
78
+ case 0:
79
+ _context.prev = 0;
80
+ checkMeasureParams(measure);
81
+ _context.next = 4;
82
+ return client.getStackClient().fetchJSON('POST', "/remote/".concat(remoteDoctype), {
83
+ data: JSON.stringify(measure)
84
+ });
85
+
86
+ case 4:
87
+ _context.next = 10;
88
+ break;
89
+
90
+ case 6:
91
+ _context.prev = 6;
92
+ _context.t0 = _context["catch"](0);
93
+ (0, _cozyLogger.default)('error', "Error while sending measure to remote doctype: ".concat(_context.t0.message));
94
+ throw _context.t0;
95
+
96
+ case 10:
97
+ case "end":
98
+ return _context.stop();
99
+ }
100
+ }
101
+ }, _callee, null, [[0, 6]]);
102
+ }));
103
+
104
+ return function sendMeasureToDACC(_x, _x2, _x3) {
105
+ return _ref.apply(this, arguments);
106
+ };
107
+ }();
108
+
109
+ exports.sendMeasureToDACC = sendMeasureToDACC;
@@ -0,0 +1,99 @@
1
+ import { checkMeasureParams } from './dacc'
2
+
3
+ describe('dacc', () => {
4
+ const measure = {
5
+ value: 42,
6
+ measureName: 'theanswer',
7
+ startDate: '2022-01-01',
8
+ createdBy: 'DeepThought'
9
+ }
10
+
11
+ it('should not throw when all params are corrects', () => {
12
+ expect(() => checkMeasureParams(measure)).not.toThrow()
13
+ })
14
+
15
+ it('should not throw when all params are corrects with groups', () => {
16
+ const measureWithGroups = {
17
+ ...measure,
18
+ group1: { a: 1 },
19
+ group2: { b: 1 },
20
+ group3: { c: 1 }
21
+ }
22
+ expect(() => checkMeasureParams(measureWithGroups)).not.toThrow()
23
+ })
24
+
25
+ it('should throw an error when createdBy is missing or malformed', () => {
26
+ expect(() => checkMeasureParams({ ...measure, createdBy: null })).toThrow()
27
+ expect(() => checkMeasureParams({ ...measure, createdBy: 123 })).toThrow()
28
+ })
29
+
30
+ it('should throw an error when value is missing or malformed', () => {
31
+ expect(() => checkMeasureParams({ ...measure, value: null })).toThrow()
32
+ expect(() =>
33
+ checkMeasureParams({ ...measure, value: 'notacorrectvalue' })
34
+ ).toThrow()
35
+ })
36
+
37
+ it('should throw an error when measureName is missing or malformed', () => {
38
+ expect(() =>
39
+ checkMeasureParams({ ...measure, measureName: null })
40
+ ).toThrow()
41
+ expect(() => checkMeasureParams({ ...measure, measureName: 10 })).toThrow()
42
+ })
43
+
44
+ it('should throw an error when startDate is missing or malformed', () => {
45
+ expect(() => checkMeasureParams({ ...measure, startDate: null })).toThrow()
46
+ expect(() => checkMeasureParams({ ...measure, startDate: 123 })).toThrow()
47
+ expect(() =>
48
+ checkMeasureParams({ ...measure, startDate: '01 January 2012' })
49
+ ).toThrow()
50
+ expect(() =>
51
+ checkMeasureParams({ ...measure, startDate: '2022:01:01' })
52
+ ).toThrow()
53
+ })
54
+
55
+ it('should throw an error when groups are not objects', () => {
56
+ expect(() => checkMeasureParams({ ...measure, group1: '123' })).toThrow()
57
+ expect(() =>
58
+ checkMeasureParams({ ...measure, group1: { a: 1 }, group2: '456' })
59
+ ).toThrow()
60
+ expect(() =>
61
+ checkMeasureParams({
62
+ ...measure,
63
+ group1: { a: 1 },
64
+ group2: { b: 1 },
65
+ group3: '789'
66
+ })
67
+ ).toThrow()
68
+ })
69
+
70
+ it('should throw an error when groups are empty objects', () => {
71
+ expect(() => checkMeasureParams({ ...measure, group1: {} })).toThrow()
72
+ expect(() =>
73
+ checkMeasureParams({ ...measure, group1: { a: 1 }, group2: {} })
74
+ ).toThrow()
75
+ expect(() =>
76
+ checkMeasureParams({
77
+ ...measure,
78
+ group1: { a: 1 },
79
+ group2: { b: 1 },
80
+ group3: {}
81
+ })
82
+ ).toThrow()
83
+ })
84
+
85
+ it('should throw an error when groups order is not respected', () => {
86
+ expect(() => checkMeasureParams({ ...measure, group2: { b: 1 } })).toThrow()
87
+ expect(() => checkMeasureParams({ ...measure, group3: { c: 1 } })).toThrow()
88
+ expect(() =>
89
+ checkMeasureParams({
90
+ ...measure,
91
+ group1: { a: 1 },
92
+ group3: { c: 1 }
93
+ })
94
+ ).toThrow()
95
+ expect(() =>
96
+ checkMeasureParams({ ...measure, group2: { b: 1 }, group3: { c: 1 } })
97
+ ).toThrow()
98
+ })
99
+ })
package/dist/types.js CHANGED
@@ -308,6 +308,19 @@ var _dsl = require("./queries/dsl");
308
308
  * @typedef {IdentityLabel|FamilyLabel|WorkStudyLabels|HealthLabels|HomeLabels|TransportLabels|FinanceLabels|InvoiceLabels|ActivityLabels} ItemsLabels
309
309
  */
310
310
 
311
+ /**
312
+ * @typedef {object} DACCMeasure
313
+ * See https://github.com/cozy/DACC
314
+ *
315
+ * @property {string} measureName - It must match an existing measure name on the DACC server
316
+ * @property {string} startDate - Start of the aggregation period. Should be in YYYY-MM-DD format
317
+ * @property {number} value - The measured value on the aggregation period
318
+ * @property {string} createdBy - The slug of the app creating the measure
319
+ * @property {object} group1 - Should be a {key: value} where the key is set in the measure definition.
320
+ * @property {object} group2 - Should be a {key: value} where the key is set in the measure definition.
321
+ * @property {object} group3 - Should be a {key: value} where the key is set in the measure definition.
322
+ */
323
+
311
324
  /**
312
325
  * Receives the URL to present to the user as a parameter, and should return a promise that resolves with the URL the user was redirected to after accepting the permissions.
313
326
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-client",
3
- "version": "27.24.0",
3
+ "version": "27.25.0",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.node.js",
6
6
  "browser": "dist/index.js",
@@ -71,5 +71,5 @@
71
71
  "react-native-ios11-devicecheck": "https://github.com/cozy/react-native-devicecheck#app-attest-v0.1"
72
72
  },
73
73
  "sideEffects": false,
74
- "gitHead": "1b797059e18e7acfca9666ae8292dabe05a4eaff"
74
+ "gitHead": "5fdb483852d6bd711dac46ff00734b853d118741"
75
75
  }