93 lines
3.6 KiB
Kotlin
93 lines
3.6 KiB
Kotlin
package com.android.trisolarisserver.controller
|
|
|
|
import com.android.trisolarisserver.component.PropertyAccess
|
|
import com.android.trisolarisserver.controller.dto.RazorpaySettingsResponse
|
|
import com.android.trisolarisserver.controller.dto.RazorpaySettingsUpsertRequest
|
|
import com.android.trisolarisserver.models.payment.RazorpaySettings
|
|
import com.android.trisolarisserver.models.property.Role
|
|
import com.android.trisolarisserver.repo.PropertyRepo
|
|
import com.android.trisolarisserver.repo.RazorpaySettingsRepo
|
|
import com.android.trisolarisserver.security.MyPrincipal
|
|
import org.springframework.web.bind.annotation.GetMapping
|
|
import org.springframework.web.bind.annotation.PathVariable
|
|
import org.springframework.web.bind.annotation.PutMapping
|
|
import org.springframework.web.bind.annotation.RequestBody
|
|
import org.springframework.web.bind.annotation.RequestMapping
|
|
import org.springframework.web.bind.annotation.RestController
|
|
import org.springframework.web.server.ResponseStatusException
|
|
import org.springframework.http.HttpStatus
|
|
import java.time.OffsetDateTime
|
|
import java.util.UUID
|
|
|
|
@RestController
|
|
@RequestMapping("/properties/{propertyId}/razorpay-settings")
|
|
class RazorpaySettingsController(
|
|
private val propertyAccess: PropertyAccess,
|
|
private val propertyRepo: PropertyRepo,
|
|
private val settingsRepo: RazorpaySettingsRepo
|
|
) {
|
|
|
|
@GetMapping
|
|
fun getSettings(
|
|
@PathVariable propertyId: UUID,
|
|
principal: MyPrincipal?
|
|
): RazorpaySettingsResponse {
|
|
requireRole(propertyAccess, propertyId, principal, Role.ADMIN, Role.MANAGER)
|
|
val settings = settingsRepo.findByPropertyId(propertyId)
|
|
return if (settings == null) {
|
|
RazorpaySettingsResponse(
|
|
propertyId = propertyId,
|
|
configured = false,
|
|
keyId = null,
|
|
isTest = false,
|
|
hasKeySecret = false,
|
|
hasWebhookSecret = false
|
|
)
|
|
} else {
|
|
settings.toResponse()
|
|
}
|
|
}
|
|
|
|
@PutMapping
|
|
fun upsertSettings(
|
|
@PathVariable propertyId: UUID,
|
|
principal: MyPrincipal?,
|
|
@RequestBody request: RazorpaySettingsUpsertRequest
|
|
): RazorpaySettingsResponse {
|
|
requireRole(propertyAccess, propertyId, principal, Role.ADMIN)
|
|
val property = propertyRepo.findById(propertyId).orElseThrow {
|
|
ResponseStatusException(HttpStatus.NOT_FOUND, "Property not found")
|
|
}
|
|
val existing = settingsRepo.findByPropertyId(propertyId)
|
|
val updated = if (existing == null) {
|
|
RazorpaySettings(
|
|
property = property,
|
|
keyId = request.keyId.trim(),
|
|
keySecret = request.keySecret.trim(),
|
|
webhookSecret = request.webhookSecret?.trim(),
|
|
isTest = request.isTest ?: false,
|
|
updatedAt = OffsetDateTime.now()
|
|
)
|
|
} else {
|
|
existing.keyId = request.keyId.trim()
|
|
existing.keySecret = request.keySecret.trim()
|
|
existing.webhookSecret = request.webhookSecret?.trim()
|
|
request.isTest?.let { existing.isTest = it }
|
|
existing.updatedAt = OffsetDateTime.now()
|
|
existing
|
|
}
|
|
return settingsRepo.save(updated).toResponse()
|
|
}
|
|
}
|
|
|
|
private fun RazorpaySettings.toResponse(): RazorpaySettingsResponse {
|
|
return RazorpaySettingsResponse(
|
|
propertyId = property.id!!,
|
|
configured = true,
|
|
keyId = keyId,
|
|
isTest = isTest,
|
|
hasKeySecret = keySecret.isNotBlank(),
|
|
hasWebhookSecret = !webhookSecret.isNullOrBlank()
|
|
)
|
|
}
|