Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve/api calls post requests #1946

Merged
merged 5 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
20 changes: 15 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,26 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@desperado1802 can we add tenantId also in the object?

).then((res) => {
console.log('invited!!!!:', res);

setTeamInvitations((prev) => [...prev, ...(res.data?.items || [])]);
return res;
});
},
[inviteQueryCall, setTeamInvitations]
[inviteQueryCall, setTeamInvitations, user?.tenantId, activeTeamId, user?.employee.organizationId]
);

useEffect(() => {
Expand Down
62 changes: 58 additions & 4 deletions apps/web/app/services/client/api/invite.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,63 @@
import { PaginationResponse } from '@app/interfaces/IDataResponse';
import { IInvitation, IInviteRequest, MyInvitationActionEnum, CreateResponse } from '@app/interfaces';
import api, { get } from '../axios';
import {
IInvitation,
// IInviteRequest,
MyInvitationActionEnum,
CreateResponse,
IInviteCreate
} from '@app/interfaces';
import { INVITE_CALLBACK_URL } from '@app/constants';
import api, { get, post } from '../axios';
// import { getEmployeeRoleRequest } from '@app/services/server/requests';
// import { getCookie } from 'cookies-next';

export function inviteByEmailsAPI(data: IInviteRequest) {
return api.post<PaginationResponse<IInvitation>>('/invite/emails', data);
// 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 authToken = getCookie('auth-token')?.toString();

const date = new Date();
date.setDate(date.getDate() - 1);

// console.log('authToken:', authToken);
// console.log('tenantId:', tenantId);

// const { data: employeeRole } = await getEmployeeRoleRequest({
// tenantId,
// role: 'EMPLOYEE',
// bearer_token: authToken as string
// });

const dataToInviteUser: IInviteCreate = {
emailIds: [data.email],
projectIds: [],
departmentIds: [],
organizationContactIds: [],
teamIds: [data.teamId],
roleId: 'b1d702d9-9380-4cda-a442-0e7d1ca7480e' || '',
invitationExpirationPeriod: 'Never',
inviteType: 'TEAM',
appliedDate: null,
fullName: data.name,
callbackUrl: INVITE_CALLBACK_URL,
organizationId: data.organizationId,
startedWorkOn: date.toISOString()
};

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
20 changes: 19 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,24 @@ function get(
: api.get(endpoint);
}

function post(
endpoint: string,
data: any,
isDirect: boolean,
extras?: {
tenantId: string;
}
) {
console.log('Post Request:', endpoint, data, isDirect, extras);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove consoles/commented code from the PR.

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 };
2 changes: 2 additions & 0 deletions apps/web/app/services/server/requests/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export function getEmployeeRoleRequest({
role: 'EMPLOYEE';
tenantId: string;
}) {
console.log('authToken:', bearer_token);
console.log('tenantId:', tenantId);
return serverFetch<IRole>({
path: `/roles/options?name=${role}`,
method: 'GET',
Expand Down
Loading