Remove redundant tenant and user metadata assignments across services.

This commit is contained in:
oskar 2026-01-12 23:13:22 +01:00
parent bbd7a371dd
commit 553ae2bd69
5 changed files with 4 additions and 38 deletions

View file

@ -1,8 +1,6 @@
package com.mosenioring.common.outbox
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.stereotype.Service
import java.util.UUID
@ -14,15 +12,11 @@ class OutboxService(
private val objectMapper: ObjectMapper
) {
fun enqueue(eventType: String, payload: Any): OutboxEvent {
val tenantId = TenantContext.getTenantId() ?: "unknown"
val event = OutboxEvent(
id = UUID.randomUUID().toString(),
eventType = eventType,
payload = objectMapper.writeValueAsString(payload)
)
event.tenantId = tenantId
event.createdBy = SecurityUtils.currentUserId()
event.updatedBy = SecurityUtils.currentUserId()
return repository.save(event)
}
}

View file

@ -8,7 +8,6 @@ import com.mosenioring.clinical.repo.TestOrderRepository
import com.mosenioring.common.Events
import com.mosenioring.common.outbox.OutboxService
import com.mosenioring.common.security.PatientAccess
import com.mosenioring.common.security.SecurityUtils
import com.mosenioring.common.tenant.TenantContext
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.stereotype.Service
@ -26,9 +25,6 @@ class MedicationPlanService(
fun createMedicationPlan(patientId: String, description: String): MedicationPlan {
val tenantId = TenantContext.getTenantId() ?: throw IllegalStateException("Missing tenant")
val plan = MedicationPlan(UUID.randomUUID().toString(), patientId, description)
plan.tenantId = tenantId
plan.createdBy = SecurityUtils.currentUserId()
plan.updatedBy = SecurityUtils.currentUserId()
val saved = repository.save(plan)
outboxService.enqueue(
Events.MEDICATION_PLAN_CREATED,
@ -52,11 +48,8 @@ class TestOrderService(
@Transactional
@PatientAccess
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")
order.tenantId = tenantId
order.createdBy = SecurityUtils.currentUserId()
order.updatedBy = SecurityUtils.currentUserId()
val saved = repository.save(order)
auditService.record("TEST_ORDER_CREATED", "test", saved.id, patientId)
return saved

View file

@ -1,7 +1,6 @@
package com.mosenioring.identity.service
import com.mosenioring.audit.service.AuditService
import com.mosenioring.common.security.SecurityUtils
import com.mosenioring.common.tenant.TenantContext
import com.mosenioring.identity.*
import com.mosenioring.identity.repo.*
@ -20,8 +19,6 @@ class TenantService(
fun createTenant(name: String): Tenant {
val tenant = Tenant(UUID.randomUUID().toString(), name)
tenant.tenantId = tenant.id
tenant.createdBy = SecurityUtils.currentUserId()
tenant.updatedBy = SecurityUtils.currentUserId()
val saved = tenantRepository.save(tenant)
auditService.record("TENANT_CREATED", "tenant", saved.id, null)
return saved
@ -36,11 +33,8 @@ class UserService(
@Transactional
@PreAuthorize("hasRole('ADMIN')")
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")
user.tenantId = tenantId
user.createdBy = SecurityUtils.currentUserId()
user.updatedBy = SecurityUtils.currentUserId()
val saved = userRepository.save(user)
auditService.record("USER_INVITED", "user", saved.id, null)
return saved
@ -57,11 +51,8 @@ class PatientService(
@Transactional
@PreAuthorize("hasAnyRole('ADMIN','DOCTOR','CAREGIVER')")
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)
patient.tenantId = tenantId
patient.createdBy = SecurityUtils.currentUserId()
patient.updatedBy = SecurityUtils.currentUserId()
val saved = patientRepository.save(patient)
auditService.record("PATIENT_CREATED", "patient", saved.id, saved.id)
return saved
@ -73,9 +64,6 @@ class PatientService(
val tenantId = TenantContext.getTenantId() ?: throw IllegalStateException("Missing tenant")
patientRepository.findByIdAndTenantId(patientId, tenantId) ?: throw IllegalArgumentException("Patient not found")
val link = PatientCaregiver(UUID.randomUUID().toString(), patientId, userId)
link.tenantId = tenantId
link.createdBy = SecurityUtils.currentUserId()
link.updatedBy = SecurityUtils.currentUserId()
val saved = caregiverRepository.save(link)
auditService.record("CARE_GIVER_LINKED", "patient_caregiver", saved.id, patientId)
return saved
@ -87,9 +75,6 @@ class PatientService(
val tenantId = TenantContext.getTenantId() ?: throw IllegalStateException("Missing tenant")
patientRepository.findByIdAndTenantId(patientId, tenantId) ?: throw IllegalArgumentException("Patient not found")
val link = PatientDoctor(UUID.randomUUID().toString(), patientId, userId)
link.tenantId = tenantId
link.createdBy = SecurityUtils.currentUserId()
link.updatedBy = SecurityUtils.currentUserId()
val saved = doctorRepository.save(link)
auditService.record("DOCTOR_LINKED", "patient_doctor", saved.id, patientId)
return saved

View file

@ -43,9 +43,6 @@ class FileService(
val fileId = UUID.randomUUID().toString()
val storageKey = "$tenantId/$fileId/$fileName"
val metadata = FileMetadata(fileId, patientId, fileName, contentType, storageKey)
metadata.tenantId = tenantId
metadata.createdBy = SecurityUtils.currentUserId()
metadata.updatedBy = SecurityUtils.currentUserId()
repository.save(metadata)
val request = PutObjectRequest.builder()

View file

@ -18,12 +18,9 @@ class MessageService(
@Transactional
@PatientAccess
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 message = Message(UUID.randomUUID().toString(), patientId, senderId, body)
message.tenantId = tenantId
message.createdBy = senderId
message.updatedBy = senderId
val saved = repository.save(message)
auditService.record("MESSAGE_ADDED", "message", saved.id, patientId)
return saved