@janiscommerce/app-tracking-shift 1.6.0 → 1.7.0-beta.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/lib/Shift.js CHANGED
@@ -10,7 +10,8 @@ import {
10
10
  isValidObject,
11
11
  isApiError,
12
12
  isInternetReachable,
13
- isValidString,
13
+ isValidDate,
14
+ parseToISOString,
14
15
  } from './utils/helpers';
15
16
  import {getStaffAuthorizationData} from './utils/storage';
16
17
  import {
@@ -251,17 +252,21 @@ class Shift {
251
252
  * @param {string} workLog.name => Name related to the work log
252
253
  * @param {string} workLog.type => Type related to the work log
253
254
  * @param {number} workLog.suggestedTime => Suggested time related to the work log
255
+ * @param {string|number} metadata.startDate => Start date related to the work log. Accepts an ISO 8601 string (e.g. "2026-03-05T10:00:00.000Z") or milliseconds since epoch (e.g. 1709636400000). Defaults to current date if omitted or invalid.
254
256
  * @throws {Error} error
255
257
  * @returns {Promise<string>} workLogId => ID related to the work log that has just been opened for the user
256
258
  */
257
259
 
258
- async openWorkLog(workLog = {}) {
260
+ async openWorkLog(workLog = {}, {startDate} = {}) {
259
261
  let previousWorkLog = {};
260
- const currentDate = new Date().toISOString();
261
262
 
262
263
  try {
263
264
  Crashlytics.log('openWorkLog:', workLog);
264
265
 
266
+ const currentDate = isValidDate(startDate)
267
+ ? parseToISOString(startDate)
268
+ : new Date().toISOString();
269
+
265
270
  if (!isValidObject(workLog)) return null;
266
271
 
267
272
  if (!this.hasStaffAuthorization) {
@@ -270,7 +275,7 @@ class Shift {
270
275
  delete workLog.endDate;
271
276
 
272
277
  if (this.hasWorkLogInProgress) {
273
- const {startDate, ...currentRest} = this.getCurrentWorkLog();
278
+ const {startDate: workLogStart, ...currentRest} = this.getCurrentWorkLog();
274
279
  previousWorkLog = {
275
280
  ...currentRest,
276
281
  endDate: currentDate,
@@ -448,15 +453,15 @@ class Shift {
448
453
  if (!this.hasPendingData) return null;
449
454
 
450
455
  const storageData = this._getOffLineWorkLogs();
451
- const formatedWorkLogs = ShiftWorklogs.formatForJanis(storageData);
456
+ const formattedWorkLogs = ShiftWorklogs.formatForJanis(storageData);
452
457
 
453
- if (isEmptyArray(formatedWorkLogs)) return null;
458
+ if (isEmptyArray(formattedWorkLogs)) return null;
454
459
 
455
460
  if (this.isClosed()) {
456
461
  await this.reOpen();
457
462
  }
458
463
 
459
- await ShiftWorklogs.batch(formatedWorkLogs);
464
+ await ShiftWorklogs.batch(formattedWorkLogs);
460
465
  OfflineData.deleteAll();
461
466
  return null;
462
467
  } catch (error) {
@@ -24,3 +24,26 @@ export const isNumber = (num) => typeof num === 'number' && !Number.isNaN(Number
24
24
  export const isValidObject = (obj) => isObject(obj) && !!Object.keys(obj).length;
25
25
 
26
26
  export const isValidString = (str) => typeof str === 'string' && str.length > 0;
27
+
28
+ const ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
29
+ export const isIsoStringDate = (str) => ISO_DATE_REGEX.test(str);
30
+
31
+ export const isValidDate = (date = '') => {
32
+ if (isNumber(date) && date > 0) {
33
+ const dateTime = new Date(date).getTime();
34
+ return !Number.isNaN(dateTime);
35
+ }
36
+ if (!isValidString(date)) return false;
37
+
38
+ if (!isIsoStringDate(date)) return false;
39
+
40
+ const dateTime = new Date(date).getTime();
41
+ return !Number.isNaN(dateTime);
42
+ };
43
+
44
+ export const parseToISOString = (date = '') => {
45
+ if (isNumber(date) && date > 0) return new Date(date).toISOString();
46
+ if (!isValidString(date)) return '';
47
+
48
+ return date;
49
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@janiscommerce/app-tracking-shift",
3
- "version": "1.6.0",
3
+ "version": "1.7.0-beta.1",
4
4
  "main": "lib/index.js",
5
5
  "module": "lib/index.js",
6
6
  "exports": {