Add SSE for Razorpay QR events
All checks were successful
build-and-deploy / build-deploy (push) Successful in 34s

This commit is contained in:
androidlover5842
2026-02-01 12:22:16 +05:30
parent c74944711e
commit 35b15f37ec
3 changed files with 75 additions and 0 deletions

View File

@@ -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
)