From 434ce47ebeb7c9688e5b136e3c90d18457a63176 Mon Sep 17 00:00:00 2001 From: desperado1802 <124465103+desperado1802@users.noreply.github.com> Date: Mon, 4 Dec 2023 17:59:31 +0200 Subject: [PATCH] Improve/api calls post requests (#1946) * created post method for direct fetching * added direct invite email post request * removed unnecessary code, and improved api --- .../app/hooks/features/useTeamInvitations.ts | 18 ++++++-- apps/web/app/services/client/api/invite.ts | 46 +++++++++++++++++-- apps/web/app/services/client/axios.ts | 19 +++++++- 3 files changed, 73 insertions(+), 10 deletions(-) diff --git a/apps/web/app/hooks/features/useTeamInvitations.ts b/apps/web/app/hooks/features/useTeamInvitations.ts index 5e1118437..6559b6159 100644 --- a/apps/web/app/hooks/features/useTeamInvitations.ts +++ b/apps/web/app/hooks/features/useTeamInvitations.ts @@ -21,8 +21,6 @@ import { useQuery } from '../useQuery'; import { useAuthenticateUser } from './useAuthenticateUser'; export function useTeamInvitations() { - const { user } = useAuthenticateUser(); - const setTeamInvitations = useSetRecoilState(teamInvitationsState); const [myInvitationsList, setMyInvitationsList] = useRecoilState(myInvitationsState); @@ -48,14 +46,24 @@ export function useTeamInvitations() { const { queryCall: acceptRejectMyInvitationsQueryCall, loading: acceptRejectMyInvitationsLoading } = useQuery(acceptRejectMyInvitationsAPI); + const { user } = useAuthenticateUser(); + const inviteUser = useCallback( (email: string, name: string) => { - return inviteQueryCall({ email, name }).then((res) => { - setTeamInvitations(res.data?.items || []); + return inviteQueryCall( + { + email, + name, + organizationId: user?.employee.organizationId as string, + teamId: activeTeamId as string + }, + user?.tenantId as string + ).then((res) => { + setTeamInvitations((prev) => [...prev, ...(res.data?.items || [])]); return res; }); }, - [inviteQueryCall, setTeamInvitations] + [inviteQueryCall, setTeamInvitations, user?.tenantId, activeTeamId, user?.employee.organizationId] ); useEffect(() => { diff --git a/apps/web/app/services/client/api/invite.ts b/apps/web/app/services/client/api/invite.ts index 63e9ceb7c..73e46035c 100644 --- a/apps/web/app/services/client/api/invite.ts +++ b/apps/web/app/services/client/api/invite.ts @@ -1,9 +1,47 @@ import { PaginationResponse } from '@app/interfaces/IDataResponse'; -import { IInvitation, IInviteRequest, MyInvitationActionEnum, CreateResponse } from '@app/interfaces'; -import api, { get } from '../axios'; +import { IInvitation, MyInvitationActionEnum, CreateResponse, IInviteCreate, IRole } from '@app/interfaces'; +import { INVITE_CALLBACK_URL } from '@app/constants'; +import api, { get, post } from '../axios'; +import { AxiosResponse } from 'axios'; -export function inviteByEmailsAPI(data: IInviteRequest) { - return api.post>('/invite/emails', data); +interface IIInviteRequest { + email: string; + name: string; + teamId: string; + organizationId: string; +} + +export async function inviteByEmailsAPI(data: IIInviteRequest, tenantId: string) { + const endpoint = '/invite/emails'; + + const date = new Date(); + date.setDate(date.getDate() - 1); + + const getRoleEndpoint = '/roles/options?name=EMPLOYEE'; + + const employeeRole: AxiosResponse = (await get(getRoleEndpoint, true, { tenantId })).data; + + const dataToInviteUser: IInviteCreate & { tenantId: string } = { + emailIds: [data.email], + projectIds: [], + departmentIds: [], + organizationContactIds: [], + teamIds: [data.teamId], + roleId: employeeRole.data.id || '', + invitationExpirationPeriod: 'Never', + inviteType: 'TEAM', + appliedDate: null, + fullName: data.name, + callbackUrl: INVITE_CALLBACK_URL, + organizationId: data.organizationId, + tenantId, + startedWorkOn: date.toISOString() + }; + + // for not direct call we need to adjust data to include name and email only + const fetchData = await post(endpoint, dataToInviteUser, true, { tenantId }); + + return process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL ? fetchData.data : fetchData; } export async function getTeamInvitationsAPI(tenantId: string, organizationId: string, role: string, teamId: string) { diff --git a/apps/web/app/services/client/axios.ts b/apps/web/app/services/client/axios.ts index 92f3452d2..bd53becb2 100644 --- a/apps/web/app/services/client/axios.ts +++ b/apps/web/app/services/client/axios.ts @@ -87,6 +87,23 @@ function get( : api.get(endpoint); } +function post( + endpoint: string, + data: any, + isDirect: boolean, + extras?: { + tenantId: string; + } +) { + return isDirect && process.env.NEXT_PUBLIC_GAUZY_API_SERVER_URL + ? apiDirect.post(endpoint, data, { + headers: { + ...(extras?.tenantId ? { 'tenant-id': extras?.tenantId } : {}) + } + }) + : api.post(endpoint, data); +} + export default api; -export { apiDirect, get }; +export { apiDirect, get, post };