Skip to content

Commit

Permalink
Improve/api calls post requests (#1946)
Browse files Browse the repository at this point in the history
* created post method for direct fetching

* added direct invite email post request

* removed unnecessary code, and improved api
  • Loading branch information
desperado1802 authored Dec 4, 2023
1 parent 493f144 commit 434ce47
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
18 changes: 13 additions & 5 deletions apps/web/app/hooks/features/useTeamInvitations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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(() => {
Expand Down
46 changes: 42 additions & 4 deletions apps/web/app/services/client/api/invite.ts
Original file line number Diff line number Diff line change
@@ -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<PaginationResponse<IInvitation>>('/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<IRole, any> = (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) {
Expand Down
19 changes: 18 additions & 1 deletion apps/web/app/services/client/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

0 comments on commit 434ce47

Please sign in to comment.