Skip to content

Commit 2acf179

Browse files
authored
Refactor notification setting (#19)
1 parent 0fe0b10 commit 2acf179

File tree

8 files changed

+88
-56
lines changed

8 files changed

+88
-56
lines changed

src/main/kotlin/com/moa/controller/NotificationSettingController.kt

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,9 @@ class NotificationSettingController(
2020
@Auth member: AuthMemberInfo,
2121
) = ApiResponse.success(notificationSettingService.getSettings(member.id))
2222

23-
@PatchMapping("/work")
24-
fun updateWorkNotification(
23+
@PatchMapping
24+
fun updateSetting(
2525
@Auth member: AuthMemberInfo,
2626
@RequestBody req: NotificationSettingUpdateRequest,
27-
) = ApiResponse.success(notificationSettingService.updateWorkNotification(member.id, req.enabled))
28-
29-
@PatchMapping("/payday")
30-
fun updatePaydayNotification(
31-
@Auth member: AuthMemberInfo,
32-
@RequestBody req: NotificationSettingUpdateRequest,
33-
) = ApiResponse.success(notificationSettingService.updatePaydayNotification(member.id, req.enabled))
34-
35-
@PatchMapping("/promotion")
36-
fun updatePromotionNotification(
37-
@Auth member: AuthMemberInfo,
38-
@RequestBody req: NotificationSettingUpdateRequest,
39-
) = ApiResponse.success(notificationSettingService.updatePromotionNotification(member.id, req.enabled))
27+
) = ApiResponse.success(notificationSettingService.updateSetting(member.id, req.type, req.checked))
4028
}

src/main/kotlin/com/moa/entity/NotificationSetting.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,30 @@ class NotificationSetting(
99
val memberId: Long,
1010

1111
@Column(nullable = false)
12-
var workNotificationEnabled: Boolean = false,
12+
var workNotificationEnabled: Boolean = true,
1313

1414
@Column(nullable = false)
15-
var paydayNotificationEnabled: Boolean = false,
15+
var paydayNotificationEnabled: Boolean = true,
1616

1717
@Column(nullable = false)
18-
var promotionNotificationEnabled: Boolean = false,
18+
var marketingNotificationEnabled: Boolean = true,
1919
) : BaseEntity() {
2020

2121
@Id
2222
@GeneratedValue(strategy = GenerationType.IDENTITY)
2323
val id: Long = 0
24+
25+
fun isEnabled(type: NotificationSettingType): Boolean = when (type) {
26+
NotificationSettingType.WORK -> workNotificationEnabled
27+
NotificationSettingType.PAYDAY -> paydayNotificationEnabled
28+
NotificationSettingType.MARKETING -> marketingNotificationEnabled
29+
}
30+
31+
fun update(type: NotificationSettingType, checked: Boolean) {
32+
when (type) {
33+
NotificationSettingType.WORK -> workNotificationEnabled = checked
34+
NotificationSettingType.PAYDAY -> paydayNotificationEnabled = checked
35+
NotificationSettingType.MARKETING -> marketingNotificationEnabled = checked
36+
}
37+
}
2438
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.moa.entity
2+
3+
enum class NotificationSettingType(
4+
val category: String,
5+
val title: String,
6+
) {
7+
WORK("서비스 알림", "출퇴근 알림"),
8+
PAYDAY("서비스 알림", "월급날 알림"),
9+
MARKETING("광고성 정보 알림", "혜택 및 이벤트 알림"),
10+
}

src/main/kotlin/com/moa/service/NotificationSettingService.kt

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.moa.service
33
import com.moa.common.exception.BadRequestException
44
import com.moa.common.exception.ErrorCode
55
import com.moa.entity.NotificationSetting
6+
import com.moa.entity.NotificationSettingType
67
import com.moa.entity.Term
78
import com.moa.repository.NotificationSettingRepository
89
import com.moa.repository.TermAgreementRepository
@@ -17,47 +18,45 @@ class NotificationSettingService(
1718
) {
1819

1920
@Transactional(readOnly = true)
20-
fun getSettings(memberId: Long): NotificationSettingResponse {
21+
fun getSettings(memberId: Long): List<NotificationSettingResponse> {
2122
val setting = notificationSettingRepository.findByMemberId(memberId)
22-
return NotificationSettingResponse.from(setting)
23-
}
24-
25-
@Transactional
26-
fun updateWorkNotification(memberId: Long, enabled: Boolean): NotificationSettingResponse {
27-
val setting = getOrCreate(memberId)
28-
setting.workNotificationEnabled = enabled
29-
return NotificationSettingResponse.from(setting)
30-
}
31-
32-
@Transactional
33-
fun updatePaydayNotification(memberId: Long, enabled: Boolean): NotificationSettingResponse {
34-
val setting = getOrCreate(memberId)
35-
setting.paydayNotificationEnabled = enabled
36-
return NotificationSettingResponse.from(setting)
23+
if (setting != null) {
24+
return NotificationSettingResponse.from(setting)
25+
}
26+
// Setting 지연생성 때문에 지저분한 코드..
27+
return NotificationSettingResponse.from(isMarketingAgreed(memberId))
3728
}
3829

3930
@Transactional
40-
fun updatePromotionNotification(memberId: Long, enabled: Boolean): NotificationSettingResponse {
41-
val setting = getOrCreate(memberId)
42-
43-
if (enabled) {
31+
fun updateSetting(
32+
memberId: Long,
33+
type: NotificationSettingType,
34+
checked: Boolean
35+
): List<NotificationSettingResponse> {
36+
if (type == NotificationSettingType.MARKETING && checked) {
4437
validateMarketingAgreed(memberId)
4538
}
46-
47-
setting.promotionNotificationEnabled = enabled
48-
39+
val setting = getOrCreate(memberId)
40+
setting.update(type, checked)
4941
return NotificationSettingResponse.from(setting)
5042
}
5143

5244
private fun getOrCreate(memberId: Long): NotificationSetting {
5345
return notificationSettingRepository.findByMemberId(memberId)
54-
?: notificationSettingRepository.save(NotificationSetting(memberId = memberId))
46+
?: notificationSettingRepository.save(
47+
NotificationSetting(
48+
memberId = memberId,
49+
marketingNotificationEnabled = isMarketingAgreed(memberId),
50+
)
51+
)
5552
}
5653

57-
private fun validateMarketingAgreed(memberId: Long) {
58-
val agreement = termAgreementRepository.findByMemberIdAndTermCode(memberId, Term.MARKETING)
54+
private fun isMarketingAgreed(memberId: Long): Boolean {
55+
return termAgreementRepository.findByMemberIdAndTermCode(memberId, Term.MARKETING)?.agreed ?: false
56+
}
5957

60-
if (agreement == null || !agreement.agreed) {
58+
private fun validateMarketingAgreed(memberId: Long) {
59+
if (!isMarketingAgreed(memberId)) {
6160
throw BadRequestException(ErrorCode.REQUIRED_MARKETING_TERM)
6261
}
6362
}
Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
package com.moa.service.dto
22

33
import com.moa.entity.NotificationSetting
4+
import com.moa.entity.NotificationSettingType
45

56
data class NotificationSettingResponse(
6-
val workNotificationEnabled: Boolean,
7-
val paydayNotificationEnabled: Boolean,
8-
val promotionNotificationEnabled: Boolean,
7+
val type: NotificationSettingType,
8+
val category: String,
9+
val title: String,
10+
val checked: Boolean,
911
) {
1012
companion object {
11-
fun from(setting: NotificationSetting?) = NotificationSettingResponse(
12-
workNotificationEnabled = setting?.workNotificationEnabled ?: false,
13-
paydayNotificationEnabled = setting?.paydayNotificationEnabled ?: false,
14-
promotionNotificationEnabled = setting?.promotionNotificationEnabled ?: false,
15-
)
13+
fun from(setting: NotificationSetting): List<NotificationSettingResponse> {
14+
return NotificationSettingType.entries.map { type ->
15+
NotificationSettingResponse(
16+
type = type,
17+
category = type.category,
18+
title = type.title,
19+
checked = setting.isEnabled(type),
20+
)
21+
}
22+
}
23+
24+
fun from(marketingAgreed: Boolean): List<NotificationSettingResponse> {
25+
return NotificationSettingType.entries.map { type ->
26+
NotificationSettingResponse(
27+
type = type,
28+
category = type.category,
29+
title = type.title,
30+
checked = if (type == NotificationSettingType.MARKETING) marketingAgreed else true,
31+
)
32+
}
33+
}
1634
}
1735
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.moa.service.dto
22

3+
import com.moa.entity.NotificationSettingType
4+
35
data class NotificationSettingUpdateRequest(
4-
val enabled: Boolean,
6+
val type: NotificationSettingType,
7+
val checked: Boolean,
58
)

src/main/resources/application-local.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
spring:
22
jpa:
33
hibernate:
4-
ddl-auto: create
4+
ddl-auto: update
55
properties:
66
hibernate:
77
format_sql: true

src/main/resources/application-prod.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ spring:
88
jpa:
99
database-platform: org.hibernate.dialect.MySQLDialect
1010
hibernate:
11-
ddl-auto: create # TODO. validates로 변경
11+
ddl-auto: validate
1212
properties:
1313
hibernate:
1414
format_sql: true

0 commit comments

Comments
 (0)