@janiscommerce/app-tracking-shift 1.5.0 → 1.7.0-beta.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/lib/Shift.js CHANGED
@@ -10,6 +10,7 @@ import {
10
10
  isValidObject,
11
11
  isApiError,
12
12
  isInternetReachable,
13
+ isValidString,
13
14
  } from './utils/helpers';
14
15
  import {getStaffAuthorizationData} from './utils/storage';
15
16
  import {
@@ -95,6 +96,10 @@ class Shift {
95
96
  return !!Storage.get(CURRENT_WORKLOG_ID);
96
97
  }
97
98
 
99
+ get isOpen() {
100
+ return this.status === 'opened';
101
+ }
102
+
98
103
  /**
99
104
  * Get if the shift is paused
100
105
  * @returns {boolean} true if the shift is paused, false otherwise
@@ -130,7 +135,7 @@ class Shift {
130
135
  * @returns {Promise<string>} shiftId => ID related to the shift that has just been opened for the user
131
136
  */
132
137
 
133
- async open() {
138
+ async open({warehouseId = ''} = {}) {
134
139
  try {
135
140
  Crashlytics.log('openShift:');
136
141
 
@@ -138,7 +143,7 @@ class Shift {
138
143
  this._throwAuthorizationError();
139
144
  }
140
145
 
141
- const {result: shift} = await StaffService.openShift();
146
+ const {result: shift} = await StaffService.openShift({warehouseId});
142
147
  const {id: shiftId = ''} = shift || {};
143
148
  const shiftData = await this.getUserOpenShift({id: shiftId});
144
149
 
@@ -157,6 +162,48 @@ class Shift {
157
162
  }
158
163
  }
159
164
 
165
+ /**
166
+ * Update the shift data in the staff MS and record this change in shift database.
167
+ * @param {Object} params
168
+ * @param {string} params.warehouseId => Warehouse ID related to the shift
169
+ * @throws {Error} error
170
+ * @returns {Promise<string>} shiftId => ID related to the shift that has just been updated for the user
171
+ */
172
+
173
+ async update({warehouseId = ''} = {}) {
174
+ try {
175
+ Crashlytics.log('[updateShift]:');
176
+
177
+ if (!this.hasStaffAuthorization) {
178
+ this._throwAuthorizationError();
179
+ }
180
+
181
+ if (this.isClosed()) return null;
182
+
183
+ // Avoid unnecessary updates if warehouseId hasn't changed
184
+ if (this.data?.warehouseId === warehouseId) {
185
+ return this.id;
186
+ }
187
+
188
+ const {result: shift} = await StaffService.updateShift({warehouseId});
189
+ const {id: shiftId = ''} = shift || {};
190
+
191
+ this.data = {
192
+ ...this.data,
193
+ warehouseId,
194
+ };
195
+
196
+ return shiftId;
197
+ } catch (error) {
198
+ const parsedError = errorParser(error);
199
+ Crashlytics.recordError(
200
+ parsedError,
201
+ '[updateShift]: An error occurred while trying to update the shift'
202
+ );
203
+ return Promise.reject(parsedError);
204
+ }
205
+ }
206
+
160
207
  /**
161
208
  * Finish a work shift in the staff MS and record this event in the time tracking database.
162
209
  * @param {Object} params
@@ -506,7 +553,8 @@ class Shift {
506
553
  throw new Error('The deadline for ending the shift has been exceeded');
507
554
  }
508
555
 
509
- await StaffService.openShift();
556
+ await StaffService.openShift({warehouseId: this.data?.warehouseId});
557
+ this.status = 'opened';
510
558
  this._extendShiftClosingDate();
511
559
 
512
560
  return null;
@@ -1,4 +1,5 @@
1
1
  import Request from './utils/request';
2
+ import {isValidString} from './utils/helpers';
2
3
 
3
4
  class StaffApiServices {
4
5
  constructor() {
@@ -7,20 +8,45 @@ class StaffApiServices {
7
8
 
8
9
  /**
9
10
  * Opens an user's shift
11
+ * @param {Object} params
12
+ * @param {string} params.warehouseId - shift current warehouse ID
10
13
  * @returns {Promise<Object>} - Response from the API
11
14
  */
12
15
 
13
- async openShift() {
16
+ async openShift({warehouseId} = {}) {
14
17
  try {
15
18
  return await Request.post({
16
19
  service: this.service,
17
20
  namespace: 'shift-open',
21
+ body: {
22
+ ...(isValidString(warehouseId) && {warehouseId}),
23
+ },
18
24
  });
19
25
  } catch (error) {
20
26
  return Promise.reject(error);
21
27
  }
22
28
  }
23
29
 
30
+ /**
31
+ * Updates the current user's shift
32
+ * @param {Object} params
33
+ * @param {string} params.warehouseId - shift current warehouse ID
34
+ * @returns {Promise<Object>} - Response from the API
35
+ */
36
+
37
+ async updateShift({warehouseId} = {}) {
38
+ try {
39
+ return await Request.post({
40
+ service: this.service,
41
+ namespace: 'shift-update',
42
+ body: {
43
+ ...(isValidString(warehouseId) && {warehouseId}),
44
+ },
45
+ });
46
+ } catch (error) {
47
+ return Promise.reject(error);
48
+ }
49
+ }
24
50
  /**
25
51
  * Closes current user's shift
26
52
  * @returns {Promise<Object>} - Response from the API
@@ -20,7 +20,15 @@ import {
20
20
  import {isValidObject, promiseWrapper} from '../utils/helpers';
21
21
  import Shift from '../Shift';
22
22
 
23
- const ShiftTrackingProvider = ({children, onError = null}) => {
23
+ /**
24
+ * ShiftTrackingProvider component
25
+ * @param {Object} props
26
+ * @param {React.ReactNode} props.children - Child components
27
+ * @param {Object} props.additionalInfo - Additional information for shift tracking
28
+ * @param {string} props.additionalInfo.warehouseId - Warehouse ID to associate with the shift
29
+ * @param {Function} props.onError - Error callback handler
30
+ */
31
+ const ShiftTrackingProvider = ({children, additionalInfo = {}, onError = null}) => {
24
32
  const shiftStatus = useStorageValue(SHIFT_STATUS);
25
33
  const shiftId = useStorageValue(SHIFT_ID);
26
34
  const shiftData = useStorageValue(SHIFT_DATA, {});
@@ -40,6 +48,7 @@ const ShiftTrackingProvider = ({children, onError = null}) => {
40
48
 
41
49
  const {workLogTypes = []} = workLogData;
42
50
  const {hasStaffAuthorization = false} = staffAuthData;
51
+ const {warehouseId = ''} = additionalInfo;
43
52
 
44
53
  const contextValues = useMemo(() => {
45
54
  return {
@@ -66,7 +75,7 @@ const ShiftTrackingProvider = ({children, onError = null}) => {
66
75
  isShiftLoading,
67
76
  ]);
68
77
 
69
- const handleShiftTrackingInit = async () => {
78
+ const shiftInitialization = async () => {
70
79
  setIsShiftLoading(true);
71
80
  const [isAuthorized, authError] = await promiseWrapper(isAuthorizedToUseStaffMS());
72
81
 
@@ -84,7 +93,12 @@ const ShiftTrackingProvider = ({children, onError = null}) => {
84
93
  return;
85
94
  }
86
95
 
87
- const [shiftResult, openError] = await promiseWrapper(openShift(onError));
96
+ const [shiftResult, openError] = await promiseWrapper(
97
+ openShift({
98
+ warehouseId,
99
+ onOpenShiftError: onError,
100
+ })
101
+ );
88
102
 
89
103
  if (openError) {
90
104
  setIsShiftLoading(false);
@@ -146,9 +160,25 @@ const ShiftTrackingProvider = ({children, onError = null}) => {
146
160
  };
147
161
 
148
162
  useEffect(() => {
149
- handleShiftTrackingInit();
163
+ shiftInitialization();
150
164
  }, []);
151
165
 
166
+ useEffect(() => {
167
+ const handleWarehouseChange = async () => {
168
+ if (warehouseId && Shift.isOpen) {
169
+ try {
170
+ await Shift.update({warehouseId});
171
+ } catch (errorWarehouseChange) {
172
+ setError({
173
+ message: errorWarehouseChange?.message,
174
+ type: 'updateShift',
175
+ });
176
+ }
177
+ }
178
+ };
179
+ handleWarehouseChange();
180
+ }, [warehouseId]);
181
+
152
182
  useEffect(() => {
153
183
  if (openShiftResult?.id && openShiftResult?.getWorkLogs) {
154
184
  getShiftWorkLogsHistory();
@@ -22,3 +22,5 @@ export const reverseArray = (arr) => arr.slice().reverse();
22
22
  export const isNumber = (num) => typeof num === 'number' && !Number.isNaN(Number(num));
23
23
 
24
24
  export const isValidObject = (obj) => isObject(obj) && !!Object.keys(obj).length;
25
+
26
+ export const isValidString = (str) => typeof str === 'string' && str.length > 0;
@@ -4,13 +4,12 @@ import Shift from '../../../Shift';
4
4
  import {isFunction} from '../../helpers';
5
5
  import errorParser from '../../errorParser';
6
6
 
7
- const openShift = async (onOpenShiftError) => {
7
+ const openShift = async ({onOpenShiftError, warehouseId}) => {
8
8
  try {
9
9
  const userId = await getUserId();
10
-
11
10
  if (!userId) {
12
11
  await Shift.deleteShiftRegisters();
13
- const openShiftId = await Shift.open();
12
+ const openShiftId = await Shift.open({warehouseId});
14
13
  return {
15
14
  openShiftId,
16
15
  getWorkLogs: true,
@@ -20,7 +19,7 @@ const openShift = async (onOpenShiftError) => {
20
19
  const currentShift = await Shift.getUserOpenShift({id: Shift.id, userId});
21
20
  if (currentShift?.status !== 'opened') {
22
21
  await Shift.deleteShiftRegisters();
23
- const openShiftId = await Shift.open();
22
+ const openShiftId = await Shift.open({warehouseId});
24
23
  return {
25
24
  openShiftId,
26
25
  getWorkLogs: true,
@@ -37,6 +36,7 @@ const openShift = async (onOpenShiftError) => {
37
36
  Shift.id = currentShift.id;
38
37
  Shift.status = currentShift.status;
39
38
  Shift.data = currentShift;
39
+
40
40
  return {
41
41
  openShiftId: currentShift.id,
42
42
  getWorkLogs: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@janiscommerce/app-tracking-shift",
3
- "version": "1.5.0",
3
+ "version": "1.7.0-beta.0",
4
4
  "main": "lib/index.js",
5
5
  "module": "lib/index.js",
6
6
  "exports": {
@@ -49,8 +49,9 @@
49
49
  "@janiscommerce/app-device-info": "^1.1.0",
50
50
  "@janiscommerce/app-request": ">=2.0.0",
51
51
  "@janiscommerce/app-storage": ">=1.1.0",
52
- "react": ">=17.0.2 <19",
53
- "react-native": ">=0.67.5 <0.75"
52
+ "react": ">=17.0.2 <20.0.0",
53
+ "react-dom": ">=17.0.2 <20.0.0",
54
+ "react-native": ">=0.71.5 <0.82.0"
54
55
  },
55
56
  "devDependencies": {
56
57
  "@babel/core": "^7.0.0",
@@ -79,6 +80,7 @@
79
80
  "jest-environment-jsdom": "^29.0.0",
80
81
  "prettier": "^2.8.8",
81
82
  "react": "18.2.0",
83
+ "react-dom": "18.2.0",
82
84
  "react-native": "0.71.6",
83
85
  "react-native-device-info": "^10.12.0",
84
86
  "react-native-mmkv": "2.12.2",