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

Refactor: #310 MSW 처리중 userServiceHandler 기능 리팩토링 #314

Merged
merged 3 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
77 changes: 68 additions & 9 deletions src/mocks/mockAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
FILE_DUMMY,
PROFILE_IMAGE_DUMMY,
PROJECT_DUMMY,
PROJECT_USER_DUMMY,
ROLE_DUMMY,
Expand All @@ -13,15 +14,21 @@ import {
} from '@mocks/mockData';

import type { Role } from '@/types/RoleType';
import type { User } from '@/types/UserType';
import type { EditUserInfoForm, EditUserLinksForm, User } from '@/types/UserType';
import type { Team, TeamInfoForm } from '@/types/TeamType';
import type { Project, ProjectInfoForm } from '@/types/ProjectType';
import type { ProjectStatus, ProjectStatusForm } from '@/types/ProjectStatusType';
import type { Task, TaskUpdateForm } from '@/types/TaskType';
import type { ProjectUser, TaskFileForMemory, TaskUser, TeamUser, UploadTaskFile } from '@/types/MockType';
import type {
ProfileFileForMemory,
ProjectUser,
TaskFileForMemory,
TaskUser,
TeamUser,
UploadTaskFile,
} from '@/types/MockType';

/* ===================== 역할(Role) 관련 처리 ===================== */

// 역할 조회
export function findRole(roleId: Role['roleId']) {
return ROLE_DUMMY.find((role) => role.roleId === roleId);
Expand All @@ -33,12 +40,41 @@ export function findRoleByRoleName(roleName: Role['roleName']) {
}

/* ===================== 유저(User) 관련 처리 ===================== */

// 유저 조회
export function findUser(userId: User['userId']) {
return USER_DUMMY.find((user) => user.userId === userId);
}

// 유저 정보 수정
export function updateUserInfo(userId: User['userId'], updatedUserInfo: EditUserInfoForm) {
const user = findUser(userId);
if (!user) throw new Error('해당 사용자를 찾을 수 없습니다. 입력 정보를 확인해 주세요.');

const { nickname, bio } = updatedUserInfo;
user.nickname = nickname;
user.bio = bio;
}

// 유저 링크 수정
export function updateUserLinks(userId: User['userId'], updatedUserLinks: EditUserLinksForm) {
const user = findUser(userId);
if (!user) throw new Error('해당 사용자를 찾을 수 없습니다. 입력 정보를 확인해 주세요.');
user.links = updatedUserLinks.links;
}

// 유저 프로필 수정
export function updateUserProfile(userId: User['userId'], uploadName: string) {
const user = findUser(userId);
if (!user) throw new Error('해당 사용자를 찾을 수 없습니다. 입력 정보를 확인해 주세요.');
user.fileName = uploadName;
}

// 유저 프로필 삭제
export function deleteUserProfile(userId: User['userId']) {
const user = findUser(userId);
if (!user) throw new Error('해당 사용자를 찾을 수 없습니다. 입력 정보를 확인해 주세요.');
user.fileName = null;
}
/* ============= 팀에 연결된 유저(Team User) 관련 처리 ============= */
// 팀과 연결된 유저 생성
export function createTeamUser(newTeamUser: TeamUser) {
Expand All @@ -50,8 +86,13 @@ export function findTeamUser(teamId: Team['teamId'], userId: User['userId']) {
return TEAM_USER_DUMMY.find((teamUser) => teamUser.teamId === teamId && teamUser.userId === userId);
}

// 유저가 속한 모든 팀 조회
export function findAllTeamUsersByUserId(userId: User['userId']) {
return TEAM_USER_DUMMY.filter((teamUser) => teamUser.userId === userId);
}

// 팀에 속한 모든 유저 조회
export function findAllTeamUsers(teamId: Team['teamId']) {
export function findAllTeamUsersByTeamId(teamId: Team['teamId']) {
return TEAM_USER_DUMMY.filter((teamUser) => teamUser.teamId === teamId);
}

Expand Down Expand Up @@ -172,8 +213,8 @@ export function deleteAllProjectUser(projectId: Project['projectId']) {
PROJECT_USER_DUMMY.push(...filteredProjectUsers);
}
}
/* ================= 프로젝트(Project) 관련 처리 ================= */

/* ================= 프로젝트(Project) 관련 처리 ================= */
// 프로젝트 생성
export function createProject(newProject: Project) {
PROJECT_DUMMY.push(newProject);
Expand Down Expand Up @@ -209,7 +250,6 @@ export function deleteProject(projectId: Project['projectId']) {
}

/* ================ 프로젝트 상태(Status) 관련 처리 ================ */

// 프로젝트 상태 정보 생성
export function createProjectStatus(newStatus: ProjectStatus) {
STATUS_DUMMY.push(newStatus);
Expand Down Expand Up @@ -262,7 +302,6 @@ export function reorderStatusByProject(projectId: Project['projectId']) {
}

/* ============ 일정에 연결된 유저(Task User) 관련 처리 ============ */

// 일정과 연결된 모든 유저 삭제
export function deleteAllTaskUser(taskId: Task['taskId']) {
const filteredTaskUsers = TASK_USER_DUMMY.filter((taskUser) => taskUser.taskId !== taskId);
Expand All @@ -284,7 +323,6 @@ export function deleteTaskUser(taskId: Task['taskId'], userId: number) {
}

/* ===================== 일정(Task) 관련 처리 ===================== */

// 일정 추가
export function createTask(task: Task) {
TASK_DUMMY.push(task);
Expand Down Expand Up @@ -400,6 +438,27 @@ export function reorderTaskByStatus(statusId: ProjectStatus['statusId']) {
}

/* =============== 업로드 파일 임시 저장 관련 처리 =============== */
// 업로드된 프로필 파일을 메모리 임시 저장
export function saveUserProfileFileInMemory(userId: User['userId'], profileInfo: ProfileFileForMemory) {
const profileImageIndex = PROFILE_IMAGE_DUMMY.findIndex((user) => user.userId === userId);
if (profileImageIndex !== -1) {
PROFILE_IMAGE_DUMMY[profileImageIndex].uploadName = profileInfo.uploadName;
} else {
PROFILE_IMAGE_DUMMY.push(profileInfo);
}
}

// 임시 저장된 프로필 파일 조회
export function downloadProfileFileInMemory(uploadName: ProfileFileForMemory['uploadName']) {
return PROFILE_IMAGE_DUMMY.find((file) => file.uploadName === uploadName);
}

// 임시 저장된 프로필 파일 삭제
export function deleteProfileFileInMemory(userId: User['userId']) {
const fileIndex = PROFILE_IMAGE_DUMMY.findIndex((file) => file.userId === userId);
if (fileIndex === -1) throw new Error('삭제할 프로필 이미지가 없습니다.');
PROFILE_IMAGE_DUMMY.splice(fileIndex, 1);
}

// 업로드된 일정 파일을 메모리 임시 저장
export function saveTaskFileInMemory(taskFile: TaskFileForMemory) {
Expand Down
19 changes: 9 additions & 10 deletions src/mocks/mockData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import type { Project } from '@/types/ProjectType';
import type { ProjectStatus } from '@/types/ProjectStatusType';
import type { Task } from '@/types/TaskType';
import type { Role } from '@/types/RoleType';
import type { ProjectUser, TaskFileForMemory, TaskUser, TeamUser, UploadTaskFile } from '@/types/MockType';

type ImageInfo = {
userId: number;
file: Blob;
uploadName: string;
};

export const JWT_TOKEN_DUMMY = 'mocked-header.mocked-payload-4.mocked-signature';
import type {
ProfileFileForMemory,
ProjectUser,
TaskFileForMemory,
TaskUser,
TeamUser,
UploadTaskFile,
} from '@/types/MockType';

export const VERIFICATION_CODE_DUMMY = '1234';
export const TEMP_PASSWORD_DUMMY = '!1p2l3nqlz';
Expand Down Expand Up @@ -814,4 +813,4 @@ export const FILE_DUMMY: TaskFileForMemory[] = [
];

// MSW 프로필 이미지 임시 저장을 위한 변수
export const PROFILE_IMAGE_DUMMY: ImageInfo[] = [];
export const PROFILE_IMAGE_DUMMY: ProfileFileForMemory[] = [];
49 changes: 0 additions & 49 deletions src/mocks/mockHash.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/mocks/services/teamServiceHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
findAllProject,
findAllProjectStatus,
findAllTask,
findAllTeamUsers,
findAllTeamUsersByTeamId,
findRole,
findRoleByRoleName,
findTeamUser,
Expand Down Expand Up @@ -54,7 +54,7 @@ const teamServiceHandler = [
if (!teamUser) return new HttpResponse(null, { status: 403 });

// 팀에 참여하고 있는 모든 유저 검색
const teamUsers = findAllTeamUsers(teamId).filter((teamUser) => teamUser.isPendingApproval === false);
const teamUsers = findAllTeamUsersByTeamId(teamId).filter((teamUser) => teamUser.isPendingApproval === false);
const searchUsers: SearchUser[] = [];

// 팀 유저 정보 취득
Expand Down Expand Up @@ -259,7 +259,7 @@ const teamServiceHandler = [
if (!userId) return new HttpResponse(null, { status: 401 });

// 모든 팀원 조회
const teamUsers = findAllTeamUsers(teamId);
const teamUsers = findAllTeamUsersByTeamId(teamId);

// 팀원 정보 조회
const memberInfo = teamUsers.map((teamUser) => {
Expand Down
Loading
Loading