diff --git a/apps/web/app/hooks/features/useTaskStatistics.ts b/apps/web/app/hooks/features/useTaskStatistics.ts index b5c9fa256..fd44986a7 100644 --- a/apps/web/app/hooks/features/useTaskStatistics.ts +++ b/apps/web/app/hooks/features/useTaskStatistics.ts @@ -21,8 +21,10 @@ import { useSyncRef } from '../useSyncRef'; import { Nullable } from '@app/interfaces'; import { useRefreshInterval } from './useRefreshInterval'; import { useOrganizationTeams } from './useOrganizationTeams'; +import { useAuthenticateUser } from './useAuthenticateUser'; export function useTaskStatistics(addSeconds = 0) { + const { user } = useAuthenticateUser(); const [statActiveTask, setStatActiveTask] = useRecoilState(activeTaskStatisticsState); const [statTasks, setStatTasks] = useRecoilState(tasksStatisticsState); const setTasksFetching = useSetRecoilState(tasksFetchingState); @@ -45,14 +47,19 @@ export function useTaskStatistics(addSeconds = 0) { */ const getTasksStatsData = useCallback( (employeeId?: string) => { - tasksTimesheetStatisticsAPI(employeeId).then(({ data }) => { - setStatTasks({ - all: data.global || [], - today: data.today || [] - }); - }); + if (!user?.employee.tenantId) { + return; + } + tasksTimesheetStatisticsAPI(user?.employee.tenantId, '', user?.employee.organizationId, employeeId).then( + ({ data }) => { + setStatTasks({ + all: data.global || [], + today: data.today || [] + }); + } + ); }, - [setStatTasks] + [setStatTasks, user?.employee.organizationId, user?.employee.tenantId] ); const getAllTasksStatsData = useCallback(() => { allTaskTimesheetStatisticsAPI().then(({ data }) => { @@ -78,8 +85,20 @@ export function useTaskStatistics(addSeconds = 0) { * Get statistics of the active tasks fresh (API Call) */ const getActiveTaskStatData = useCallback(() => { + if (!user?.employee.tenantId || !user?.employee.organizationId) { + return new Promise((resolve) => { + resolve(true); + }); + } + setTasksFetching(true); - const promise = activeTaskTimesheetStatisticsAPI(); + + const promise = activeTaskTimesheetStatisticsAPI( + user?.employee.tenantId, + '', + user?.employee.organizationId, + '' + ); promise.then(({ data }) => { setStatActiveTask({ total: data.global ? data.global[0] || null : null, @@ -90,7 +109,7 @@ export function useTaskStatistics(addSeconds = 0) { setTasksFetching(false); }); return promise; - }, [setStatActiveTask, setTasksFetching]); + }, [setStatActiveTask, setTasksFetching, user?.employee.organizationId, user?.employee.tenantId]); // eslint-disable-next-line react-hooks/exhaustive-deps const debounceLoadActiveTaskStat = useCallback(debounce(getActiveTaskStatData, 100), []); @@ -104,7 +123,7 @@ export function useTaskStatistics(addSeconds = 0) { initialLoad.current = true; }); } - }, [firstLoad, getActiveTaskStatData]); + }, [firstLoad, getActiveTaskStatData, user?.employee.organizationId, user?.employee.tenantId]); /** * Get fresh statistic of the active task diff --git a/apps/web/app/services/client/api/tasks.ts b/apps/web/app/services/client/api/tasks.ts index 086f20eb6..62c81e640 100644 --- a/apps/web/app/services/client/api/tasks.ts +++ b/apps/web/app/services/client/api/tasks.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-mixed-spaces-and-tabs */ import { CreateResponse, DeleteResponse, PaginationResponse } from '@app/interfaces/IDataResponse'; import { ICreateTask, ITeamTask } from '@app/interfaces/ITask'; import { ITasksTimesheet } from '@app/interfaces/ITimer'; @@ -54,16 +55,97 @@ export function createTeamTaskAPI(body: Partial & { title: string } return api.post>('/tasks/team', body); } -export function tasksTimesheetStatisticsAPI(employeeId?: string) { - return api.get<{ global: ITasksTimesheet[]; today: ITasksTimesheet[] }>( - `/timer/timesheet/statistics-tasks${employeeId ? '?employeeId=' + employeeId : ''}` - ); +export async function tasksTimesheetStatisticsAPI( + tenantId: string, + activeTaskId: string, + organizationId: string, + employeeId?: string +) { + if (process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL) { + const employeesParams = employeeId + ? [employeeId].reduce((acc: any, v, i) => { + acc[`employeeIds[${i}]`] = v; + return acc; + }) + : {}; + const commonParams = { + tenantId, + organizationId, + // ...(activeTaskId ? { 'taskIds[0]': activeTaskId } : {}), + ...employeesParams + }; + const globalQueries = new URLSearchParams({ + ...commonParams, + defaultRange: 'false' + }); + const globalData = await get(`/timesheet/statistics/tasks?${globalQueries.toString()}`, true, { + tenantId + }); + + const todayQueries = new URLSearchParams({ + defaultRange: 'true', + unitOfTime: 'day' + }); + const todayData = await get(`/timesheet/statistics/tasks?${todayQueries.toString()}`, true, { + tenantId + }); + + return { + data: { + global: globalData.data, + today: todayData.data + } + }; + } else { + return api.get<{ global: ITasksTimesheet[]; today: ITasksTimesheet[] }>( + `/timer/timesheet/statistics-tasks${employeeId ? '?employeeId=' + employeeId : ''}` + ); + } } -export function activeTaskTimesheetStatisticsAPI() { - return api.get<{ global: ITasksTimesheet[]; today: ITasksTimesheet[] }>( - `/timer/timesheet/statistics-tasks?activeTask=true` - ); +export async function activeTaskTimesheetStatisticsAPI( + tenantId: string, + activeTaskId: string, + organizationId: string, + employeeId?: string +) { + if (process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL) { + const employeesParams = employeeId + ? [employeeId].reduce((acc: any, v, i) => { + acc[`employeeIds[${i}]`] = v; + return acc; + }) + : {}; + const commonParams = { + tenantId, + organizationId, + ...(activeTaskId ? { 'taskIds[0]': activeTaskId } : {}), + ...employeesParams + }; + const globalQueries = new URLSearchParams({ + ...commonParams, + defaultRange: 'false' + }); + const globalData = await get(`/timesheet/statistics/tasks?${globalQueries.toString()}`, true, { + tenantId + }); + + const todayQueries = new URLSearchParams({ ...commonParams, defaultRange: 'true', unitOfTime: 'day' }); + const todayData = await get(`/timesheet/statistics/tasks?${todayQueries.toString()}`, true, { + tenantId + }); + + return { + data: { + global: globalData.data, + today: todayData.data + } + }; + } else { + return api.get<{ global: ITasksTimesheet[]; today: ITasksTimesheet[] }>( + `/timer/timesheet/statistics-tasks?activeTask=true` + ); + } } export function allTaskTimesheetStatisticsAPI() { diff --git a/apps/web/app/services/server/requests/timesheet.ts b/apps/web/app/services/server/requests/timesheet.ts index 6b602c895..7b17116a2 100644 --- a/apps/web/app/services/server/requests/timesheet.ts +++ b/apps/web/app/services/server/requests/timesheet.ts @@ -1,7 +1,7 @@ import { ITasksTimesheet } from '@app/interfaces/ITimer'; import { serverFetch } from '../fetch'; -type TTasksTimesheetStatisticsParams = { +export type TTasksTimesheetStatisticsParams = { tenantId: string; organizationId: string; startDate?: string;