-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushPreferenceService.kt
67 lines (60 loc) · 2 KB
/
PushPreferenceService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.wafflestudio.snutt.notification.service
import com.wafflestudio.snutt.notification.data.PushCategory
import com.wafflestudio.snutt.notification.data.PushOptOut
import com.wafflestudio.snutt.notification.dto.PushPreference
import com.wafflestudio.snutt.notification.repository.PushOptOutRepository
import com.wafflestudio.snutt.users.data.User
import org.springframework.stereotype.Service
interface PushPreferenceService {
suspend fun enablePush(
user: User,
pushCategory: PushCategory,
)
suspend fun disablePush(
user: User,
pushCategory: PushCategory,
)
suspend fun getPushPreferences(user: User): List<PushPreference>
}
@Service
class PushPreferenceServiceImpl(
private val pushOptOutRepository: PushOptOutRepository,
) : PushPreferenceService {
override suspend fun enablePush(
user: User,
pushCategory: PushCategory,
) {
pushOptOutRepository.save(
PushOptOut(
userId = user.id!!,
pushCategory = pushCategory,
),
)
}
override suspend fun disablePush(
user: User,
pushCategory: PushCategory,
) {
pushOptOutRepository.deleteByUserIdAndPushCategory(
userId = user.id!!,
pushCategory = pushCategory,
)
}
override suspend fun getPushPreferences(user: User): List<PushPreference> {
val allPushCategories = PushCategory.entries.filterNot { it == PushCategory.NORMAL }
val disabledPushCategories = pushOptOutRepository.findByUserId(user.id!!).map { it.pushCategory }.toSet()
return allPushCategories.map {
if (it in disabledPushCategories) {
return@map PushPreference(
pushCategory = it,
enabled = false,
)
} else {
return@map PushPreference(
pushCategory = it,
enabled = true,
)
}
}
}
}