Skip to content

Commit ec74dc5

Browse files
Merge pull request #460 from PSMRI/vb/stoptb-3.12.0
fix(user): do not wait for the CTI server when a password is set (StopTB camp laptops)
2 parents 54080cc + 2701b01 commit ec74dc5

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.iemr.common-API</groupId>
88
<artifactId>common-api</artifactId>
9-
<version>3.8.2</version>
9+
<version>3.12.0</version>
1010
<packaging>war</packaging>
1111

1212
<name>Common-API</name>

‎src/main/java/com/iemr/common/service/users/IEMRAdminUserServiceImpl.java‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.security.NoSuchAlgorithmException;
2727
import java.security.SecureRandom;
2828
import java.security.spec.InvalidKeySpecException;
29+
import java.util.concurrent.CompletableFuture;
2930
import java.util.ArrayList;
3031
import java.util.HashMap;
3132
import java.util.HashSet;
@@ -469,15 +470,15 @@ public int setForgetPassword(User user, String loginpass, String transactionId,
469470
int count = 0;
470471
try {
471472
if (isAdmin != null && isAdmin == true) {
472-
updateCTIPasswordForUserV1(user.getUserID(), loginpass);
473+
updateCTIPasswordInBackground(user.getUserID(), loginpass);
473474
loginpass = securePassword.generateStrongPassword(loginpass);
474475
count = iEMRUserRepositoryCustom.updateSetForgetPassword(user.getUserID(), loginpass);
475476
} else {
476477
String tokenFromRedis = sessionObject
477478
.getSessionObjectForChangePassword((user.getUserID().toString() + user.getUserName()));
478479
if (tokenFromRedis.equalsIgnoreCase(transactionId)) {
479480

480-
updateCTIPasswordForUserV1(user.getUserID(), loginpass);
481+
updateCTIPasswordInBackground(user.getUserID(), loginpass);
481482
loginpass = securePassword.generateStrongPassword(loginpass);
482483
count = iEMRUserRepositoryCustom.updateSetForgetPassword(user.getUserID(), loginpass);
483484
// Deleting transaction Id
@@ -600,6 +601,15 @@ public void updateCTIPasswordForUser(Long userID, String password) {
600601
}
601602
}
602603

604+
/**
605+
* Sends the new password to the CTI server without making the user wait: called from this class, so the
606+
* {@code @Async} of updateCTIPasswordForUserV1 is not applied (no proxy) and an unreachable CTI server held the
607+
* password screen until the network timeout - before the password was even saved.
608+
*/
609+
void updateCTIPasswordInBackground(Long userID, String password) {
610+
CompletableFuture.runAsync(() -> updateCTIPasswordForUserV1(userID, password));
611+
}
612+
603613
@Async
604614
public void updateCTIPasswordForUserV1(Long userID, String password) {
605615
try {

‎src/test/java/com/iemr/common/service/users/IEMRAdminUserServiceImplTest.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,28 @@ void testSetForgetPassword_ValidTransaction() throws Exception {
374374
assertEquals(1, result);
375375
}
376376

377+
@Test
378+
void testSetForgetPassword_DoesNotWaitForCtiServer() throws Exception {
379+
// Given: the CTI server does not answer until released
380+
java.util.concurrent.CountDownLatch ctiReleased = new java.util.concurrent.CountDownLatch(1);
381+
when(iEMRUserRepositoryCustom.findUserByUserID(1L)).thenReturn(testUser);
382+
when(ctiService.addUpdateUserData(anyString(), anyString())).thenAnswer(invocation -> {
383+
ctiReleased.await(10, java.util.concurrent.TimeUnit.SECONDS);
384+
return new OutputResponse();
385+
});
386+
when(securePassword.generateStrongPassword("newpassword")).thenReturn("hashedNewPassword");
387+
when(iEMRUserRepositoryCustom.updateSetForgetPassword(1L, "hashedNewPassword")).thenReturn(1);
388+
389+
// When: the password is saved without waiting for CTI
390+
int result = assertTimeoutPreemptively(java.time.Duration.ofSeconds(2),
391+
() -> userService.setForgetPassword(testUser, "newpassword", "token123", true));
392+
393+
// Then: saved, and CTI is still updated in the background
394+
assertEquals(1, result);
395+
ctiReleased.countDown();
396+
verify(ctiService, timeout(2000)).addUpdateUserData(anyString(), eq("127.0.0.1"));
397+
}
398+
377399
@Test
378400
void testSetForgetPassword_InvalidTransaction() throws Exception {
379401
// Given

0 commit comments

Comments
 (0)