From f93b1d0df8ad358e96a07989611d5ea5de1f40ac Mon Sep 17 00:00:00 2001 From: nael Date: Sat, 27 Jul 2024 06:52:34 +0200 Subject: [PATCH] :rotating_light: Fix lint in packages/shared --- .../src/hooks/queries/useLinkedUser.tsx | 17 --- apps/magic-link/src/hooks/useOAuth.ts | 5 +- apps/magic-link/src/lib/ProviderModal.tsx | 17 ++- .../hooks/create/useCreateBatchLinkedUser.tsx | 2 +- .../src/hooks/create/useCreateLinkedUser.tsx | 2 +- apps/webapp/src/hooks/get/useLinkedUsers.tsx | 2 +- .../dto/create-linked-user.dto.ts | 4 - .../linked-users/linked-users.controller.ts | 106 ++++++++++++++++-- .../linked-users/linked-users.service.ts | 12 +- packages/shared/src/authUrl.ts | 7 +- packages/shared/src/types.ts | 2 +- 11 files changed, 130 insertions(+), 46 deletions(-) delete mode 100644 apps/magic-link/src/hooks/queries/useLinkedUser.tsx diff --git a/apps/magic-link/src/hooks/queries/useLinkedUser.tsx b/apps/magic-link/src/hooks/queries/useLinkedUser.tsx deleted file mode 100644 index 097b96e82..000000000 --- a/apps/magic-link/src/hooks/queries/useLinkedUser.tsx +++ /dev/null @@ -1,17 +0,0 @@ -import { useQuery } from '@tanstack/react-query'; -import { linked_users as LinkedUser } from 'api'; -import config from '@/helpers/config'; - -const useLinkedUser = (id: string) => { - return useQuery({ - queryKey: ['linked-users', id], - queryFn: async (): Promise => { - const response = await fetch(`${config.API_URL}/linked-users/single?id=${id}`); - if (!response.ok) { - throw new Error('Network response was not ok'); - } - return response.json(); - } - }); -}; -export default useLinkedUser; diff --git a/apps/magic-link/src/hooks/useOAuth.ts b/apps/magic-link/src/hooks/useOAuth.ts index 50e6f6012..aa19751e2 100644 --- a/apps/magic-link/src/hooks/useOAuth.ts +++ b/apps/magic-link/src/hooks/useOAuth.ts @@ -9,7 +9,10 @@ type UseOAuthProps = { returnUrl: string; // Return URL after OAuth flow projectId: string; // Project ID linkedUserId: string; // Linked User ID - redirectIngressUri: string | null; // URL of the User's Server + redirectIngressUri: { + status: boolean; + value: string | null; + } // URL of the User's Server onSuccess: () => void; }; diff --git a/apps/magic-link/src/lib/ProviderModal.tsx b/apps/magic-link/src/lib/ProviderModal.tsx index c3d7e0cfd..f91cf3f06 100644 --- a/apps/magic-link/src/lib/ProviderModal.tsx +++ b/apps/magic-link/src/lib/ProviderModal.tsx @@ -63,7 +63,13 @@ const ProviderModal = () => { const [openSuccessDialog,setOpenSuccessDialog] = useState(false); const [currentProviderLogoURL,setCurrentProviderLogoURL] = useState('') const [currentProvider,setCurrentProvider] = useState('') - const [redirectIngressUri, setRedirectIngressUri] = useState(null); + const [redirectIngressUri, setRedirectIngressUri] = useState<{ + status: boolean; + value: string | null; + }>({ + status: false, + value: null + }); const {mutate : createApiKeyConnection} = useCreateApiKeyConnection(); const {data: magicLink} = useUniqueMagicLink(uniqueMagicLinkId); const {data: connectorsForProject} = useProjectConnectors(isProjectIdReady ? projectId : null); @@ -88,9 +94,12 @@ const ProviderModal = () => { useEffect(() => { const queryParams = new URLSearchParams(window.location.search); - const redirectIngressUri = queryParams.get('redirectIngressUri'); - if (redirectIngressUri) { - setRedirectIngressUri(redirectIngressUri); + const param = queryParams.get('redirectIngressUri'); + if (param !== null && param !== undefined) { + setRedirectIngressUri({ + status: true, + value: param + }); } }, []); diff --git a/apps/webapp/src/hooks/create/useCreateBatchLinkedUser.tsx b/apps/webapp/src/hooks/create/useCreateBatchLinkedUser.tsx index 383c03cab..551188c46 100644 --- a/apps/webapp/src/hooks/create/useCreateBatchLinkedUser.tsx +++ b/apps/webapp/src/hooks/create/useCreateBatchLinkedUser.tsx @@ -9,7 +9,7 @@ interface ILinkedUserDto { } const useCreateBatchLinkedUser = () => { const add = async (linkedUserData: ILinkedUserDto) => { - const response = await fetch(`${config.API_URL}/linked-users/batch`, { + const response = await fetch(`${config.API_URL}/linked-users/internal/batch`, { method: 'POST', body: JSON.stringify(linkedUserData), headers: { diff --git a/apps/webapp/src/hooks/create/useCreateLinkedUser.tsx b/apps/webapp/src/hooks/create/useCreateLinkedUser.tsx index e2cdfe666..e7c18aa76 100644 --- a/apps/webapp/src/hooks/create/useCreateLinkedUser.tsx +++ b/apps/webapp/src/hooks/create/useCreateLinkedUser.tsx @@ -9,7 +9,7 @@ interface ILinkedUserDto { } const useCreateLinkedUser = () => { const add = async (linkedUserData: ILinkedUserDto) => { - const response = await fetch(`${config.API_URL}/linked-users`, { + const response = await fetch(`${config.API_URL}/linked-users/internal`, { method: 'POST', body: JSON.stringify(linkedUserData), headers: { diff --git a/apps/webapp/src/hooks/get/useLinkedUsers.tsx b/apps/webapp/src/hooks/get/useLinkedUsers.tsx index 54405212d..bf7181e78 100644 --- a/apps/webapp/src/hooks/get/useLinkedUsers.tsx +++ b/apps/webapp/src/hooks/get/useLinkedUsers.tsx @@ -7,7 +7,7 @@ const useLinkedUsers = () => { return useQuery({ queryKey: ['linked-users'], queryFn: async (): Promise => { - const response = await fetch(`${config.API_URL}/linked-users`, + const response = await fetch(`${config.API_URL}/linked-users/internal`, { method: 'GET', headers: { diff --git a/packages/api/src/@core/linked-users/dto/create-linked-user.dto.ts b/packages/api/src/@core/linked-users/dto/create-linked-user.dto.ts index a2251914e..580154844 100644 --- a/packages/api/src/@core/linked-users/dto/create-linked-user.dto.ts +++ b/packages/api/src/@core/linked-users/dto/create-linked-user.dto.ts @@ -5,8 +5,6 @@ export class CreateLinkedUserDto { linked_user_origin_id: string; @ApiProperty() alias: string; - @ApiProperty() - id_project: string; } export class CreateBatchLinkedUserDto { @@ -14,6 +12,4 @@ export class CreateBatchLinkedUserDto { linked_user_origin_ids: string[]; @ApiProperty() alias: string; - @ApiProperty() - id_project: string; } diff --git a/packages/api/src/@core/linked-users/linked-users.controller.ts b/packages/api/src/@core/linked-users/linked-users.controller.ts index 7d6d9e512..3206de2f7 100644 --- a/packages/api/src/@core/linked-users/linked-users.controller.ts +++ b/packages/api/src/@core/linked-users/linked-users.controller.ts @@ -15,12 +15,14 @@ import { } from './dto/create-linked-user.dto'; import { ApiBody, + ApiExcludeEndpoint, ApiOperation, ApiQuery, ApiResponse, ApiTags, } from '@nestjs/swagger'; import { JwtAuthGuard } from '@@core/auth/guards/jwt-auth.guard'; +import { ApiKeyAuthGuard } from '@@core/auth/guards/api-key.guard'; @ApiTags('linkedUsers') @Controller('linked-users') @@ -35,10 +37,17 @@ export class LinkedUsersController { @ApiOperation({ operationId: 'createLinkedUser', summary: 'Add Linked User' }) @ApiBody({ type: CreateLinkedUserDto }) @ApiResponse({ status: 201 }) - @UseGuards(JwtAuthGuard) + @UseGuards(ApiKeyAuthGuard) @Post() - addLinkedUser(@Body() linkedUserCreateDto: CreateLinkedUserDto) { - return this.linkedUsersService.addLinkedUser(linkedUserCreateDto); + addLinkedUser( + @Request() req: any, + @Body() linkedUserCreateDto: CreateLinkedUserDto, + ) { + const projectId = req.user.id_project; + return this.linkedUsersService.addLinkedUser( + linkedUserCreateDto, + projectId, + ); } @ApiOperation({ @@ -47,10 +56,14 @@ export class LinkedUsersController { }) @ApiBody({ type: CreateBatchLinkedUserDto }) @ApiResponse({ status: 201 }) - @UseGuards(JwtAuthGuard) + @UseGuards(ApiKeyAuthGuard) @Post('batch') - addBatchLinkedUsers(@Body() data: CreateBatchLinkedUserDto) { - return this.linkedUsersService.addBatchLinkedUsers(data); + addBatchLinkedUsers( + @Request() req: any, + @Body() data: CreateBatchLinkedUserDto, + ) { + const projectId = req.user.id_project; + return this.linkedUsersService.addBatchLinkedUsers(data, projectId); } @ApiOperation({ @@ -58,7 +71,7 @@ export class LinkedUsersController { summary: 'Retrieve Linked Users', }) @ApiResponse({ status: 200 }) - @UseGuards(JwtAuthGuard) + @UseGuards(ApiKeyAuthGuard) @Get() fetchLinkedUsers(@Request() req: any) { const { id_project } = req.user; @@ -71,7 +84,7 @@ export class LinkedUsersController { }) @ApiQuery({ name: 'id', required: true, type: String }) @ApiResponse({ status: 200 }) - @UseGuards(JwtAuthGuard) + @UseGuards(ApiKeyAuthGuard) @Get('single') getLinkedUser(@Query('id') id: string) { // validate project_id against user @@ -84,10 +97,85 @@ export class LinkedUsersController { }) @ApiQuery({ name: 'remoteId', required: true, type: String }) @ApiResponse({ status: 200 }) - @UseGuards(JwtAuthGuard) + @UseGuards(ApiKeyAuthGuard) @Get('fromRemoteId') linkedUserFromRemoteId(@Query('remoteId') id: string) { // validate project_id against user return this.linkedUsersService.getLinkedUserV2(id); } + + @ApiOperation({ operationId: 'createLinkedUser', summary: 'Add Linked User' }) + @ApiBody({ type: CreateLinkedUserDto }) + @ApiResponse({ status: 201 }) + @ApiExcludeEndpoint() + @UseGuards(JwtAuthGuard) + @Post('internal') + addLinkedUserInternal( + @Request() req: any, + @Body() linkedUserCreateDto: CreateLinkedUserDto, + ) { + const { id_project } = req.user; + return this.linkedUsersService.addLinkedUser( + linkedUserCreateDto, + id_project, + ); + } + + @ApiOperation({ + operationId: 'importBatch', + summary: 'Add Batch Linked Users', + }) + @ApiBody({ type: CreateBatchLinkedUserDto }) + @ApiResponse({ status: 201 }) + @ApiExcludeEndpoint() + @UseGuards(JwtAuthGuard) + @Post('internal/batch') + addBatchLinkedUsersInternal( + @Request() req: any, + @Body() data: CreateBatchLinkedUserDto, + ) { + const { id_project } = req.user; + return this.linkedUsersService.addBatchLinkedUsers(data, id_project); + } + + @ApiOperation({ + operationId: 'listLinkedUsers', + summary: 'Retrieve Linked Users', + }) + @ApiResponse({ status: 200 }) + @UseGuards(JwtAuthGuard) + @ApiExcludeEndpoint() + @Get('internal') + fetchLinkedUsersInternal(@Request() req: any) { + const { id_project } = req.user; + return this.linkedUsersService.getLinkedUsers(id_project); + } + + @ApiOperation({ + operationId: 'retrieveLinkedUser', + summary: 'Retrieve a Linked User', + }) + @ApiQuery({ name: 'id', required: true, type: String }) + @ApiResponse({ status: 200 }) + @UseGuards(JwtAuthGuard) + @ApiExcludeEndpoint() + @Get('internal/single') + getLinkedUserInternal(@Query('id') id: string) { + // validate project_id against user + return this.linkedUsersService.getLinkedUser(id); + } + + @ApiOperation({ + operationId: 'remoteId', + summary: 'Retrieve a Linked User From A Remote Id', + }) + @ApiQuery({ name: 'remoteId', required: true, type: String }) + @ApiResponse({ status: 200 }) + @ApiExcludeEndpoint() + @UseGuards(JwtAuthGuard) + @Get('internal/fromRemoteId') + linkedUserFromRemoteIdInternal(@Query('remoteId') id: string) { + // validate project_id against user + return this.linkedUsersService.getLinkedUserV2(id); + } } diff --git a/packages/api/src/@core/linked-users/linked-users.service.ts b/packages/api/src/@core/linked-users/linked-users.service.ts index fc424bc9e..31465dd50 100644 --- a/packages/api/src/@core/linked-users/linked-users.service.ts +++ b/packages/api/src/@core/linked-users/linked-users.service.ts @@ -45,12 +45,11 @@ export class LinkedUsersService { throw error; } } - async addLinkedUser(data: CreateLinkedUserDto) { + async addLinkedUser(data: CreateLinkedUserDto, id_project: string) { try { - const { id_project, ...rest } = data; const res = await this.prisma.linked_users.create({ data: { - ...rest, + ...data, id_linked_user: uuidv4(), id_project: id_project, }, @@ -60,9 +59,12 @@ export class LinkedUsersService { throw error; } } - async addBatchLinkedUsers(data: CreateBatchLinkedUserDto) { + async addBatchLinkedUsers( + data: CreateBatchLinkedUserDto, + id_project: string, + ) { try { - const { linked_user_origin_ids, alias, id_project } = data; + const { linked_user_origin_ids, alias } = data; const linkedUsersData = linked_user_origin_ids.map((id) => ({ id_linked_user: uuidv4(), // Ensure each user gets a unique ID diff --git a/packages/shared/src/authUrl.ts b/packages/shared/src/authUrl.ts index 4b72d08af..29c8bc734 100644 --- a/packages/shared/src/authUrl.ts +++ b/packages/shared/src/authUrl.ts @@ -10,13 +10,16 @@ interface AuthParams { returnUrl: string; apiUrl: string; vertical: string; - rediectUriIngress?: string | null; + rediectUriIngress?: { + status: boolean; + value: string | null; + }; } // make sure to check wether its api_key or oauth2 to build the right auth // make sure to check if client has own credentials to connect or panora managed ones export const constructAuthUrl = async ({ projectId, linkedUserId, providerName, returnUrl, apiUrl, vertical, rediectUriIngress }: AuthParams) => { - const encodedRedirectUrl = encodeURIComponent(`${rediectUriIngress !== null && rediectUriIngress ? rediectUriIngress : apiUrl}/connections/oauth/callback`); + const encodedRedirectUrl = encodeURIComponent(`${rediectUriIngress && rediectUriIngress.status == true ? rediectUriIngress.value : apiUrl}/connections/oauth/callback`); const state = encodeURIComponent(JSON.stringify({ projectId, linkedUserId, providerName, vertical, returnUrl })); // console.log('State : ', JSON.stringify({ projectId, linkedUserId, providerName, vertical, returnUrl })); // console.log('encodedRedirect URL : ', encodedRedirectUrl); diff --git a/packages/shared/src/types.ts b/packages/shared/src/types.ts index a635174d3..274fbd5f8 100644 --- a/packages/shared/src/types.ts +++ b/packages/shared/src/types.ts @@ -48,4 +48,4 @@ export type VerticalConfig = { export type ProvidersConfig = { [vertical: string]: VerticalConfig; -} \ No newline at end of file +}