SUPPORT-9212: fix

This commit is contained in:
adel.kalimullin 2025-06-09 15:02:00 +03:00
parent 7bfd61e63e
commit e0c6544e6c
5 changed files with 24 additions and 15 deletions

View file

@ -2,6 +2,7 @@ package ervu_business_metrics.dao;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.jooq.*; import org.jooq.*;
import org.jooq.impl.DSL; import org.jooq.impl.DSL;
@ -54,12 +55,12 @@ public abstract class AbstractDataDao<T extends UpdatableRecord<T>> {
.execute(); .execute();
} }
protected <S, F> List<S> getValuesByField(Field<S> selectField, Field<F> filterField, protected <S, F> Set<S> getValuesByField(Field<S> selectField, Field<F> filterField,
F filterValue) { F filterValue) {
return dsl.select(selectField) return dsl.select(selectField)
.from(getTable()) .from(getTable())
.where(filterField.eq(filterValue)) .where(filterField.eq(filterValue))
.fetch(selectField); .fetchSet(selectField);
} }
public void setActiveStatus(String id, boolean isActive) { public void setActiveStatus(String id, boolean isActive) {
@ -79,4 +80,5 @@ public abstract class AbstractDataDao<T extends UpdatableRecord<T>> {
public void mergeRecords(List<T> records) { public void mergeRecords(List<T> records) {
dsl.batchMerge(records).execute(); dsl.batchMerge(records).execute();
} }
} }

View file

@ -1,6 +1,7 @@
package ervu_business_metrics.dao; package ervu_business_metrics.dao;
import java.util.List; import java.util.List;
import java.util.Set;
import org.jooq.DSLContext; import org.jooq.DSLContext;
import org.jooq.Table; import org.jooq.Table;
@ -19,7 +20,7 @@ public class AccountDataDao extends AbstractDataDao<AccountRecord> {
super(dsl); super(dsl);
} }
public List<String> getAccountIdsByPersonId(String personId) { public Set<String> getAccountIdsByPersonId(String personId) {
return getValuesByField(Tables.ACCOUNT.ID, Tables.ACCOUNT.PERSON_ID, personId); return getValuesByField(Tables.ACCOUNT.ID, Tables.ACCOUNT.PERSON_ID, personId);
} }

View file

@ -3,6 +3,7 @@ package ervu_business_metrics.dao;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.jooq.DSLContext; import org.jooq.DSLContext;
import org.jooq.Field; import org.jooq.Field;
@ -22,7 +23,7 @@ public class AccountRoleDataDao extends AbstractDataDao<AccountRoleRecord> {
super(dsl); super(dsl);
} }
public List<String> getRoleIdsByAccountId(String accountId) { public Set<String> getRoleIdsByAccountId(String accountId) {
return getValuesByField(Tables.ACCOUNT_ROLE.ROLE_ID, Tables.ACCOUNT_ROLE.ACCOUNT_ID, accountId); return getValuesByField(Tables.ACCOUNT_ROLE.ROLE_ID, Tables.ACCOUNT_ROLE.ACCOUNT_ID, accountId);
} }

View file

@ -84,7 +84,7 @@ public class AccountDataProcessor implements DataProcessor<AccountData, AccountR
} }
private void deleteAbsentAccounts(String personId, Set<String> incomingAccountIds) { private void deleteAbsentAccounts(String personId, Set<String> incomingAccountIds) {
List<String> existingAccountIds = dao.getAccountIdsByPersonId(personId); Set<String> existingAccountIds = dao.getAccountIdsByPersonId(personId);
List<String> toDelete = existingAccountIds.stream() List<String> toDelete = existingAccountIds.stream()
.filter(id -> !incomingAccountIds.contains(id)) .filter(id -> !incomingAccountIds.contains(id))

View file

@ -49,11 +49,11 @@ public class AccountRoleDataProcessor implements LinkDataProcessor<AccountRoleDa
} }
public void upsertAccountRoles(List<AccountData> accounts) { public void upsertAccountRoles(List<AccountData> accounts) {
List<AccountRoleRecord> allRoleRecords = new ArrayList<>(); List<AccountRoleRecord> accountRoleRecords = new ArrayList<>();
for (AccountData account : accounts) { for (AccountData account : accounts) {
List<String> existingRoleIds = dao.getRoleIdsByAccountId(account.getId()); String accountId = account.getId();
Set<String> existingRoleIds = dao.getRoleIdsByAccountId(accountId);
Set<String> incomingRoleIds = account.getRoles() == null Set<String> incomingRoleIds = account.getRoles() == null
? Set.of() ? Set.of()
: account.getRoles() : account.getRoles()
@ -66,18 +66,23 @@ public class AccountRoleDataProcessor implements LinkDataProcessor<AccountRoleDa
.toList(); .toList();
if (!toDelete.isEmpty()) { if (!toDelete.isEmpty()) {
dao.deleteAccountRolesByAccountIdAndRoleIds(account.getId(), toDelete); dao.deleteAccountRolesByAccountIdAndRoleIds(accountId, toDelete);
} }
if (!incomingRoleIds.isEmpty()) { List<String> toAdd = incomingRoleIds.stream()
for (ReferenceEntity role : account.getRoles()) { .filter(roleId -> !existingRoleIds.contains(roleId))
allRoleRecords.add(mapToRecord(new AccountRoleData(account.getId(), role.getId()))); .toList();
}
if (!toAdd.isEmpty()) {
List<AccountRoleRecord> newRecords = toAdd.stream()
.map(roleId -> mapToRecord(new AccountRoleData(accountId, roleId)))
.toList();
accountRoleRecords.addAll(newRecords);
} }
} }
if (!allRoleRecords.isEmpty()) { if (!accountRoleRecords.isEmpty()) {
dao.mergeRecords(allRoleRecords); dao.mergeRecords(accountRoleRecords);
} }
} }