Remove redundant tenant and user metadata assignments across services.
This commit is contained in:
parent
bbd7a371dd
commit
553ae2bd69
|
|
@ -1,8 +1,6 @@
|
||||||
package com.mosenioring.common.outbox
|
package com.mosenioring.common.outbox
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
import com.mosenioring.common.security.SecurityUtils
|
|
||||||
import com.mosenioring.common.tenant.TenantContext
|
|
||||||
import org.springframework.context.annotation.Profile
|
import org.springframework.context.annotation.Profile
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
|
|
@ -14,15 +12,11 @@ class OutboxService(
|
||||||
private val objectMapper: ObjectMapper
|
private val objectMapper: ObjectMapper
|
||||||
) {
|
) {
|
||||||
fun enqueue(eventType: String, payload: Any): OutboxEvent {
|
fun enqueue(eventType: String, payload: Any): OutboxEvent {
|
||||||
val tenantId = TenantContext.getTenantId() ?: "unknown"
|
|
||||||
val event = OutboxEvent(
|
val event = OutboxEvent(
|
||||||
id = UUID.randomUUID().toString(),
|
id = UUID.randomUUID().toString(),
|
||||||
eventType = eventType,
|
eventType = eventType,
|
||||||
payload = objectMapper.writeValueAsString(payload)
|
payload = objectMapper.writeValueAsString(payload)
|
||||||
)
|
)
|
||||||
event.tenantId = tenantId
|
|
||||||
event.createdBy = SecurityUtils.currentUserId()
|
|
||||||
event.updatedBy = SecurityUtils.currentUserId()
|
|
||||||
return repository.save(event)
|
return repository.save(event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ import com.mosenioring.clinical.repo.TestOrderRepository
|
||||||
import com.mosenioring.common.Events
|
import com.mosenioring.common.Events
|
||||||
import com.mosenioring.common.outbox.OutboxService
|
import com.mosenioring.common.outbox.OutboxService
|
||||||
import com.mosenioring.common.security.PatientAccess
|
import com.mosenioring.common.security.PatientAccess
|
||||||
import com.mosenioring.common.security.SecurityUtils
|
|
||||||
import com.mosenioring.common.tenant.TenantContext
|
import com.mosenioring.common.tenant.TenantContext
|
||||||
import org.springframework.security.access.prepost.PreAuthorize
|
import org.springframework.security.access.prepost.PreAuthorize
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
|
@ -26,9 +25,6 @@ class MedicationPlanService(
|
||||||
fun createMedicationPlan(patientId: String, description: String): MedicationPlan {
|
fun createMedicationPlan(patientId: String, description: String): MedicationPlan {
|
||||||
val tenantId = TenantContext.getTenantId() ?: throw IllegalStateException("Missing tenant")
|
val tenantId = TenantContext.getTenantId() ?: throw IllegalStateException("Missing tenant")
|
||||||
val plan = MedicationPlan(UUID.randomUUID().toString(), patientId, description)
|
val plan = MedicationPlan(UUID.randomUUID().toString(), patientId, description)
|
||||||
plan.tenantId = tenantId
|
|
||||||
plan.createdBy = SecurityUtils.currentUserId()
|
|
||||||
plan.updatedBy = SecurityUtils.currentUserId()
|
|
||||||
val saved = repository.save(plan)
|
val saved = repository.save(plan)
|
||||||
outboxService.enqueue(
|
outboxService.enqueue(
|
||||||
Events.MEDICATION_PLAN_CREATED,
|
Events.MEDICATION_PLAN_CREATED,
|
||||||
|
|
@ -52,11 +48,8 @@ class TestOrderService(
|
||||||
@Transactional
|
@Transactional
|
||||||
@PatientAccess
|
@PatientAccess
|
||||||
fun createTestOrder(patientId: String, testName: String): TestOrder {
|
fun createTestOrder(patientId: String, testName: String): TestOrder {
|
||||||
val tenantId = TenantContext.getTenantId() ?: throw IllegalStateException("Missing tenant")
|
requireNotNull(TenantContext.getTenantId()) { "Missing tenant" }
|
||||||
val order = TestOrder(UUID.randomUUID().toString(), patientId, testName, "ORDERED")
|
val order = TestOrder(UUID.randomUUID().toString(), patientId, testName, "ORDERED")
|
||||||
order.tenantId = tenantId
|
|
||||||
order.createdBy = SecurityUtils.currentUserId()
|
|
||||||
order.updatedBy = SecurityUtils.currentUserId()
|
|
||||||
val saved = repository.save(order)
|
val saved = repository.save(order)
|
||||||
auditService.record("TEST_ORDER_CREATED", "test", saved.id, patientId)
|
auditService.record("TEST_ORDER_CREATED", "test", saved.id, patientId)
|
||||||
return saved
|
return saved
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package com.mosenioring.identity.service
|
package com.mosenioring.identity.service
|
||||||
|
|
||||||
import com.mosenioring.audit.service.AuditService
|
import com.mosenioring.audit.service.AuditService
|
||||||
import com.mosenioring.common.security.SecurityUtils
|
|
||||||
import com.mosenioring.common.tenant.TenantContext
|
import com.mosenioring.common.tenant.TenantContext
|
||||||
import com.mosenioring.identity.*
|
import com.mosenioring.identity.*
|
||||||
import com.mosenioring.identity.repo.*
|
import com.mosenioring.identity.repo.*
|
||||||
|
|
@ -20,8 +19,6 @@ class TenantService(
|
||||||
fun createTenant(name: String): Tenant {
|
fun createTenant(name: String): Tenant {
|
||||||
val tenant = Tenant(UUID.randomUUID().toString(), name)
|
val tenant = Tenant(UUID.randomUUID().toString(), name)
|
||||||
tenant.tenantId = tenant.id
|
tenant.tenantId = tenant.id
|
||||||
tenant.createdBy = SecurityUtils.currentUserId()
|
|
||||||
tenant.updatedBy = SecurityUtils.currentUserId()
|
|
||||||
val saved = tenantRepository.save(tenant)
|
val saved = tenantRepository.save(tenant)
|
||||||
auditService.record("TENANT_CREATED", "tenant", saved.id, null)
|
auditService.record("TENANT_CREATED", "tenant", saved.id, null)
|
||||||
return saved
|
return saved
|
||||||
|
|
@ -36,11 +33,8 @@ class UserService(
|
||||||
@Transactional
|
@Transactional
|
||||||
@PreAuthorize("hasRole('ADMIN')")
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
fun inviteUser(email: String, role: String): User {
|
fun inviteUser(email: String, role: String): User {
|
||||||
val tenantId = TenantContext.getTenantId() ?: throw IllegalStateException("Missing tenant")
|
requireNotNull(TenantContext.getTenantId()) { "Missing tenant" }
|
||||||
val user = User(UUID.randomUUID().toString(), email, role, "INVITED")
|
val user = User(UUID.randomUUID().toString(), email, role, "INVITED")
|
||||||
user.tenantId = tenantId
|
|
||||||
user.createdBy = SecurityUtils.currentUserId()
|
|
||||||
user.updatedBy = SecurityUtils.currentUserId()
|
|
||||||
val saved = userRepository.save(user)
|
val saved = userRepository.save(user)
|
||||||
auditService.record("USER_INVITED", "user", saved.id, null)
|
auditService.record("USER_INVITED", "user", saved.id, null)
|
||||||
return saved
|
return saved
|
||||||
|
|
@ -57,11 +51,8 @@ class PatientService(
|
||||||
@Transactional
|
@Transactional
|
||||||
@PreAuthorize("hasAnyRole('ADMIN','DOCTOR','CAREGIVER')")
|
@PreAuthorize("hasAnyRole('ADMIN','DOCTOR','CAREGIVER')")
|
||||||
fun createPatient(fullName: String): Patient {
|
fun createPatient(fullName: String): Patient {
|
||||||
val tenantId = TenantContext.getTenantId() ?: throw IllegalStateException("Missing tenant")
|
requireNotNull(TenantContext.getTenantId()) { "Missing tenant" }
|
||||||
val patient = Patient(UUID.randomUUID().toString(), fullName)
|
val patient = Patient(UUID.randomUUID().toString(), fullName)
|
||||||
patient.tenantId = tenantId
|
|
||||||
patient.createdBy = SecurityUtils.currentUserId()
|
|
||||||
patient.updatedBy = SecurityUtils.currentUserId()
|
|
||||||
val saved = patientRepository.save(patient)
|
val saved = patientRepository.save(patient)
|
||||||
auditService.record("PATIENT_CREATED", "patient", saved.id, saved.id)
|
auditService.record("PATIENT_CREATED", "patient", saved.id, saved.id)
|
||||||
return saved
|
return saved
|
||||||
|
|
@ -73,9 +64,6 @@ class PatientService(
|
||||||
val tenantId = TenantContext.getTenantId() ?: throw IllegalStateException("Missing tenant")
|
val tenantId = TenantContext.getTenantId() ?: throw IllegalStateException("Missing tenant")
|
||||||
patientRepository.findByIdAndTenantId(patientId, tenantId) ?: throw IllegalArgumentException("Patient not found")
|
patientRepository.findByIdAndTenantId(patientId, tenantId) ?: throw IllegalArgumentException("Patient not found")
|
||||||
val link = PatientCaregiver(UUID.randomUUID().toString(), patientId, userId)
|
val link = PatientCaregiver(UUID.randomUUID().toString(), patientId, userId)
|
||||||
link.tenantId = tenantId
|
|
||||||
link.createdBy = SecurityUtils.currentUserId()
|
|
||||||
link.updatedBy = SecurityUtils.currentUserId()
|
|
||||||
val saved = caregiverRepository.save(link)
|
val saved = caregiverRepository.save(link)
|
||||||
auditService.record("CARE_GIVER_LINKED", "patient_caregiver", saved.id, patientId)
|
auditService.record("CARE_GIVER_LINKED", "patient_caregiver", saved.id, patientId)
|
||||||
return saved
|
return saved
|
||||||
|
|
@ -87,9 +75,6 @@ class PatientService(
|
||||||
val tenantId = TenantContext.getTenantId() ?: throw IllegalStateException("Missing tenant")
|
val tenantId = TenantContext.getTenantId() ?: throw IllegalStateException("Missing tenant")
|
||||||
patientRepository.findByIdAndTenantId(patientId, tenantId) ?: throw IllegalArgumentException("Patient not found")
|
patientRepository.findByIdAndTenantId(patientId, tenantId) ?: throw IllegalArgumentException("Patient not found")
|
||||||
val link = PatientDoctor(UUID.randomUUID().toString(), patientId, userId)
|
val link = PatientDoctor(UUID.randomUUID().toString(), patientId, userId)
|
||||||
link.tenantId = tenantId
|
|
||||||
link.createdBy = SecurityUtils.currentUserId()
|
|
||||||
link.updatedBy = SecurityUtils.currentUserId()
|
|
||||||
val saved = doctorRepository.save(link)
|
val saved = doctorRepository.save(link)
|
||||||
auditService.record("DOCTOR_LINKED", "patient_doctor", saved.id, patientId)
|
auditService.record("DOCTOR_LINKED", "patient_doctor", saved.id, patientId)
|
||||||
return saved
|
return saved
|
||||||
|
|
|
||||||
|
|
@ -43,9 +43,6 @@ class FileService(
|
||||||
val fileId = UUID.randomUUID().toString()
|
val fileId = UUID.randomUUID().toString()
|
||||||
val storageKey = "$tenantId/$fileId/$fileName"
|
val storageKey = "$tenantId/$fileId/$fileName"
|
||||||
val metadata = FileMetadata(fileId, patientId, fileName, contentType, storageKey)
|
val metadata = FileMetadata(fileId, patientId, fileName, contentType, storageKey)
|
||||||
metadata.tenantId = tenantId
|
|
||||||
metadata.createdBy = SecurityUtils.currentUserId()
|
|
||||||
metadata.updatedBy = SecurityUtils.currentUserId()
|
|
||||||
repository.save(metadata)
|
repository.save(metadata)
|
||||||
|
|
||||||
val request = PutObjectRequest.builder()
|
val request = PutObjectRequest.builder()
|
||||||
|
|
|
||||||
|
|
@ -18,12 +18,9 @@ class MessageService(
|
||||||
@Transactional
|
@Transactional
|
||||||
@PatientAccess
|
@PatientAccess
|
||||||
fun addMessage(patientId: String, body: String): Message {
|
fun addMessage(patientId: String, body: String): Message {
|
||||||
val tenantId = TenantContext.getTenantId() ?: throw IllegalStateException("Missing tenant")
|
requireNotNull(TenantContext.getTenantId()) { "Missing tenant" }
|
||||||
val senderId = SecurityUtils.currentUserId() ?: throw IllegalStateException("Missing user")
|
val senderId = SecurityUtils.currentUserId() ?: throw IllegalStateException("Missing user")
|
||||||
val message = Message(UUID.randomUUID().toString(), patientId, senderId, body)
|
val message = Message(UUID.randomUUID().toString(), patientId, senderId, body)
|
||||||
message.tenantId = tenantId
|
|
||||||
message.createdBy = senderId
|
|
||||||
message.updatedBy = senderId
|
|
||||||
val saved = repository.save(message)
|
val saved = repository.save(message)
|
||||||
auditService.record("MESSAGE_ADDED", "message", saved.id, patientId)
|
auditService.record("MESSAGE_ADDED", "message", saved.id, patientId)
|
||||||
return saved
|
return saved
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue