Skip to content

Commit

Permalink
[feat #71] 알림 기초 작업 (#73)
Browse files Browse the repository at this point in the history
* chore : Firebase 라이브러리 추가

* feat : Alert 도메인모델, 엔티티

* feat : 알림 전송 로직 구현

* remove : 기존에 있던 파베 설정클래스 제거

* edit : Content 응답 통일 (categpry 관련)

* feat : CategoryInfo 생성
  • Loading branch information
dlswns2480 authored Aug 8, 2024
1 parent 0837e4f commit 51af067
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pokit.content.dto.response


import com.pokit.category.model.RemindCategory
import java.time.format.DateTimeFormatter

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.pokit.out.persistence.alert.persist

import com.pokit.alert.model.Alert
import com.pokit.content.model.ContentInfo
import com.pokit.out.persistence.BaseEntity
import jakarta.persistence.*

@Table(name = "ALERT")
@Entity
class AlertEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = 0L,

@Column(name = "user_id")
val userId: Long,

@Column(name = "content_id")
val contentId: Long,

@Column(name = "content_thumb_nail")
val contentThumbNail: String,

@Column(name = "title")
val title: String,

@Column(name = "body")
val body: String,

@Column(name = "is_deleted")
val deleted: Boolean = false

) : BaseEntity() {
companion object {
fun of(alert: Alert) = AlertEntity(
userId = alert.userId,
contentId = alert.content.contentId,
contentThumbNail = alert.content.contentThumbNail,
title = alert.title,
body = alert.body,
)
}
}

fun AlertEntity.toDomain() = Alert(
id = this.id,
userId = this.userId,
content = ContentInfo(this.contentId, this.contentThumbNail),
title = this.title,
body = this.body,
)

This file was deleted.

1 change: 1 addition & 0 deletions adapters/out-web/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.12.5")
implementation("org.bouncycastle:bcpkix-jdk15on:1.69")
implementation("org.springframework.cloud:spring-cloud-starter-openfeign:4.1.3")
implementation("com.google.firebase:firebase-admin:8.1.0")

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.pokit.auth.common.config
package com.pokit.alert.common.config

import com.google.auth.oauth2.GoogleCredentials
import com.google.firebase.FirebaseApp
Expand All @@ -15,10 +15,9 @@ class FirebaseConfig {
val resource = ClassPathResource("google-services.json")
val serviceAccount = resource.inputStream

val options =
FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.build()
val options = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.build()

return FirebaseApp.initializeApp(options)
}
Expand Down
42 changes: 42 additions & 0 deletions adapters/out-web/src/main/kotlin/com/pokit/alert/impl/FcmSender.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.pokit.alert.impl

import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.FirebaseMessagingException
import com.google.firebase.messaging.Message
import com.google.firebase.messaging.Notification
import com.pokit.alert.model.Alert
import com.pokit.alert.port.out.AlertSender
import io.github.oshai.kotlinlogging.KotlinLogging
import org.springframework.stereotype.Component

@Component
class FcmSender : AlertSender {
private val logger = KotlinLogging.logger { }

companion object {
const val IMAGE_PATH = "https://pokit-storage.s3.ap-northeast-2.amazonaws.com/logo/pokit.png" // 앱 로고
}

override fun sendMessage(tokens: List<String>, alert: Alert) {
val notification = Notification.builder()
.setTitle(alert.title)
.setBody(alert.body)
.setImage(IMAGE_PATH)
.build()

val messages = tokens.map {
Message.builder()
.setNotification(notification)
.setToken(it)
.build()
}

try {
messages.forEach {
FirebaseMessaging.getInstance().sendAsync(it)
}
} catch (e: FirebaseMessagingException) {
logger.warn { "Failed To Send Message : ${e.message}" }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.pokit.alert.port.out

import com.pokit.alert.model.Alert

interface AlertSender {
fun sendMessage(tokens: List<String>, alert: Alert)
}
11 changes: 11 additions & 0 deletions domain/src/main/kotlin/com/pokit/alert/model/Alert.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.pokit.alert.model

import com.pokit.content.model.ContentInfo

data class Alert(
val id: Long,
val userId: Long,
val content: ContentInfo,
val title: String,
val body: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.pokit.content.dto.response

import com.pokit.category.model.RemindCategory
import com.pokit.content.model.Content
import com.pokit.content.model.CategoryInfo
import java.time.LocalDateTime

data class ContentsResult(
Expand Down
13 changes: 12 additions & 1 deletion domain/src/main/kotlin/com/pokit/content/model/Content.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ data class Content(
var memo: String,
var alertYn: String,
val createdAt: LocalDateTime = LocalDateTime.now(),
var domain: String = data
var domain: String = data,
val thumbNail: String = "https://pokit-storage.s3.ap-northeast-2.amazonaws.com/category-image/-3+1.png"
) {
fun modify(contentCommand: ContentCommand) {
this.categoryId = contentCommand.categoryId
Expand All @@ -36,3 +37,13 @@ data class Content(
}
}
}

data class ContentInfo(
val contentId: Long,
val contentThumbNail: String
)

data class CategoryInfo(
val categoryId: Long,
val categoryName: String
)

0 comments on commit 51af067

Please sign in to comment.