Skip to content

Commit

Permalink
implement integration tenant API on frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesNg35 committed Jan 15, 2024
1 parent 2ba676e commit 9f5db19
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 19 deletions.
2 changes: 1 addition & 1 deletion apps/web/app/api/integration-tenant/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export async function DELETE(req: Request, { params }: { params: { id: string }
if (id) {
const response = await deleteIntegrationTenantRequest(id as string, tenantId, organizationId, access_token);

return $res(response);
return $res(response.data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ export async function GET(req: Request) {
access_token
);

return $res(response);
return $res(response.data);
}
22 changes: 22 additions & 0 deletions apps/web/app/api/integration/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { authenticatedGuard } from '@app/services/server/guards/authenticated-guard-app';
import { getIntegrationRequest } from '@app/services/server/requests/integrations';
import { NextResponse } from 'next/server';

export async function GET(req: Request) {
const res = new NextResponse();
const { $res, user, access_token, tenantId } = await authenticatedGuard(req, res);
if (!user) return NextResponse.json({}, { status: 401 });

const { searchParams } = new URL(req.url);

const response = await getIntegrationRequest(
{
tenantId: tenantId,
integrationTypeId: searchParams.get('integrationTypeId') as string,
searchQuery: searchParams.get('searchQuery') as string
},
access_token
);

return $res(response.data);
}
4 changes: 2 additions & 2 deletions apps/web/app/hooks/integrations/useIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export function useIntegration() {
const getIntegration = useCallback(
(name: string) => {
return queryCall(name).then((response) => {
setIntegration(response.data.data);
setIntegration(response.data);

return response.data.data;
return response.data;
});
},
[queryCall, setIntegration]
Expand Down
4 changes: 2 additions & 2 deletions apps/web/app/hooks/integrations/useIntegrationTenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export function useIntegrationTenant() {
const getIntegrationTenant = useCallback(
(name: string) => {
return queryCall(name).then((response) => {
setIntegrationTenant(response.data.data.items);
setIntegrationTenant(response.data.items);

return response.data.data.items;
return response.data.items;
});
},
[queryCall, setIntegrationTenant]
Expand Down
8 changes: 3 additions & 5 deletions apps/web/app/services/client/api/integrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { CreateResponse, IIntegration } from '@app/interfaces';
import api from '../../axios';
import { IIntegration } from '@app/interfaces';
import { get } from '../../axios';

export function getIntegrationAPI(integrationTypeId: string, searchQuery = '') {
return api.get<CreateResponse<IIntegration[]>>(
`/integration?integrationTypeId=${integrationTypeId}&searchQuery=${searchQuery}`
);
return get<IIntegration[]>(`/integration?integrationTypeId=${integrationTypeId}&searchQuery=${searchQuery}`);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
import { IIntegrationTenant, PaginationResponse, CreateResponse, DeleteResponse } from '@app/interfaces';
import api from '../../axios';
import { IIntegrationTenant, PaginationResponse, DeleteResponse } from '@app/interfaces';
import { deleteApi, get } from '../../axios';
import { GAUZY_API_BASE_SERVER_URL } from '@app/constants';
import { getOrganizationIdCookie, getTenantIdCookie } from '@app/helpers';

export function getIntegrationTenantAPI(name: string) {
return api.get<CreateResponse<PaginationResponse<IIntegrationTenant>>>(
`/integration-tenant/remember/state?name=${name}`
);
const organizationId = getOrganizationIdCookie();
const tenantId = getTenantIdCookie();

const query = new URLSearchParams({
'where[organizationId]': organizationId,
'where[tenantId]': tenantId,
'where[name]': name
});

const endpoint = GAUZY_API_BASE_SERVER_URL.value
? `/integration-tenant?${query.toString()}`
: `/integration-tenant/remember/state?name=${name}`;

return get<PaginationResponse<IIntegrationTenant>>(endpoint);
}

export function deleteIntegrationTenantAPI(integrationId: string) {
return api.delete<DeleteResponse>(`/integration-tenant/${integrationId}`);
const organizationId = getOrganizationIdCookie();
const tenantId = getTenantIdCookie();

const endpoint = GAUZY_API_BASE_SERVER_URL.value
? `/integration-tenant/${integrationId}?organizationId=${organizationId}&tenantId=${tenantId}`
: `/integration-tenant/${integrationId}`;

return deleteApi<DeleteResponse>(endpoint);
}
12 changes: 10 additions & 2 deletions apps/web/app/services/client/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,16 @@ function apiConfig(config?: APIConfig) {

function get<T>(endpoint: string, config?: APIConfig) {
const { baseURL, headers } = apiConfig(config);
const { directAPI = true } = config || {};

return baseURL && directAPI ? apiDirect.get<T>(endpoint, { ...config, headers }) : api.get<T>(endpoint);
}

function deleteApi<T>(endpoint: string, config?: APIConfig) {
const { baseURL, headers } = apiConfig(config);
const { directAPI = true } = config || {};

return baseURL ? apiDirect.get<T>(endpoint, { ...config, headers }) : api.get<T>(endpoint);
return baseURL && directAPI ? apiDirect.delete<T>(endpoint, { ...config, headers }) : api.delete<T>(endpoint);
}

function post<T>(url: string, data?: Record<string, any> | FormData, config?: APIConfig) {
Expand All @@ -124,6 +132,6 @@ function post<T>(url: string, data?: Record<string, any> | FormData, config?: AP
return baseURL && directAPI ? apiDirect.post<T>(url, data, { ...config, headers }) : api.post<T>(url, data);
}

export { get, post };
export { get, post, deleteApi };

export default api;
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function getIntegrationRequest(
integrationTypeId
})
});

return serverFetch<IIntegration>({
path: `/integration?${query.toString()}`,
method: 'GET',
Expand Down

0 comments on commit 9f5db19

Please sign in to comment.