Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import com.chaineeproject.chainee.exception.ApplicationException;
import com.chaineeproject.chainee.exception.ErrorCode;
import com.chaineeproject.chainee.repository.*;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
@Slf4j
public class JobApplicationService {

private final UserRepository userRepository;
Expand All @@ -25,27 +27,22 @@ public class JobApplicationService {

@Transactional
public void applyToJob(Long postId, Long applicantId, Long resumeId) {
// 1) 지원자 조회(JWT uid로 강제)
User applicant = userRepository.findById(applicantId)
.orElseThrow(() -> new ApplicationException(ErrorCode.APPLICANT_NOT_FOUND));

// 2) 공고 조회
JobPost post = jobPostRepository.findById(postId)
.orElseThrow(() -> new ApplicationException(ErrorCode.JOB_POST_NOT_FOUND));

// 3) 이력서 조회 + 소유권 검증(남의 이력서로 지원 방지)
Resume resume = resumeRepository.findById(resumeId)
.orElseThrow(() -> new ApplicationException(ErrorCode.RESUME_NOT_FOUND));
if (resume.getOwner() == null || !resume.getOwner().getId().equals(applicantId)) {
throw new ApplicationException(ErrorCode.FORBIDDEN); // 또는 전용 에러코드 정의 가능: NOT_OWNER_OF_RESUME
throw new ApplicationException(ErrorCode.FORBIDDEN);
}

// 4) 중복 지원 방지
if (jobApplicationRepository.existsByPostIdAndApplicantId(postId, applicantId)) {
throw new ApplicationException(ErrorCode.DUPLICATE_APPLICATION);
}

// 5) 저장
JobApplication application = new JobApplication();
application.setPost(post);
application.setApplicant(applicant);
Expand All @@ -56,7 +53,7 @@ public void applyToJob(Long postId, Long applicantId, Long resumeId) {
jobApplicationRepository.save(application);
jobPostRepository.incrementApplicantCount(postId);

// 6) 이벤트(알림용)
log.debug("Publishing JobApplicationCreatedEvent: {}", application.getId());
eventPublisher.publishEvent(new JobApplicationCreatedEvent(application.getId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;
import org.springframework.transaction.event.TransactionPhase;
import lombok.extern.slf4j.Slf4j;

@Service
@RequiredArgsConstructor
@Slf4j
public class NotificationService {

private final NotificationRepository notificationRepository;
Expand Down