Ensure tenant ID is properly set for patients, users, invitations, and patient-subject links in InvitationService; add corresponding unit tests.
This commit is contained in:
parent
c65d1031ed
commit
dc4bc979af
|
|
@ -40,13 +40,15 @@ class InvitationService(
|
||||||
@Transactional
|
@Transactional
|
||||||
@PreAuthorize("hasRole('ADMIN')")
|
@PreAuthorize("hasRole('ADMIN')")
|
||||||
fun createPatientInvite(email: String, createdByAdmin: String?): InviteCreationResult {
|
fun createPatientInvite(email: String, createdByAdmin: String?): InviteCreationResult {
|
||||||
requireNotNull(TenantContext.getTenantId()) { "Missing tenant" }
|
val tenantId = TenantContext.getTenantId() ?: throw IllegalArgumentException("Missing tenant")
|
||||||
|
|
||||||
val patient = Patient(UUID.randomUUID().toString(), generatePatientPlaceholderName())
|
val patient = Patient(UUID.randomUUID().toString(), generatePatientPlaceholderName())
|
||||||
|
patient.tenantId = tenantId
|
||||||
val savedPatient = patientRepository.save(patient)
|
val savedPatient = patientRepository.save(patient)
|
||||||
|
|
||||||
keycloakProvisioningService.provisionUser(email, Invitation.ROLE_PATIENT)?.let { userId ->
|
keycloakProvisioningService.provisionUser(email, Invitation.ROLE_PATIENT)?.let { userId ->
|
||||||
val user = User(userId, email, Invitation.ROLE_PATIENT, "INVITED")
|
val user = User(userId, email, Invitation.ROLE_PATIENT, "INVITED")
|
||||||
|
user.tenantId = tenantId
|
||||||
userRepository.save(user)
|
userRepository.save(user)
|
||||||
}
|
}
|
||||||
keycloakProvisioningService.sendSetPasswordEmail(email)
|
keycloakProvisioningService.sendSetPasswordEmail(email)
|
||||||
|
|
@ -64,6 +66,7 @@ class InvitationService(
|
||||||
acceptedBy = null,
|
acceptedBy = null,
|
||||||
createdByAdmin = createdByAdmin
|
createdByAdmin = createdByAdmin
|
||||||
)
|
)
|
||||||
|
invitation.tenantId = tenantId
|
||||||
invitationRepository.save(invitation)
|
invitationRepository.save(invitation)
|
||||||
return InviteCreationResult(token, invitation.expiresAt)
|
return InviteCreationResult(token, invitation.expiresAt)
|
||||||
}
|
}
|
||||||
|
|
@ -103,6 +106,7 @@ class InvitationService(
|
||||||
throw IllegalArgumentException("Invitation email mismatch")
|
throw IllegalArgumentException("Invitation email mismatch")
|
||||||
}
|
}
|
||||||
val newUser = User(authenticatedUserId, authenticatedEmail, invitation.role, "ACTIVE")
|
val newUser = User(authenticatedUserId, authenticatedEmail, invitation.role, "ACTIVE")
|
||||||
|
newUser.tenantId = TenantContext.getTenantId() ?: throw IllegalStateException("Missing tenant")
|
||||||
userRepository.save(newUser)
|
userRepository.save(newUser)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -140,6 +144,7 @@ class InvitationService(
|
||||||
val tenantId = TenantContext.getTenantId() ?: throw IllegalStateException("Missing tenant")
|
val tenantId = TenantContext.getTenantId() ?: throw IllegalStateException("Missing tenant")
|
||||||
if (!subjectRepository.existsByTenantIdAndPatientIdAndUserId(tenantId, patientId, userId)) {
|
if (!subjectRepository.existsByTenantIdAndPatientIdAndUserId(tenantId, patientId, userId)) {
|
||||||
val link = PatientSubject(UUID.randomUUID().toString(), patientId, userId)
|
val link = PatientSubject(UUID.randomUUID().toString(), patientId, userId)
|
||||||
|
link.tenantId = tenantId
|
||||||
subjectRepository.save(link)
|
subjectRepository.save(link)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,28 @@ class InvitationServiceTest {
|
||||||
assertEquals(Invitation.STATUS_ACCEPTED, invitation.status)
|
assertEquals(Invitation.STATUS_ACCEPTED, invitation.status)
|
||||||
assertNotNull(invitation.acceptedAt)
|
assertNotNull(invitation.acceptedAt)
|
||||||
assertEquals(user, invitation.acceptedBy)
|
assertEquals(user, invitation.acceptedBy)
|
||||||
|
org.mockito.kotlin.verify(subjectRepository).save(org.mockito.kotlin.check {
|
||||||
|
assertEquals("t1", it.tenantId)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `creates patient invite with tenantId`() {
|
||||||
|
val email = "new@example.com"
|
||||||
|
whenever(patientRepository.save(any<Patient>())).thenAnswer { it.arguments[0] as Patient }
|
||||||
|
whenever(userRepository.save(any<User>())).thenAnswer { it.arguments[0] as User }
|
||||||
|
whenever(invitationRepository.save(any<Invitation>())).thenAnswer { it.arguments[0] as Invitation }
|
||||||
|
|
||||||
|
val result = service.createPatientInvite(email, "admin-1")
|
||||||
|
|
||||||
|
assertNotNull(result.token)
|
||||||
|
org.mockito.kotlin.verify(patientRepository).save(org.mockito.kotlin.check<Patient> {
|
||||||
|
assertEquals("t1", it.tenantId)
|
||||||
|
})
|
||||||
|
org.mockito.kotlin.verify(invitationRepository).save(org.mockito.kotlin.check<Invitation> {
|
||||||
|
assertEquals("t1", it.tenantId)
|
||||||
|
assertEquals(email, it.email)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue