feat(selector): filter send accounts by account type
This commit is contained in:
@@ -16,6 +16,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -39,7 +40,14 @@ public class AccountSelector {
|
||||
return CollectionUtils.isEmpty(accounts) ? null : accounts.get(0);
|
||||
}
|
||||
|
||||
public List<Long> selectSendAccounts(String tenantCode, Long conversationId, ChannelType channelType, String preferredStrategy) {
|
||||
public List<Long> selectSendAccounts(String tenantCode, Long conversationId,
|
||||
ChannelType channelType, String preferredStrategy) {
|
||||
return selectSendAccounts(tenantCode, conversationId, channelType, null, preferredStrategy);
|
||||
}
|
||||
|
||||
public List<Long> selectSendAccounts(String tenantCode, Long conversationId,
|
||||
ChannelType channelType, Set<Integer> accountTypes,
|
||||
String preferredStrategy) {
|
||||
if (conversationId == null || channelType == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
@@ -64,9 +72,13 @@ public class AccountSelector {
|
||||
List<Long> healthyAccountIds = new ArrayList<>();
|
||||
for (ChannelAccountConversation binding : bindings) {
|
||||
ChannelAccount account = accountMapper.selectById(binding.getAccountId());
|
||||
if (isHealthy(account, tenantCode, channelType)) {
|
||||
healthyAccountIds.add(account.getId());
|
||||
if (!isHealthy(account, tenantCode, channelType)) {
|
||||
continue;
|
||||
}
|
||||
if (accountTypes != null && !accountTypes.contains(account.getAccountType())) {
|
||||
continue;
|
||||
}
|
||||
healthyAccountIds.add(account.getId());
|
||||
}
|
||||
return healthyAccountIds;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,8 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
@@ -86,6 +88,20 @@ class AccountSelectorTest {
|
||||
assertEquals(2L, accountId);
|
||||
}
|
||||
|
||||
@Test
|
||||
void selectSendAccountsFiltersByAccountType() {
|
||||
ChannelAccountConversation primary = createBinding(1L, 1, 1);
|
||||
ChannelAccountConversation backup = createBinding(2L, 2, 1);
|
||||
when(accountConversationMapper.selectList(any())).thenReturn(Arrays.asList(primary, backup));
|
||||
when(accountMapper.selectById(1L)).thenReturn(createHealthyAccount(1L, 1));
|
||||
when(accountMapper.selectById(2L)).thenReturn(createHealthyAccount(2L, 3));
|
||||
|
||||
List<Long> accountIds = accountSelector.selectSendAccounts(
|
||||
"tenant", 100L, ChannelType.WECHAT_PERSONAL, Set.of(1), "primary");
|
||||
|
||||
assertEquals(Collections.singletonList(1L), accountIds);
|
||||
}
|
||||
|
||||
private ChannelAccountConversation createBinding(Long accountId, int bindingType, int sortOrder) {
|
||||
ChannelAccountConversation binding = new ChannelAccountConversation();
|
||||
binding.setAccountId(accountId);
|
||||
@@ -96,10 +112,14 @@ class AccountSelectorTest {
|
||||
}
|
||||
|
||||
private ChannelAccount createHealthyAccount(Long id) {
|
||||
return createHealthyAccount(id, 3);
|
||||
}
|
||||
|
||||
private ChannelAccount createHealthyAccount(Long id, int accountType) {
|
||||
ChannelAccount account = new ChannelAccount();
|
||||
account.setId(id);
|
||||
account.setTenantCode("tenant");
|
||||
account.setAccountType(3);
|
||||
account.setAccountType(accountType);
|
||||
account.setChannelType("wechat_personal");
|
||||
account.setAccountId("wxid-" + id);
|
||||
account.setStatus(1);
|
||||
|
||||
Reference in New Issue
Block a user