Skip to content

Commit

Permalink
Merge pull request #94 from SystemConsultantGroup/fix/93-professor-op…
Browse files Browse the repository at this point in the history
…tional

[FIX] 교수 등록 시 depId 필드 optional로 수정
  • Loading branch information
yesjuhee authored Apr 11, 2024
2 parents 9e1ca4c + 0f180d8 commit d696c52
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
7 changes: 4 additions & 3 deletions src/modules/professors/dtos/create-professor.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class CreateProfessorDto {
required: false,
})
@IsOptional()
@IsNotEmpty({ message: "이메일을 입력해주세요." })
@IsNotEmpty()
@IsEmail()
@Type(() => String)
email: string;
Expand All @@ -48,7 +48,7 @@ export class CreateProfessorDto {
required: false,
})
@IsOptional()
@IsNotEmpty({ message: "연락처를 입력해주세요." })
@IsNotEmpty()
@IsString()
@IsKoreanPhoneNumber()
@Type(() => String)
Expand All @@ -58,7 +58,8 @@ export class CreateProfessorDto {
description: "학과 아이디",
example: 1,
})
@IsNotEmpty({ message: "학과 아이디를 입력해주세요." })
@IsOptional()
@IsNotEmpty()
@Type(() => Number)
deptId: number;
}
2 changes: 1 addition & 1 deletion src/modules/professors/dtos/professor.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class ProfessorDto {
이름: this.name,
이메일: this.email,
연락처: this.phone,
학과: this.department.name,
학과: this.department ? this.department.name : null,
};
}
}
31 changes: 17 additions & 14 deletions src/modules/professors/professors.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ export class ProfessorsService {
}
}

const checkDepartment = await this.prismaService.department.findUnique({
where: { id: deptId },
});

if (!checkDepartment) {
throw new BadRequestException("존재하지 않는 학과입니다.");
if (deptId) {
const checkDepartment = await this.prismaService.department.findUnique({
where: { id: deptId },
});
if (!checkDepartment) {
throw new BadRequestException("존재하지 않는 학과입니다.");
}
}

try {
Expand Down Expand Up @@ -220,12 +221,15 @@ export class ProfessorsService {
professors.map(async (professor, index) => {
const { loginId, name, password, email, phone, departmentName } = professor;

const dept = await tx.department.findFirst({
where: { name: departmentName },
select: { id: true },
});
let dept;
if (departmentName) {
dept = await tx.department.findFirst({
where: { name: departmentName },
select: { id: true },
});

if (!dept) throw new BadRequestException(`${index + 2}번째 줄의 소속학과가 존재하지 않습니다.`);
if (!dept) throw new BadRequestException(`${index + 2}번째 줄의 소속학과가 존재하지 않습니다.`);
}

// 해당 ID가 존재할 경우 업데이트 진행
// 없는 경우 생성 진행
Expand Down Expand Up @@ -269,14 +273,13 @@ export class ProfessorsService {
data: {
name,
phone,
deptId: dept.id,
deptId: dept ? dept.id : undefined,
password: password ? await this.authService.createHash(password) : undefined,
},
include: { department: true },
});
} else {
// 새로 생성하는 유저
if (!departmentName) throw new BadRequestException(`${index + 2}번째 줄의 소속학과를 입력해주세요.`);
if (!name) throw new BadRequestException(`${index + 2}번째 줄의 이름을 입력해주세요.`);
if (!password) throw new BadRequestException(`${index + 2}번째 줄의 비밀번호를 입력해주세요.`);
if (existingEmail)
Expand All @@ -295,7 +298,7 @@ export class ProfessorsService {
name,
email,
phone,
deptId: dept.id,
deptId: dept ? dept.id : undefined,
password: await this.authService.createHash(password),
type: UserType.PROFESSOR,
},
Expand Down

0 comments on commit d696c52

Please sign in to comment.