Skip to content

Commit

Permalink
Feat: #200 프로젝트 생성 팀원 검색 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-bear98 committed Oct 23, 2024
1 parent 278caba commit 1a94ffc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
20 changes: 19 additions & 1 deletion src/hooks/query/useTeamQuery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { generateTeamCoworkersQueryKey, generateTeamsQueryKey } from '@utils/queryKeyGenerator';

import { getTeamList } from '@services/userService';
import {
acceptTeamInvitation,
Expand All @@ -13,6 +12,7 @@ import {
leaveTeam,
removeTeamMember,
updateTeamInfo,
findUserByTeam,
} from '@services/teamService';
import useToast from '@hooks/useToast';
import { useMemo } from 'react';
Expand Down Expand Up @@ -285,3 +285,21 @@ export function useReadTeam(teamId: Team['teamId']) {

return team;
}

// // 팀원 검색 목록 조회
// export function useSearchTeamCoworker(teamId: Team['teamId'], nickname: User['nickname']) {
// const {
// data: searchedCoworkers = [] as TeamCoworker[],
// isLoading,
// isError,
// } = useQuery({
// queryKey: generateSearchTeamCoworkersQueryKey(teamId, nickname),
// queryFn: async () => {
// const { data } = await findUserByTeam(teamId, nickname);
// return data;
// },
// enabled: !!nickname,
// });

// return { searchedCoworkers, isLoading, isError };
// }
32 changes: 29 additions & 3 deletions src/mocks/services/teamServiceHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,46 @@ import {
USER_DUMMY,
} from '@mocks/mockData';
import type { TeamCoworkerForm, TeamForm } from '@/types/TeamType';
import { convertTokenToUserId } from '@/utils/converter';
import { findAllTeamUsers, findTeamUser, findUser } from '../mockAPI';
import { SearchUser } from '@/types/UserType';

const BASE_URL = import.meta.env.VITE_BASE_URL;

const teamServiceHandler = [
// 팀 소속 유저 검색 API
http.get(`${BASE_URL}/team/:teamId/user/search`, ({ request, params }) => {
const url = new URL(request.url);
const nickname = url.searchParams.get('nickname');
const nickname = url.searchParams.get('nickname') || '';
const accessToken = request.headers.get('Authorization');
const { teamId } = params;
const teamId = Number(params.teamId);

// 유저 인증 확인
if (!accessToken) return new HttpResponse(null, { status: 401 });

return HttpResponse.json([]);
// 유저 ID 정보 취득
const userId = convertTokenToUserId(accessToken);
if (!userId) return new HttpResponse(null, { status: 401 });

// 유저의 팀 접근 권한 확인
const teamUser = findTeamUser(teamId, userId);
if (!teamUser) return new HttpResponse(null, { status: 403 });

// 팀에 속한 모든 유저 검색
const teamUsers = findAllTeamUsers(teamId);
const searchUsers: SearchUser[] = [];

// 팀 유저 정보 취득
for (let i = 0; i < teamUsers.length; i++) {
const user = findUser(teamUsers[i].userId);
if (!user) return new HttpResponse(null, { status: 404 });
searchUsers.push({ userId: user.userId, nickname: user.nickname });
}

// 접두사(nickname)과 일치하는 유저 정보 최대 5명 추출
const matchedSearchUsers = searchUsers.filter((user) => user.nickname.startsWith(nickname)).slice(0, 5);

return HttpResponse.json(matchedSearchUsers);
}),
// 팀 생성 API
http.post(`${BASE_URL}/team`, async ({ request }) => {
Expand Down

0 comments on commit 1a94ffc

Please sign in to comment.