Skip to content

Commit

Permalink
Feat : 약관 동의시, 비동의시 유저 추가정보 입력 (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
Astin01 authored Oct 5, 2024
1 parent 3b00e0c commit f95a4bc
Show file tree
Hide file tree
Showing 13 changed files with 424 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public ResponseEntity<Void> logout(HttpServletResponse response, @Authentication
return ResponseEntity.ok().build();
}


@PostMapping("/token/refresh")
public ResponseEntity<Void> reissueAccessToken(HttpServletResponse response,
@AuthenticationRefreshPrincipal Long memberId) {
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/solitour_backend/solitour/auth/entity/Term.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package solitour_backend.solitour.auth.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import solitour_backend.solitour.user.entity.User;

@Getter
@NoArgsConstructor
@Entity
@Builder
@AllArgsConstructor
@Table(name = "term")
public class Term {

@Id
@Column(name = "term_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@Column(nullable = false, name = "term_condition_agreement")
private Boolean termCondition;

@Column(nullable = false, name = "term_privacy_agreement")
private Boolean termPrivacy;

@Column(name="term_created_at")
private LocalDateTime createdAt;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package solitour_backend.solitour.auth.entity;

import java.util.Optional;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import solitour_backend.solitour.user.entity.User;

public interface TermRepository extends Repository<Term, Long> {
void save(Term term);

@Query("SELECT t FROM Term t WHERE t.user = :user")
<T> Optional<T> findByUser(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ private User saveActiveKakaoUser(KakaoUserResponse kakaoUserResponse, CreateUser
UserImage savedUserImage = userImageService.saveUserImage(imageUrl);

User user = User.builder()
.userStatus(UserStatus.ACTIVATE)
.userStatus(UserStatus.PENDING)
.oauthId(String.valueOf(kakaoUserResponse.getId()))
.provider("kakao")
.isAdmin(false)
Expand All @@ -279,7 +279,7 @@ private User saveKakaoUser(KakaoUserResponse response) {
UserImage savedUserImage = userImageService.saveUserImage(imageUrl);

User user = User.builder()
.userStatus(UserStatus.INACTIVATE)
.userStatus(UserStatus.PENDING)
.oauthId(String.valueOf(response.getId()))
.provider("kakao")
.isAdmin(false)
Expand Down Expand Up @@ -376,15 +376,15 @@ private String getDefaultProfile(User user) {
return USER_PROFILE_NONE;
}

private void deleteUserProfileFromS3 (UserImage userImage, String defaultImageUrl){
String userImageUrl = userImage.getAddress();
if (userImageUrl.equals(USER_PROFILE_MALE) || userImageUrl.equals(USER_PROFILE_FEMALE)
|| userImageUrl.equals(
USER_PROFILE_NONE)) {
return;
}
s3Uploader.deleteImage(userImageUrl);
userImage.changeToDefaultProfile(defaultImageUrl);
private void deleteUserProfileFromS3(UserImage userImage, String defaultImageUrl) {
String userImageUrl = userImage.getAddress();
if (userImageUrl.equals(USER_PROFILE_MALE) || userImageUrl.equals(USER_PROFILE_FEMALE)
|| userImageUrl.equals(
USER_PROFILE_NONE)) {
return;
}

s3Uploader.deleteImage(userImageUrl);
userImage.changeToDefaultProfile(defaultImageUrl);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import solitour_backend.solitour.gathering.dto.response.GatheringMypageResponse;
import solitour_backend.solitour.information.dto.response.InformationBriefResponse;
import solitour_backend.solitour.user.dto.UpdateNicknameRequest;
import solitour_backend.solitour.user.dto.request.UpdateUserInfoRequest;
import solitour_backend.solitour.user.dto.request.AgreeUserInfoRequest;
import solitour_backend.solitour.user.dto.request.DisagreeUserInfoRequest;
import solitour_backend.solitour.user.exception.NicknameAlreadyExistsException;
import solitour_backend.solitour.user.exception.UserNotExistsException;
import solitour_backend.solitour.user.service.UserService;
Expand All @@ -44,10 +45,18 @@ public ResponseEntity<UserInfoResponse> retrieveUserInfo(@AuthenticationPrincipa
return ResponseEntity.ok(response);
}

@PutMapping("/info")
public ResponseEntity<Void> updateUserInfo(@AuthenticationPrincipal Long userId,
@RequestBody UpdateUserInfoRequest request) {
userService.updateUserInfo(userId, request);
@PutMapping("/info/agree")
public ResponseEntity<Void> agreeUserInfo(@AuthenticationPrincipal Long userId,
@RequestBody AgreeUserInfoRequest request) {
userService.agreeUserInfo(userId, request);

return ResponseEntity.noContent().build();
}

@PutMapping("/info/disagree")
public ResponseEntity<Void> disagreeUserInfo(@AuthenticationPrincipal Long userId,
@RequestBody DisagreeUserInfoRequest request) {
userService.disagreeUserInfo(userId, request);

return ResponseEntity.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

@NoArgsConstructor
@Getter
public class UpdateUserInfoRequest {
public class AgreeUserInfoRequest {
private Boolean termConditionAgreement;
private Boolean privacyPolicyAgreement;
private String name;
private String age;
private String sex;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package solitour_backend.solitour.user.dto.request;

import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Getter
public class DisagreeUserInfoRequest {
private Boolean termConditionAgreement;
private Boolean privacyPolicyAgreement;
}
14 changes: 12 additions & 2 deletions src/main/java/solitour_backend/solitour/user/entity/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package solitour_backend.solitour.user.entity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
Expand All @@ -15,7 +16,9 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import solitour_backend.solitour.user.dto.request.UpdateUserInfoRequest;
import solitour_backend.solitour.auth.entity.Term;
import solitour_backend.solitour.user.dto.request.AgreeUserInfoRequest;
import solitour_backend.solitour.user.dto.request.DisagreeUserInfoRequest;
import solitour_backend.solitour.user.user_status.UserStatus;
import solitour_backend.solitour.user.user_status.UserStatusConverter;
import solitour_backend.solitour.user_image.entity.UserImage;
Expand Down Expand Up @@ -47,6 +50,9 @@ public class User {
@JoinColumn(name = "user_image_id")
private UserImage userImage;

@OneToOne(mappedBy = "user",cascade = CascadeType.ALL)
private Term term;

@Column(name = "user_nickname")
private String nickname;

Expand Down Expand Up @@ -105,10 +111,14 @@ public void updateLoginTime() {
this.latestLoginAt = LocalDateTime.now();
}

public void updateUserInfo(UpdateUserInfoRequest request) {
public void agreeUserInfo(AgreeUserInfoRequest request) {
this.name = request.getName();
this.userStatus = UserStatus.ACTIVATE;
this.age = Integer.valueOf(request.getAge());
this.sex = request.getSex();
}

public void disagreeUserInfo(DisagreeUserInfoRequest request) {
this.userStatus = UserStatus.INACTIVATE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

public interface UserRepository extends JpaRepository<User, Long>, UserRepositoryCustom {

@Query("SELECT u FROM User u JOIN FETCH u.userImage WHERE u.id = :userId AND (u.userStatus = '활성화' OR u.userStatus = '비활성화')")
@Query("SELECT u FROM User u JOIN FETCH u.userImage WHERE u.id = :userId AND u.userStatus NOT IN ('차단', '휴먼','삭제')")
User findByUserId(Long userId);

@Query("SELECT u FROM User u JOIN FETCH u.userImage WHERE u.oauthId = :oauthId AND (u.userStatus = '활성화' OR u.userStatus = '비활성화')")
@Query("SELECT u FROM User u JOIN FETCH u.userImage WHERE u.oauthId = :oauthId AND u.userStatus NOT IN ('차단', '휴먼','삭제')")
Optional<User> findByOauthId(String oauthId);

boolean existsByNickname(String nickname);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package solitour_backend.solitour.user.service;

import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import solitour_backend.solitour.auth.entity.Term;
import solitour_backend.solitour.auth.entity.TermRepository;
import solitour_backend.solitour.gathering.dto.response.GatheringApplicantResponse;
import solitour_backend.solitour.gathering.dto.response.GatheringMypageResponse;
import solitour_backend.solitour.image.s3.S3Uploader;
import solitour_backend.solitour.information.dto.response.InformationBriefResponse;
import solitour_backend.solitour.user.dto.request.UpdateUserInfoRequest;
import solitour_backend.solitour.user.dto.request.AgreeUserInfoRequest;
import solitour_backend.solitour.user.dto.request.DisagreeUserInfoRequest;
import solitour_backend.solitour.user.entity.User;
import solitour_backend.solitour.user.exception.NicknameAlreadyExistsException;
import solitour_backend.solitour.user.repository.UserRepository;
Expand All @@ -26,6 +30,7 @@ public class UserService {

private final UserRepository userRepository;
private final UserImageService userImageService;
private final TermRepository termRepository;
private final S3Uploader s3Uploader;
@Value("${user.profile.url.female}")
private String femaleProfileUrl;
Expand Down Expand Up @@ -84,13 +89,33 @@ public Page<GatheringApplicantResponse> retrieveGatheringApplicant(Pageable page
}

@Transactional
public void updateUserInfo(Long userId, UpdateUserInfoRequest request) {
public void agreeUserInfo(Long userId, AgreeUserInfoRequest request) {
User user = userRepository.findByUserId(userId);
changeUserProfile(user, request);
user.updateUserInfo(request);
if(!termRepository.findByUser(user).isPresent()){
saveTerm(user, request.getTermConditionAgreement(),request.getPrivacyPolicyAgreement());
}
user.agreeUserInfo(request);
}

@Transactional
public void disagreeUserInfo(Long userId, DisagreeUserInfoRequest request) {
User user = userRepository.findByUserId(userId);
saveTerm(user, request.getTermConditionAgreement(),request.getPrivacyPolicyAgreement());
user.disagreeUserInfo(request);
}

private void saveTerm(User user, Boolean termCondition, Boolean termPrivacy) {
Term term = Term.builder()
.user(user)
.termCondition(termCondition)
.termPrivacy(termPrivacy)
.createdAt(LocalDateTime.now())
.build();
termRepository.save(term);
}

private void changeUserProfile(User user, UpdateUserInfoRequest request) {
private void changeUserProfile(User user, AgreeUserInfoRequest request) {
String sex = request.getSex();

if(user.getUserImage().equals(noneProfileUrl) && sex.equals("male")){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

@Getter
public enum UserStatus {
PENDING("대기"),
ACTIVATE("활성화"),
INACTIVATE("비활성화"),
BLOCK("차단"),
Expand Down
20 changes: 17 additions & 3 deletions src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ DROP TABLE IF EXISTS `banner`;
DROP TABLE IF EXISTS `notice`;
DROP TABLE IF EXISTS `qna_message`;
DROP TABLE IF EXISTS `qna`;
DROP TABLE IF EXISTS `term`;
DROP TABLE IF EXISTS `user`;
DROP TABLE IF EXISTS `user_image`;
DROP TABLE IF EXISTS `diary_day_content`;
DROP TABLE IF EXISTS `diary`;


CREATE TABLE `user_image`
(
`user_image_id` BIGINT NOT NULL AUTO_INCREMENT,
Expand Down Expand Up @@ -55,8 +57,8 @@ CREATE TABLE `user`

CREATE TABLE `token`
(
`token_id` BIGINT NOT NULL AUTO_INCREMENT,
`user_id` BIGINT NOT NULL,
`token_id` BIGINT NOT NULL AUTO_INCREMENT,
`user_id` BIGINT NOT NULL,
`refresh_token` VARCHAR(250) NULL,
`oauth_token` VARCHAR(250) NULL,
CONSTRAINT PK_TOKEN PRIMARY KEY (`token_id`),
Expand Down Expand Up @@ -311,4 +313,16 @@ CREATE TABLE `diary_day_content`
`diary_day_content_image` TEXT DEFAULT NULL,
PRIMARY KEY (`diary_day_content_id`),
CONSTRAINT `FK_diary_day_content_TO_diary` FOREIGN KEY (`diary_id`) REFERENCES `diary` (`diary_id`)
);
);


CREATE TABLE `term`
(
`term_id` BIGINT NOT NULL AUTO_INCREMENT,
`user_id` BIGINT NOT NULL,
`term_condition_agreement` BOOLEAN NOT NULL,
`term_privacy_agreement` BOOLEAN NOT NULL,
`term_created_at` DATETIME NOT NULL,
PRIMARY KEY (`term_id`),
CONSTRAINT `FK_term_TO_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`)
);
Loading

0 comments on commit f95a4bc

Please sign in to comment.