Add SSE for Razorpay QR events
All checks were successful
build-and-deploy / build-deploy (push) Successful in 34s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 34s
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package com.android.trisolarisserver.component
|
||||
|
||||
import com.android.trisolarisserver.controller.dto.RazorpayQrEventResponse
|
||||
import com.android.trisolarisserver.repo.RazorpayQrRequestRepo
|
||||
import org.springframework.scheduling.annotation.Scheduled
|
||||
import org.springframework.stereotype.Component
|
||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter
|
||||
import java.time.OffsetDateTime
|
||||
import java.util.UUID
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
@Component
|
||||
class RazorpayQrEvents(
|
||||
private val qrRequestRepo: RazorpayQrRequestRepo
|
||||
) {
|
||||
private val latestEvents: MutableMap<QrKey, RazorpayQrEventResponse> = ConcurrentHashMap()
|
||||
private val hub = SseHub<QrKey>("qr") { key ->
|
||||
latestEvents[key] ?: run {
|
||||
val latest = qrRequestRepo.findTopByQrIdOrderByCreatedAtDesc(key.qrId)
|
||||
RazorpayQrEventResponse(
|
||||
event = "snapshot",
|
||||
qrId = key.qrId,
|
||||
status = latest?.status,
|
||||
receivedAt = (latest?.createdAt ?: OffsetDateTime.now()).toString()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun subscribe(propertyId: UUID, qrId: String): SseEmitter {
|
||||
return hub.subscribe(QrKey(propertyId, qrId))
|
||||
}
|
||||
|
||||
fun emit(propertyId: UUID, qrId: String, event: RazorpayQrEventResponse) {
|
||||
val key = QrKey(propertyId, qrId)
|
||||
latestEvents[key] = event
|
||||
hub.emit(key)
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelayString = "25000")
|
||||
fun heartbeat() {
|
||||
hub.heartbeat()
|
||||
}
|
||||
}
|
||||
|
||||
private data class QrKey(
|
||||
val propertyId: UUID,
|
||||
val qrId: String
|
||||
)
|
||||
Reference in New Issue
Block a user