-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotificationController.kt
105 lines (92 loc) · 3.66 KB
/
NotificationController.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.aamdigital.aambackendservice.notification.controller
import com.aamdigital.aambackendservice.error.HttpErrorDto
import com.aamdigital.aambackendservice.notification.repositiory.UserDeviceEntity
import com.aamdigital.aambackendservice.notification.repositiory.UserDeviceRepository
import org.slf4j.LoggerFactory
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken
import org.springframework.transaction.annotation.Transactional
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.io.IOException
import kotlin.jvm.optionals.getOrNull
data class DeviceRegistrationDto(
val deviceName: String? = null,
val deviceToken: String,
)
@RestController
@RequestMapping("/v1/notification")
@ConditionalOnProperty(
prefix = "features.notification-api",
name = ["enabled"],
havingValue = "true",
matchIfMissing = false
)
@Transactional
class NotificationController(
private val userDeviceRepository: UserDeviceRepository,
) {
private val logger = LoggerFactory.getLogger(javaClass)
@PostMapping("/device")
@Validated
fun registerDevice(
@RequestBody deviceRegistrationDto: DeviceRegistrationDto,
authentication: JwtAuthenticationToken,
): ResponseEntity<Any> {
if (userDeviceRepository.existsByDeviceToken(deviceRegistrationDto.deviceToken)) {
return ResponseEntity.badRequest().body(
HttpErrorDto(
errorCode = "Bad Request",
errorMessage = "The device is already registered."
)
)
}
if (authentication.name == null) {
return ResponseEntity.badRequest().body(
HttpErrorDto(
errorCode = "Bad Request",
errorMessage = "No subject found in the token."
)
)
}
userDeviceRepository.save(
UserDeviceEntity(
userIdentifier = authentication.name,
deviceToken = deviceRegistrationDto.deviceToken,
deviceName = deviceRegistrationDto.deviceName,
)
)
return ResponseEntity.noContent().build()
}
@DeleteMapping("/device/{id}")
fun unregisterDevice(
@PathVariable id: String,
authentication: JwtAuthenticationToken,
): ResponseEntity<Any> {
val userDevice =
userDeviceRepository.findByDeviceToken(id).getOrNull() ?: return ResponseEntity.notFound().build()
if (userDevice.userIdentifier != (authentication.name
?: authentication.tokenAttributes["username"].toString())
) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(
HttpErrorDto(
errorCode = "Forbidden",
errorMessage = "Token does not belong to User",
)
)
}
try {
userDeviceRepository.deleteByDeviceToken(id)
} catch (ex: IOException) {
logger.warn("[NotificationController.unregisterDevice()] error: {}", ex.message)
}
return ResponseEntity.noContent().build()
}
}