Skip to content

Commit

Permalink
[feat] : comment TimeStamp change
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackBean99 committed Sep 3, 2023
1 parent 7562e0e commit ee78322
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,30 @@ public void deleteCommentLike(Long commentId) {

@Override
public List<CommentPairVo> findByCardId(Long cardId) {
Long currentUserId = SecurityUtils.getCurrentUserId();
Long idpId = SecurityUtils.getCurrentUserId();
Card card = cardLoadPort.findById(cardId);
List<Comment> comments = commentLoadPort.findByCardId(card.getId());

List<Long> commentIdpIds =
comments.stream().map(Comment::getIdpId).collect(Collectors.toList());

List<Interviewer> interviewers = interviewerLoadPort.loadInterviewerByIdpIds(commentIdpIds);
List<Long> idpIds = comments.stream().map(Comment::getIdpId).collect(Collectors.toList());

List<Interviewer> interviewers = interviewerLoadPort.loadInterviewerByIdpIds(idpIds);
List<CommentLike> commentLikes = commentLikeLoadPort.findByCommentIds(comments.stream().map(Comment::getId).collect(Collectors.toList()));
return comments.stream()
.map(
comment -> {
boolean isLiked = commentLikeLoadPort.getByIdpId(currentUserId);
Boolean canEdit = comment.getIdpId().equals(currentUserId);
String interviewerName =
Boolean isLiked =
commentLikes.stream()
.anyMatch(
commentLike ->
commentLike
.getCommentId()
.equals(comment.getId())
&& commentLike
.getIdpId()
.equals(idpId));

Boolean canEdit = Objects.equals(comment.getIdpId(), idpId);
String interviewersName =
interviewers.stream()
.filter(
interviewer ->
Expand All @@ -157,7 +166,7 @@ public List<CommentPairVo> findByCardId(Long cardId) {
.findFirst()
.map(Interviewer::getName)
.orElse("");
return CommentPairVo.of(comment, isLiked, interviewerName, canEdit);
return CommentPairVo.of(comment, isLiked, interviewersName, canEdit);
})
.collect(Collectors.toList());
}
Expand Down Expand Up @@ -187,9 +196,9 @@ public void updateCommentContent(Long commentId, String content) {
public List<CommentPairVo> findByApplicantId(String applicantId) {
Long idpId = SecurityUtils.getCurrentUserId();
List<Comment> comments = commentLoadPort.findByApplicantId(applicantId);
List<Long> commentIds = comments.stream().map(Comment::getId).collect(Collectors.toList());
List<Interviewer> interviewers = interviewerLoadPort.loadInterviewerByIdpIds(commentIds);
List<CommentLike> commentLikes = commentLikeLoadPort.findByCommentIds(commentIds);
List<Long> idpIds = comments.stream().map(Comment::getIdpId).collect(Collectors.toList());
List<Interviewer> interviewers = interviewerLoadPort.loadInterviewerByIdpIds(idpIds);
List<CommentLike> commentLikes = commentLikeLoadPort.findByCommentIds(comments.stream().map(Comment::getId).collect(Collectors.toList()));
return comments.stream()
.map(
comment -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.econovation.recruit.api.user.helper;

import com.econovation.recruitcommon.annotation.Helper;
import com.econovation.recruitcommon.exception.RecruitDynamicException;
import com.econovation.recruitinfrastructure.ncp.NcpClient;
import com.econovation.recruitinfrastructure.ncp.NcpProperties;
import com.econovation.recruitinfrastructure.ses.RecipientForRequest;
Expand All @@ -11,53 +12,59 @@
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.apache.commons.codec.binary.Base64;

@Helper
@RequiredArgsConstructor
public class NcpMailHelper {
private final NcpProperties ncpProperties;
private final NcpClient ncpClient;

@SneakyThrows
static String method = "POST"; // method
static String space = " "; // space
static String newLine = "\n"; // new line


public void sendMail(String title, String body, String recipientAddress) {
String timeStamp = String.valueOf(Instant.now().toEpochMilli());
ncpClient.sendMail(timeStamp, ncpProperties.getAccessKey(), makeSignature(timeStamp),createSendRawEmailDto(title, body, recipientAddress));
String signature = makeSignature(ncpProperties.getAccessKey(), ncpProperties.getSecretKey(), ncpProperties.getSendUrl(), timeStamp);
SendRawEmailDto sendRawEmailDto = createSendRawEmailDto(title, body, recipientAddress);
ncpClient.sendMail(ncpProperties.getAccessKey(), timeStamp, signature, sendRawEmailDto);
}
public String makeSignature(String timeStamp) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
String space = " "; // 공백
String newLine = "\n"; // 줄바꿈
String method = "POST"; // HTTP 메소드
String url = "/api/v1/mails"; // 도메인을 제외한 "/" 아래 전체 url (쿼리스트링 포함)
String timestamp = timeStamp; // 현재 타임스탬프 (epoch, millisecond)
String accessKey = ncpProperties.getAccessKey(); // access key id (from portal or sub account)
String secretKey = ncpProperties.getSecretKey(); // secret key (from portal or sub account)
public String makeSignature(
String accessKey, String secretKey, String url, String timeStamp) {
String result;
try {
String message =
new StringBuilder()
.append(method)
.append(space)
.append(url)
.append(newLine)
.append(timeStamp)
.append(newLine)
.append(accessKey)
.toString();

String message = new StringBuilder()
.append(method)
.append(space)
.append(url)
.append(newLine)
.append(timestamp)
.append(newLine)
.append(accessKey)
.toString();
SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);

SecretKeySpec signingKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(message.getBytes("UTF-8"));
String encodeBase64String = Base64.encodeBase64String(rawHmac);

byte[] rawHmac = mac.doFinal(message.getBytes("UTF-8"));
String encodeBase64String = Base64.getEncoder().encodeToString(rawHmac);

return encodeBase64String;
result = encodeBase64String;

} catch (Exception ex) {
throw new RecruitDynamicException(0, "400", ex.getMessage());
}
return result;
}

public SendRawEmailDto createSendRawEmailDto(String title, String body, String recipientAddress) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.econovation.recruitdomain.domains.dto;

import com.econovation.recruitdomain.domains.comment.domain.Comment;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class CommentPairVo {
// commentId
private Long id;
private LocalDateTime createdAt;
private String createdAt;
private String interviewerName;
private String content;
private Boolean isLike;
Expand All @@ -20,11 +20,12 @@ public class CommentPairVo {
public static CommentPairVo of(Comment comment, Boolean isLike, String interviewerName, Boolean canEdit) {
return CommentPairVo.builder()
.id(comment.getId())
.createdAt(comment.getCreatedAt())
.createdAt(String.valueOf(Timestamp.valueOf(comment.getCreatedAt()).getTime()))
.content(comment.getContent())
.isLike(isLike)
.likeCount(comment.getLikeCount())
.interviewerName(interviewerName)
.canEdit(canEdit)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public interface NcpClient {
path = "/mails",
consumes = "application/json; charset=UTF-8")
public void sendMail(
@RequestHeader("x-ncp-apigw-timestamp") String timestamp,
@RequestHeader("x-ncp-iam-access-key") String accessKey,
@RequestHeader("x-ncp-apigw-timestamp") String timestamp,
@RequestHeader("x-ncp-apigw-signature-v2") String signature,
@RequestBody SendRawEmailDto sendRawEmailDto);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@
import com.econovation.recruitcommon.exception.OtherServerUnauthorizedException;
import feign.Response;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class NcpInfoErrorDecoder implements ErrorDecoder {
@Override
public Exception decode(String methodKey, Response response) {
switch (response.status()) {
case 401:
log.error("NCP 인증에 실패하였습니다. {}", response.body());
throw OtherServerUnauthorizedException.EXCEPTION;
case 403:
log.error("NCP 인증에 실패하였습니다. {}", response.body());
throw OtherServerForbiddenException.EXCEPTION;
case 404:
log.error("NCP 인증에 실패하였습니다. {}", response.body());
throw OtherServerNotFoundException.EXCEPTION;
case 500:
log.error("NCP 인증에 실패하였습니다. {}", response.body());
throw OtherServerInternalSeverErrorException.EXCEPTION;
default:
log.error("NCP 인증에 실패하였습니다. {}", response.body());
throw OtherServerBadRequestException.EXCEPTION;
}
}
Expand Down

0 comments on commit ee78322

Please sign in to comment.