Skip to content

Commit

Permalink
Merge pull request #91 from ecolink-JOIN/feature/push
Browse files Browse the repository at this point in the history
[Feat] 푸시 알림 동의 변경 API 추가
  • Loading branch information
wnsvy607 authored Feb 6, 2025
2 parents 74be2cc + 1235ba1 commit caac54b
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/main/java/com/join/core/auth/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import com.join.core.auth.constant.UserType;
import com.join.core.avatar.domain.Avatar;
import com.join.core.avatar.domain.ProfilePhoto;
import com.join.core.common.domain.BaseTimeEntity;
import com.join.core.common.exception.impl.InvalidParamException;
import com.join.core.common.util.TokenGenerator;
Expand Down Expand Up @@ -69,6 +68,11 @@ public enum Status {
@NotNull
private boolean termsAgreed;

@NotNull
private boolean pushConsent;

private String fcmToken;

@NotNull
@OneToOne(mappedBy = "user", cascade = CascadeType.PERSIST)
private Avatar avatar;
Expand All @@ -79,21 +83,23 @@ public enum Status {
@OneToMany(mappedBy = "user", cascade = CascadeType.PERSIST, orphanRemoval = true)
private List<TermAgreeHistory> termAgreeHistoryList = new ArrayList<>();


public boolean isUserOf(UserType providerType) {
return providerType.equals(this.platform);
}

public User(String email, UserType platform) {
if (StringUtils.isEmpty(email)) throw new InvalidParamException(INVALID_PARAMETER, "User.email");
if (platform == null) throw new InvalidParamException(INVALID_PARAMETER, "User.platform");
if (StringUtils.isEmpty(email))
throw new InvalidParamException(INVALID_PARAMETER, "User.email");
if (platform == null)
throw new InvalidParamException(INVALID_PARAMETER, "User.platform");

this.email = email;
this.platform = platform;
this.userToken = TokenGenerator.randomCharacterWithPrefix(USER_PREFIX);
this.singUpDate = LocalDateTime.now();
this.status = Status.PENDING;
this.termsAgreed = false;
this.pushConsent = false;
this.avatar = new Avatar(this);
}

Expand All @@ -106,9 +112,14 @@ public Set<SimpleGrantedAuthority> getAuthorities() {
}

public void agree(Term term, TermAgreeHistory.AcceptStatus status) {
if(ObjectUtils.isEmpty(term))
if (ObjectUtils.isEmpty(term))
throw new InvalidParamException(INVALID_PARAMETER, "agree.term");
this.termAgreeHistoryList.add(new TermAgreeHistory(this, term, status));
}

public void updatePushConsent(boolean consent, String fcmToken) {
if (consent && StringUtils.isEmpty(fcmToken)) throw new InvalidParamException(INVALID_PARAMETER, "updatePushConsent.fcmToken");
this.pushConsent = consent;
}

}
1 change: 1 addition & 0 deletions src/main/java/com/join/core/auth/domain/UserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static class SigIn {
private final boolean registered;
private final boolean newUser;
private final boolean termsAgreed;
private final boolean pushConsent;
private final boolean nicknameSet;

public static SigIn unregistered() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public interface UserInfoMapper {
@Mapping(constant = "true", target = "registered")
@Mapping(source = "newUser", target = "newUser")
@Mapping(expression = "java(user.isTermsAgreed())", target = "termsAgreed")
@Mapping(expression = "java(user.isPushConsent())", target = "pushConsent")
@Mapping(expression = "java(user.getAvatar().isNicknameSet())", target = "nicknameSet")
UserInfo.SigIn of(User user, boolean newUser);

Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/join/core/avatar/controller/PushController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.join.core.avatar.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.join.core.auth.domain.UserPrincipal;
import com.join.core.avatar.controller.specification.PushControllerSpecification;
import com.join.core.avatar.domain.AvatarCommand;
import com.join.core.avatar.service.PushService;
import com.join.core.common.response.ApiResponse;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("${api.prefix}/avatars/push")
public class PushController implements PushControllerSpecification {

private final PushService pushService;


@PreAuthorize("isAuthenticated()")
@PutMapping
public ApiResponse<Void> changePushConsent(@AuthenticationPrincipal UserPrincipal principal,
@RequestBody AvatarCommand.ChangePushConsent command) {
pushService.updatePushConsent(principal.getUserId(), command);
return ApiResponse.ok();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.join.core.avatar.controller.specification;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import com.join.core.auth.domain.UserPrincipal;
import com.join.core.avatar.domain.AvatarCommand;
import com.join.core.common.response.ApiResponse;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;

public interface PushControllerSpecification {

@Tag(name = "${swagger.tag.sign-up}")
@Tag(name = "${swagger.tag.user}")
@Operation(summary = "푸시 알림 동의 API - 인증 필요",
description = "푸시 알림 동의 API - 인증 필요",
security = {@SecurityRequirement(name = "session-token")})
@PreAuthorize("isAuthenticated()")
@PutMapping
ApiResponse<Void> changePushConsent(@AuthenticationPrincipal UserPrincipal principal,
@RequestBody AvatarCommand.ChangePushConsent command);

}
9 changes: 9 additions & 0 deletions src/main/java/com/join/core/avatar/domain/AvatarCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@ public static class ChangePhoto {
private boolean defaultPhoto;
}

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public static class ChangePushConsent {
private boolean consent;
private String fcmToken;
}

}
9 changes: 9 additions & 0 deletions src/main/java/com/join/core/avatar/service/PushService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.join.core.avatar.service;

import com.join.core.avatar.domain.AvatarCommand;

public interface PushService {

void updatePushConsent(Long userId, AvatarCommand.ChangePushConsent command);

}
26 changes: 26 additions & 0 deletions src/main/java/com/join/core/avatar/service/PushServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.join.core.avatar.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.join.core.auth.domain.User;
import com.join.core.auth.service.UserReader;
import com.join.core.avatar.domain.AvatarCommand;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Transactional(readOnly = true)
@Service
public class PushServiceImpl implements PushService {

private final UserReader userReader;

@Transactional
@Override
public void updatePushConsent(Long userId, AvatarCommand.ChangePushConsent command) {
User user = userReader.getUser(userId);
user.updatePushConsent(command.isConsent(), command.getFcmToken());
}

}

0 comments on commit caac54b

Please sign in to comment.