Skip to content

Commit

Permalink
Merge pull request #1937 from ever-co/develop
Browse files Browse the repository at this point in the history
Fix/api improvements (#1934)
  • Loading branch information
evereq authored Dec 2, 2023
2 parents 8689bcf + d5b3ddb commit 6a5d362
Show file tree
Hide file tree
Showing 34 changed files with 241 additions and 90 deletions.
2 changes: 1 addition & 1 deletion apps/web/app/hooks/features/useAuthenticateUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const useAuthenticateUser = (defaultUser?: IUser) => {
return;
}
refreshUserQueryCall().then((res) => {
setUser(res.data.user);
setUser(res.data);
});
}, [refreshUserQueryCall, setUser, refreshUserLoadingRef]);

Expand Down
11 changes: 8 additions & 3 deletions apps/web/app/hooks/features/useEmployee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@ import { useCallback, useEffect } from 'react';
import { useRecoilState } from 'recoil';

import { useQuery } from '../useQuery';
import { useAuthenticateUser } from './useAuthenticateUser';

export const useEmployee = () => {
const { user } = useAuthenticateUser();
const [workingEmployees, setWorkingEmployees] = useRecoilState(workingEmployeesState);
const [workingEmployeesEmail, setWorkingEmployeesEmail] = useRecoilState(workingEmployeesEmailState);

const { queryCall: getWorkingEmployeeQueryCall, loading: getWorkingEmployeeLoading } =
useQuery(getWorkingEmployeesAPI);

const getWorkingEmployee = useCallback(() => {
getWorkingEmployeeQueryCall().then((data) => {
if (!user?.tenantId) {
return;
}
getWorkingEmployeeQueryCall(user?.tenantId, user?.employee.organizationId).then((data) => {
if (data?.data?.items && data?.data?.items?.length) {
const items = data.data.items || [];
setWorkingEmployees(items);
setWorkingEmployeesEmail(items.map((item) => item.user?.email || ''));
setWorkingEmployeesEmail(items.map((item: any) => item.user?.email || ''));
}
});
}, [getWorkingEmployeeQueryCall, setWorkingEmployees, setWorkingEmployeesEmail]);
}, [getWorkingEmployeeQueryCall, setWorkingEmployees, setWorkingEmployeesEmail, user]);

useEffect(() => {
getWorkingEmployee();
Expand Down
19 changes: 12 additions & 7 deletions apps/web/app/hooks/features/useOrganizationTeams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,25 @@ export function useOrganizationTeams() {
);

const loadTeamsData = useCallback(() => {
if (loadingRef.current || loadingRefTeam.current) {
if (
loadingRef.current ||
loadingRefTeam.current ||
!user?.employee.organizationId ||
!user?.employee.tenantId
) {
return;
}

let teamId = getActiveTeamIdCookie();
setActiveTeamId(teamId);

return queryCall().then((res) => {
return queryCall(user?.employee.organizationId, user?.employee.tenantId).then((res) => {
if (res.data?.items && res.data?.items?.length === 0) {
setIsTeamMember(false);
}
const latestTeams = res.data?.items || [];

const latestTeamsSorted = latestTeams.slice().sort((a, b) => a.name.localeCompare(b.name));
const latestTeamsSorted = latestTeams.slice().sort((a: any, b: any) => a.name.localeCompare(b.name));

const teamsRefSorted = teamsRef.current.slice().sort((a, b) => a.name.localeCompare(b.name));

Expand All @@ -256,7 +261,7 @@ export function useOrganizationTeams() {

// Handle case where user might Remove Account from all teams,
// In such case need to update active team with Latest list of Teams
if (!latestTeams.find((team) => team.id === teamId) && latestTeams.length) {
if (!latestTeams.find((team: any) => team.id === teamId) && latestTeams.length) {
setActiveTeam(latestTeams[0]);
} else if (!latestTeams.length) {
teamId = '';
Expand All @@ -274,7 +279,7 @@ export function useOrganizationTeams() {
* (It prevents unnecessary re-rendering)
*/
if (!isEqual(latestTeamsSorted, teamsRefSorted)) {
setTeams([newTeam, ...latestTeams.filter((team) => team.id !== newTeam.id)]);
setTeams([newTeam, ...latestTeams.filter((team: any) => team.id !== newTeam.id)]);

// Set Project Id to cookie
// TODO: Make it dynamic when we add Dropdown in Navbar
Expand Down Expand Up @@ -304,8 +309,8 @@ export function useOrganizationTeams() {
loadingTeamsRef,
setTeams,
setTeamsUpdate,
user?.employee.organizationId,
user?.employee.tenantId
user?.employee?.organizationId,
user?.employee?.tenantId
]);

const editOrganizationTeam = useCallback(
Expand Down
2 changes: 1 addition & 1 deletion apps/web/app/hooks/features/useSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function useSettings() {
(userData: Partial<IUser> & { id: string }) => {
return updateAvatarQueryCall(userData.id, userData).then((res) => {
refreshUserQueryCall().then((result) => {
setUser(result.data.user);
setUser(result.data);
});
return res;
});
Expand Down
20 changes: 18 additions & 2 deletions apps/web/app/hooks/features/useTeamTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import { useFirstLoad } from '../useFirstLoad';
import { useQuery } from '../useQuery';
import { useSyncRef } from '../useSyncRef';
import { useOrganizationEmployeeTeams } from './useOrganizatioTeamsEmployee';
import { useAuthenticateUser } from './useAuthenticateUser';

export function useTeamTasks() {
const { updateOrganizationTeamEmployeeActiveTask } = useOrganizationEmployeeTeams();
const { user } = useAuthenticateUser();

const setAllTasks = useSetRecoilState(teamTasksState);
const tasks = useRecoilValue(tasksByTeamState);
Expand Down Expand Up @@ -109,12 +111,26 @@ export function useTeamTasks() {
response(true);
});
}
return queryCall().then((res) => {

if (!user || activeTeamRef.current?.id) {
return new Promise((response) => {
response(true);
});
}

return queryCall(
user?.employee.organizationId,
user?.employee.tenantId,
activeTeamRef.current?.projects && activeTeamRef.current?.projects.length
? activeTeamRef.current?.projects[0].id
: '',
activeTeamRef.current?.id || ''
).then((res) => {
deepCheckAndUpdateTasks(res?.data?.items || [], deepCheck);
return res;
});
},
[queryCall, deepCheckAndUpdateTasks, loadingRef]
[queryCall, deepCheckAndUpdateTasks, loadingRef, user, activeTeamRef]
);

// Global loading state
Expand Down
6 changes: 3 additions & 3 deletions apps/web/app/hooks/features/useTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ export function useTimer() {

const getTimerStatus = useCallback(
(deepCheck?: boolean) => {
if (loadingRef.current) {
if (loadingRef.current || !user?.tenantId) {
return;
}
return queryCall().then((res) => {
return queryCall(user?.tenantId, user?.employee.organizationId).then((res) => {
if (res.data && !isEqual(timerStatus, res.data)) {
setTimerStatus((t) => {
if (deepCheck) {
Expand All @@ -206,7 +206,7 @@ export function useTimer() {
return res;
});
},
[timerStatus, setTimerStatus, queryCall, loadingRef]
[timerStatus, setTimerStatus, queryCall, loadingRef, user]
);

const toggleTimer = useCallback(
Expand Down
18 changes: 15 additions & 3 deletions apps/web/app/services/client/api/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getRefreshTokenCookie } from '@app/helpers/cookies';
import { ISuccessResponse } from '@app/interfaces';
import { ILoginResponse, IRegisterDataAPI, ISigninEmailConfirmResponse } from '@app/interfaces/IAuthentication';
import api from '../axios';
import api, { get } from '../axios';

export const signInWithEmailAndCodeAPI = (email: string, code: string) => {
return api.post<ILoginResponse>(`/auth/login`, {
Expand Down Expand Up @@ -31,8 +31,20 @@ export const signInEmailAPI = (email: string) => {
});
};

export const getAuthenticatedUserDataAPI = () => {
return api.get<Pick<ILoginResponse, 'user'>>(`/user/me`);
export const getAuthenticatedUserDataAPI = async () => {
const params = {} as { [x: string]: string };
const relations = ['employee', 'role', 'tenant'];

relations.forEach((rl, i) => {
params[`relations[${i}]`] = rl;
});

const query = new URLSearchParams(params);

const endpoint = `/user/me?${query.toString()}`;
const data = await get(endpoint, true);

return process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL ? data.data : data;
};

export const verifyUserEmailByCodeAPI = (code: string) => {
Expand Down
19 changes: 15 additions & 4 deletions apps/web/app/services/client/api/employee.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { PaginationResponse, IWorkingEmployee } from '@app/interfaces';
import api from '../axios';
import { get } from '../axios';

export function getWorkingEmployeesAPI() {
return api.get<PaginationResponse<IWorkingEmployee>>('/employee/working');
export async function getWorkingEmployeesAPI(tenantId: string, organizationId: string) {
const params = {
'where[tenantId]': tenantId,
'where[organizationId]': organizationId,
'relations[0]': 'user'
};
const query = new URLSearchParams(params);

const endpoint = process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL
? `/employee/pagination?${query.toString()}`
: '/employee/working';
const data = await get(endpoint, true, { tenantId });

return process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL ? data.data : data;
}
9 changes: 6 additions & 3 deletions apps/web/app/services/client/api/issue-type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CreateResponse, DeleteResponse, IIssueTypesCreate } from '@app/interfaces';
import api from '../axios';
import api, { get } from '../axios';

export function createIssueTypeAPI(data: IIssueTypesCreate, tenantId?: string) {
return api.post<CreateResponse<IIssueTypesCreate>>('/issue-types', data, {
Expand All @@ -21,6 +21,9 @@ export function deleteIssueTypeAPI(id: string) {
return api.delete<DeleteResponse>(`/issue-types/${id}`);
}

export function getIssueTypeList(tenantId: string, organizationId: string, activeTeamId: string | null) {
return api.get(`/issue-types?tenantId=${tenantId}&organizationId=${organizationId}&activeTeamId=${activeTeamId}`);
export async function getIssueTypeList(tenantId: string, organizationId: string, activeTeamId: string | null) {
const endpoint = `/issue-types?tenantId=${tenantId}&organizationId=${organizationId}&organizationTeamId=${activeTeamId}`;
const data = await get(endpoint, true, { tenantId });

return data;
}
32 changes: 29 additions & 3 deletions apps/web/app/services/client/api/organization-team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,39 @@ import {
IOrganizationTeamList,
IOrganizationTeamWithMStatus,
IOrganizationTeamUpdate,
IOrganizationTeam
IOrganizationTeam,
TimerSource
} from '@app/interfaces';
import moment from 'moment';
import api, { get } from '../axios';

export function getOrganizationTeamsAPI() {
return api.get<PaginationResponse<IOrganizationTeamList>>('/organization-team');
export async function getOrganizationTeamsAPI(organizationId: string, tenantId: string) {
const relations = [
'members',
'members.role',
'members.employee',
'members.employee.user',
'createdBy',
'createdBy.employee',
'projects',
'projects.repository'
];

const params = {
'where[organizationId]': organizationId,
'where[tenantId]': tenantId,
source: TimerSource.TEAMS,
withLaskWorkedTask: 'true'
} as { [x: string]: string };

relations.forEach((rl, i) => {
params[`relations[${i}]`] = rl;
});
const query = new URLSearchParams(params);
const endpoint = `/organization-team?${query.toString()}`;

const data = await get(endpoint, true, { tenantId });
return process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL ? data.data : data;
}

export function createOrganizationTeamAPI(name: string) {
Expand Down
9 changes: 6 additions & 3 deletions apps/web/app/services/client/api/task-labels.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CreateResponse, DeleteResponse, ITaskLabelsCreate } from '@app/interfaces';
import api from '../axios';
import api, { get } from '../axios';

export function createTaskLabelsAPI(data: ITaskLabelsCreate, tenantId?: string) {
return api.post<CreateResponse<ITaskLabelsCreate>>('/tags', data, {
Expand All @@ -21,6 +21,9 @@ export function deleteTaskLabelsAPI(id: string) {
return api.delete<DeleteResponse>(`/tags/${id}`);
}

export function getTaskLabelsList(tenantId: string, organizationId: string, activeTeamId: string | null) {
return api.get(`/tags/level?tenantId=${tenantId}&organizationId=${organizationId}&activeTeamId=${activeTeamId}`);
export async function getTaskLabelsList(tenantId: string, organizationId: string, organizationTeamId: string | null) {
const endpoint = `/tags/level?tenantId=${tenantId}&organizationId=${organizationId}&organizationTeamId=${organizationTeamId}`;
const data = await get(endpoint, true, { tenantId });

return data;
}
16 changes: 11 additions & 5 deletions apps/web/app/services/client/api/task-priorities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CreateResponse, DeleteResponse, ITaskPrioritiesCreate } from '@app/interfaces';
import api from '../axios';
import api, { get } from '../axios';

export function createTaskPrioritiesAPI(data: ITaskPrioritiesCreate, tenantId?: string) {
return api.post<CreateResponse<ITaskPrioritiesCreate>>('/task-priorities', data, {
Expand All @@ -21,8 +21,14 @@ export function deleteTaskPrioritiesAPI(id: string) {
return api.delete<DeleteResponse>(`/task-priorities/${id}`);
}

export function getTaskPrioritiesList(tenantId: string, organizationId: string, activeTeamId: string | null) {
return api.get(
`/task-priorities?tenantId=${tenantId}&organizationId=${organizationId}&activeTeamId=${activeTeamId}`
);
export async function getTaskPrioritiesList(
tenantId: string,
organizationId: string,
organizationTeamId: string | null
) {
const endpoint = `/task-priorities?tenantId=${tenantId}&organizationId=${organizationId}&organizationTeamId=${organizationTeamId}`;

const data = await get(endpoint, true, { tenantId });

return data;
}
16 changes: 11 additions & 5 deletions apps/web/app/services/client/api/task-related-issue-type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CreateResponse, DeleteResponse, ITaskRelatedIssueTypeCreate } from '@app/interfaces';
import api from '../axios';
import api, { get } from '../axios';

export function createTaskRelatedIssueTypeAPI(data: ITaskRelatedIssueTypeCreate, tenantId?: string) {
return api.post<CreateResponse<ITaskRelatedIssueTypeCreate>>('/task-related-issue-types', data, {
Expand All @@ -21,8 +21,14 @@ export function deleteTaskRelatedIssueTypeAPI(id: string) {
return api.delete<DeleteResponse>(`/task-related-issue-types/${id}`);
}

export function getTaskRelatedIssueTypeList(tenantId: string, organizationId: string, activeTeamId: string | null) {
return api.get(
`/task-related-issue-types?tenantId=${tenantId}&organizationId=${organizationId}&activeTeamId=${activeTeamId}`
);
export async function getTaskRelatedIssueTypeList(
tenantId: string,
organizationId: string,
organizationTeamId: string | null
) {
const endpoint = `/task-related-issue-types?tenantId=${tenantId}&organizationId=${organizationId}&organizationTeamId=${organizationTeamId}`;

const data = await get(endpoint, true, { tenantId });

return data;
}
10 changes: 7 additions & 3 deletions apps/web/app/services/client/api/task-status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CreateResponse, DeleteResponse, ITaskStatusCreate } from '@app/interfaces';
import api from '../axios';
import api, { get } from '../axios';

export function createTaskStatusAPI(data: ITaskStatusCreate, tenantId?: string) {
return api.post<CreateResponse<ITaskStatusCreate>>('/task-statuses', data, {
Expand All @@ -21,6 +21,10 @@ export function deleteTaskStatusAPI(id: string) {
return api.delete<DeleteResponse>(`/task-statuses/${id}`);
}

export function getTaskStatusList(tenantId: string, organizationId: string, activeTeamId: string | null) {
return api.get(`/task-statuses?tenantId=${tenantId}&organizationId=${organizationId}&activeTeamId=${activeTeamId}`);
export async function getTaskStatusList(tenantId: string, organizationId: string, organizationTeamId: string | null) {
const endpoint = `/task-statuses?tenantId=${tenantId}&organizationId=${organizationId}&organizationTeamId=${organizationTeamId}`;

const data = await get(endpoint, true, { tenantId });

return data;
}
10 changes: 7 additions & 3 deletions apps/web/app/services/client/api/task-version.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CreateResponse, DeleteResponse, ITaskVersionCreate } from '@app/interfaces';
import api from '../axios';
import api, { get } from '../axios';

export function createTaskVersionAPI(data: ITaskVersionCreate, tenantId?: string) {
return api.post<CreateResponse<ITaskVersionCreate>>('/task-versions', data, {
Expand All @@ -21,6 +21,10 @@ export function deleteTaskVersionAPI(id: string) {
return api.delete<DeleteResponse>(`/task-versions/${id}`);
}

export function getTaskVersionList(tenantId: string, organizationId: string, activeTeamId: string | null) {
return api.get(`/task-versions?tenantId=${tenantId}&organizationId=${organizationId}&activeTeamId=${activeTeamId}`);
export async function getTaskVersionList(tenantId: string, organizationId: string, organizationTeamId: string | null) {
const endpoint = `/task-versions?tenantId=${tenantId}&organizationId=${organizationId}&organizationTeamId=${organizationTeamId}`;

const data = await get(endpoint, true, { tenantId });

return data;
}
Loading

0 comments on commit 6a5d362

Please sign in to comment.