Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/actions/timeEntries.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
GET_TIME_ENTRIES_PERIOD_BULK,
} from '../constants/timeEntries';
import { ENDPOINTS } from '~/utils/URL';
import { fetchServerTime, getServerMoment } from '../utils/serverTime';

export const setTimeEntriesForWeek = (data, offset) => ({
type: GET_TIME_ENTRIES_WEEK,
Expand Down Expand Up @@ -47,6 +48,7 @@
.format('YYYY-MM-DDTHH:mm:ss');

const url = ENDPOINTS.TIME_ENTRIES_PERIOD(userId, fromDate, toDate);

return async dispatch => {
let loggedOut = false;
const res = await axios.get(url).catch(error => {
Expand Down Expand Up @@ -197,21 +199,67 @@
}
};
};

/*
export const postTimeEntry = (timeEntry, { displayedUserId } = {}) => {
const url = ENDPOINTS.TIME_ENTRY();
return async dispatch => {
try {
const res = await axios.post(url, timeEntry);
if (timeEntry.entryType === 'default' || timeEntry.entryType === 'person') {
dispatch(updateTimeEntries(timeEntry, undefined, displayedUserId));
}
return res.status;
} catch (e) {
return e.response.status;
}
};
};
*/


export const postTimeEntry = (timeEntry, { displayedUserId } = {}) => {
const url = ENDPOINTS.TIME_ENTRY();

return async dispatch => {
try {
// Get backend server time
await fetchServerTime();

const serverNow = moment(getServerMoment());

const selectedDate = moment(timeEntry.dateOfWork);

console.log('Server time:', serverNow.format());
console.log('Selected time entry date:', selectedDate.format());

// // Prevent future date/time logging
// if (selectedDate.isAfter(serverNow)) {
// toast.error('You cannot log time for a future date or time.');
// return 400;
// }

// Prevent logging on previous or future dates
if (!selectedDate.isSame(serverNow, 'day')) {
toast.error('You can only log time for today.');
return 400;
}

const res = await axios.post(url, timeEntry);

if (timeEntry.entryType === 'default' || timeEntry.entryType === 'person') {
dispatch(updateTimeEntries(timeEntry, undefined, displayedUserId));
}

return res.status;

} catch (e) {
return e.response.status;

Check failure on line 257 in src/actions/timeEntries.js

View workflow job for this annotation

GitHub Actions / test

src/actions/__tests__/timeEntries.js.test.js > timeEntries action creators > postTimeEntry > should handle errors and return the error response status

TypeError: Cannot read properties of undefined (reading 'status') ❯ src/actions/timeEntries.js:257:25 ❯ src/actions/__tests__/timeEntries.js.test.js:181:22

Check failure on line 257 in src/actions/timeEntries.js

View workflow job for this annotation

GitHub Actions / test

src/actions/__tests__/timeEntries.js.test.js > timeEntries action creators > postTimeEntry > should return the response status

TypeError: Cannot read properties of undefined (reading 'status') ❯ src/actions/timeEntries.js:257:25 ❯ src/actions/__tests__/timeEntries.js.test.js:170:22

Check failure on line 257 in src/actions/timeEntries.js

View workflow job for this annotation

GitHub Actions / test

src/actions/__tests__/timeEntries.js.test.js > timeEntries action creators > postTimeEntry > should dispatch updateTimeEntries if entryType is default

TypeError: Cannot read properties of undefined (reading 'status') ❯ src/actions/timeEntries.js:257:25 ❯ src/actions/__tests__/timeEntries.js.test.js:156:7

Check failure on line 257 in src/actions/timeEntries.js

View workflow job for this annotation

GitHub Actions / test

src/actions/__tests__/timeEntries.js.test.js > timeEntries action creators > postTimeEntry > should handle errors and return the error response status

TypeError: Cannot read properties of undefined (reading 'status') ❯ src/actions/timeEntries.js:257:25 ❯ src/actions/__tests__/timeEntries.js.test.js:181:22

Check failure on line 257 in src/actions/timeEntries.js

View workflow job for this annotation

GitHub Actions / test

src/actions/__tests__/timeEntries.js.test.js > timeEntries action creators > postTimeEntry > should return the response status

TypeError: Cannot read properties of undefined (reading 'status') ❯ src/actions/timeEntries.js:257:25 ❯ src/actions/__tests__/timeEntries.js.test.js:170:22

Check failure on line 257 in src/actions/timeEntries.js

View workflow job for this annotation

GitHub Actions / test

src/actions/__tests__/timeEntries.js.test.js > timeEntries action creators > postTimeEntry > should dispatch updateTimeEntries if entryType is default

TypeError: Cannot read properties of undefined (reading 'status') ❯ src/actions/timeEntries.js:257:25 ❯ src/actions/__tests__/timeEntries.js.test.js:156:7
}
};
};


export const editTimeEntry = (timeEntryId, timeEntry, oldDateOfWork, { displayedUserId } = {}) => {
const url = ENDPOINTS.TIME_ENTRY_CHANGE(timeEntryId);
return async dispatch => {
Expand Down Expand Up @@ -270,4 +318,4 @@
// export const setTimeEntriesForPeriod = data => ({
// type: GET_TIME_ENTRIES_PERIOD,
// payload: data,
// });
// });
13 changes: 13 additions & 0 deletions src/utils/serverTime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import axios from 'axios';

let serverTime = null;

export const fetchServerTime = async () => {
const response = await axios.get('/api/servertime');
serverTime = response.data.serverTime;
return serverTime;
};

export const getServerMoment = () => {
return serverTime ? serverTime : new Date();

Check warning on line 12 in src/utils/serverTime.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Unnecessary use of conditional expression for default assignment.

See more on https://sonarcloud.io/project/issues?id=OneCommunityGlobal_HighestGoodNetworkApp&issues=AZ9nVTdQOZcrxV_HnKq4&open=AZ9nVTdQOZcrxV_HnKq4&pullRequest=5390
};
Loading