Skip to content

Commit

Permalink
Merge pull request #212 from coffee-meet/refactor/#209
Browse files Browse the repository at this point in the history
Refactor#209 인증 도메인 구조 변경
  • Loading branch information
smartandhandsome authored Dec 21, 2023
2 parents 1960414 + ad483d5 commit 41c9360
Show file tree
Hide file tree
Showing 62 changed files with 949 additions and 531 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import coffeemeet.server.admin.implement.AdminQuery;
import coffeemeet.server.certification.implement.CertificationCommand;
import coffeemeet.server.certification.implement.CertificationQuery;
import coffeemeet.server.common.implement.FCMNotificationSender;
import coffeemeet.server.common.infrastructure.FCMNotificationSender;
import coffeemeet.server.inquiry.domain.Inquiry;
import coffeemeet.server.inquiry.implement.InquiryCommand;
import coffeemeet.server.inquiry.implement.InquiryQuery;
Expand Down Expand Up @@ -38,7 +38,7 @@ public void login(String id, String password) {
}

public void approveCertification(Long certificationId) {
certificationCommand.certificated(certificationId);
certificationCommand.completeCertification(certificationId);

Long userId = certificationQuery.getUserIdByCertificationId(certificationId);
NotificationInfo notificationInfo = userQuery.getNotificationInfoByUserId(userId);
Expand All @@ -48,7 +48,7 @@ public void approveCertification(Long certificationId) {
}

public void rejectCertification(Long certificationId) {
certificationCommand.deleteCertification(certificationId);
certificationCommand.deleteCertificationByUserId(certificationId);

Long userId = certificationQuery.getUserIdByCertificationId(certificationId);
NotificationInfo notificationInfo = userQuery.getNotificationInfoByUserId(userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,16 @@ public class Certification extends AdvancedBaseEntity {
@Column(nullable = false)
private String companyName;

@Embedded
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private Department department;

@Embedded
private CompanyEmail companyEmail;

@Column(nullable = false)
private String businessCardUrl;

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private Department department;

@Column(nullable = false)
private boolean isCertificated;

Expand All @@ -64,4 +63,12 @@ public void qualify() {
isCertificated = true;
}

public void update(String companyName, CompanyEmail companyEmail, String businessCardUrl,
Department department) {
this.companyName = companyName;
this.companyEmail = companyEmail;
this.businessCardUrl = businessCardUrl;
this.department = department;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
import org.springframework.data.redis.core.RedisHash;

@Getter
@RedisHash(value = "email_verification", timeToLive = 360)
public class EmailVerification {
@RedisHash(value = "email_verification", timeToLive = 300)
public class VerificationInfo {

@Id
private Long userId;
private CompanyEmail companyEmail;
private String code;
private String verificationCode;
private LocalDateTime createdAt;

public EmailVerification(@NonNull Long userId, @NonNull CompanyEmail companyEmail,
@NonNull String code) {
public VerificationInfo(@NonNull Long userId, @NonNull CompanyEmail companyEmail,
@NonNull String verificationCode) {
this.userId = userId;
this.companyEmail = companyEmail;
this.code = code;
this.verificationCode = verificationCode;
this.createdAt = LocalDateTime.now();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package coffeemeet.server.certification.infrastructure;
package coffeemeet.server.certification.domain.repository;

import coffeemeet.server.certification.domain.Certification;
import coffeemeet.server.certification.domain.CompanyEmail;
Expand All @@ -14,6 +14,8 @@ public interface CertificationRepository extends JpaRepository<Certification, Lo

boolean existsByCompanyEmail(CompanyEmail companyEmail);

void deleteByUserId(Long userId);

@Query("SELECT c FROM Certification c JOIN FETCH c.user WHERE c.isCertificated = false")
Page<Certification> findPendingCertifications(Pageable pageable);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package coffeemeet.server.certification.domain.repository;

import coffeemeet.server.certification.domain.VerificationInfo;
import org.springframework.data.repository.CrudRepository;

public interface VerificationInfoRepository extends CrudRepository<VerificationInfo, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package coffeemeet.server.certification.implement;

import static coffeemeet.server.common.domain.S3KeyPrefix.BUSINESS_CARD;

import coffeemeet.server.certification.domain.Certification;
import coffeemeet.server.common.implement.ImageDeleter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class BusinessCardImageDeleter {

private final CertificationQuery certificationQuery;
private final ImageDeleter imageDeleter;

public void deleteBusinessCardImageByUserId(Long userId) {
Certification certification = certificationQuery.getCertificationByUserId(userId);
imageDeleter.deleteImage(certification.getBusinessCardUrl(), BUSINESS_CARD);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package coffeemeet.server.certification.implement;

import static coffeemeet.server.common.domain.S3KeyPrefix.BUSINESS_CARD;

import coffeemeet.server.common.implement.ImageUploader;
import java.io.File;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class BusinessCardImageUploader {

private final ImageUploader imageUploader;

public String uploadBusinessCardImage(File businessCardImage) {
return imageUploader.uploadImage(businessCardImage, BUSINESS_CARD);
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package coffeemeet.server.certification.implement;

import static coffeemeet.server.certification.exception.CertificationErrorCode.EXISTED_COMPANY_EMAIL;

import coffeemeet.server.certification.domain.Certification;
import coffeemeet.server.certification.domain.CompanyEmail;
import coffeemeet.server.certification.domain.Department;
import coffeemeet.server.certification.infrastructure.CertificationRepository;
import coffeemeet.server.common.execption.InvalidInputException;
import coffeemeet.server.certification.domain.repository.CertificationRepository;
import coffeemeet.server.user.domain.User;
import java.util.function.Consumer;
import coffeemeet.server.user.implement.UserQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -18,13 +15,13 @@
@RequiredArgsConstructor
public class CertificationCommand {

private static final String EXISTED_COMPANY_EMAIL_MESSAGE = "이미 사용 중인 회사 이메일(%s) 입니다.";

private final CertificationRepository certificationRepository;
private final CertificationQuery certificationQuery;
private final UserQuery userQuery;

public void createCertification(User user, String companyName, CompanyEmail companyEmail,
public void createCertification(Long userId, String companyName, CompanyEmail companyEmail,
Department department, String businessCardUrl) {
User user = userQuery.getUserById(userId);
certificationRepository.save(
Certification.builder()
.companyName(companyName)
Expand All @@ -36,25 +33,19 @@ public void createCertification(User user, String companyName, CompanyEmail comp
);
}

public void hasDuplicatedCompanyEmail(CompanyEmail companyEmail) {
if (certificationRepository.existsByCompanyEmail(companyEmail)) {
throw new InvalidInputException(EXISTED_COMPANY_EMAIL,
String.format(EXISTED_COMPANY_EMAIL_MESSAGE, companyEmail.getValue()));
}
public void updateCertification(Long userId, String companyName, CompanyEmail companyEmail,
Department department, String businessCardImageUrl) {
Certification certification = certificationQuery.getCertificationByUserId(userId);
certification.update(companyName, companyEmail, businessCardImageUrl, department);
}

public void certificated(Long userId) {
public void completeCertification(Long userId) {
Certification certification = certificationQuery.getCertificationByUserId(userId);
certification.qualify();
}

@Transactional(readOnly = true)
public void applyIfCertifiedUser(Long userId, Consumer<? super Certification> consumer) {
certificationRepository.findByUserId(userId).ifPresent(consumer);
}

public void deleteCertification(Long userId) {
certificationRepository.deleteById(userId);
public void deleteCertificationByUserId(Long userId) {
certificationRepository.deleteByUserId(userId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import static coffeemeet.server.certification.exception.CertificationErrorCode.CERTIFICATION_NOT_FOUND;

import coffeemeet.server.certification.domain.Certification;
import coffeemeet.server.certification.infrastructure.CertificationRepository;
import coffeemeet.server.certification.domain.CompanyEmail;
import coffeemeet.server.certification.domain.repository.CertificationRepository;
import coffeemeet.server.common.execption.InvalidInputException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -33,6 +34,10 @@ public String getCompanyNameByUserId(Long userId) {
).getCompanyName();
}

public boolean isExistedCompanyEmail(CompanyEmail companyEmail) {
return certificationRepository.existsByCompanyEmail(companyEmail);
}

public Long getUserIdByCertificationId(Long certificationId) {
return certificationId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package coffeemeet.server.certification.implement;

import static coffeemeet.server.certification.exception.CertificationErrorCode.EXISTED_COMPANY_EMAIL;

import coffeemeet.server.certification.domain.CompanyEmail;
import coffeemeet.server.common.execption.InvalidInputException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class CompanyEmailValidator {

private static final String EXISTED_COMPANY_EMAIL_MESSAGE = "이미 사용 중인 회사 이메일(%s) 입니다.";

private final CertificationQuery certificationQuery;

public void validateDuplicatedCompanyEmail(CompanyEmail companyEmail) {
if (certificationQuery.isExistedCompanyEmail(companyEmail)) {
throw new InvalidInputException(EXISTED_COMPANY_EMAIL,
String.format(EXISTED_COMPANY_EMAIL_MESSAGE, companyEmail.getValue()));
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package coffeemeet.server.certification.implement;

import java.util.random.RandomGenerator;
import org.springframework.stereotype.Component;

@Component
public class VerificationCodeGenerator {

private static final RandomGenerator RANDOM_GENERATOR = RandomGenerator.getDefault();

public String generateVerificationCode() {
return String.format("%06d", RANDOM_GENERATOR.nextInt(1000000));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package coffeemeet.server.certification.implement;

import static coffeemeet.server.certification.exception.CertificationErrorCode.INVALID_VERIFICATION_CODE;

import coffeemeet.server.common.execption.InvalidInputException;
import org.springframework.stereotype.Component;

@Component
public class VerificationCodeValidator {

private static final String WRONG_VERIFICATION_CODE_MESSAGE = "잘못된 인증코드(%s)를 입력했습니다.";

public void validateVerificationCode(String verificationCode, String userInputCode) {
if (!userInputCode.equals(verificationCode)) {
throw new InvalidInputException(INVALID_VERIFICATION_CODE,
String.format(WRONG_VERIFICATION_CODE_MESSAGE, verificationCode));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package coffeemeet.server.certification.implement;

import coffeemeet.server.certification.domain.CompanyEmail;
import coffeemeet.server.certification.domain.VerificationInfo;
import coffeemeet.server.certification.domain.repository.VerificationInfoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class VerificationInfoCommand {

private final VerificationInfoRepository verificationInfoRepository;

public void createVerificationInfo(Long userId, CompanyEmail companyEmail,
String verificationCode) {
verificationInfoRepository.save(new VerificationInfo(userId, companyEmail, verificationCode));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@

import static coffeemeet.server.certification.exception.CertificationErrorCode.VERIFICATION_CODE_NOT_FOUND;

import coffeemeet.server.certification.domain.EmailVerification;
import coffeemeet.server.certification.infrastructure.EmailVerificationRepository;
import coffeemeet.server.certification.domain.VerificationInfo;
import coffeemeet.server.certification.domain.repository.VerificationInfoRepository;
import coffeemeet.server.common.execption.InvalidInputException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class EmailVerificationQuery {
public class VerificationInfoQuery {

private static final String VERIFICATION_CODE_NOT_FOUND_MESSAGE = "인증코드 기간이 만료되었거나 해당 유저(%s)가 인증코드를 요청한 기록이 없습니다.";

private final EmailVerificationRepository emailVerificationRepository;
private final VerificationInfoRepository verificationInfoRepository;

public String getCodeById(Long userId) {
EmailVerification emailVerification = emailVerificationRepository.findById(userId)
public String getVerificationCodeById(Long userId) {
VerificationInfo verificationInfo = verificationInfoRepository.findById(userId)
.orElseThrow(() -> new InvalidInputException(
VERIFICATION_CODE_NOT_FOUND,
String.format(VERIFICATION_CODE_NOT_FOUND_MESSAGE, userId)));
return emailVerification.getCode();
return verificationInfo.getVerificationCode();
}

}
Loading

0 comments on commit 41c9360

Please sign in to comment.