Maintenance code clean up

This commit is contained in:
amentis 2024-07-23 17:05:25 +03:00
parent 117bc22515
commit b230db5e66
1 changed files with 29 additions and 48 deletions

View File

@ -213,22 +213,14 @@ public class MaintenanceServiceImpl implements MaintenanceService {
try {
this.tenantEntityManager.disableTenantFilters();
List<PlanEntity> items = this.queryFactory.query(PlanQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Plan._id).ensure(Plan._isActive).ensure(Plan._creator));
List<PlanEntity> items = this.queryFactory.query(PlanQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(Plan._id).ensure(Plan._isActive).ensure(Plan._creator).ensure(Plan._belongsToCurrentTenant));
List<UserCredentialEntity> userCredentials = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(items.stream().map(x -> x.getCreatorId()).distinct().collect(Collectors.toList())).collect();
List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
for (PlanEntity item : items) {
String subjectId;
if (userCredentials != null) {
UserCredentialEntity userCredential = userCredentials.stream().filter(x -> x.getUserId().equals(item.getCreatorId())).findFirst().orElse(null);
if (userCredential != null) subjectId = userCredential.getExternalId();
else subjectId = item.getCreatorId().toString();
} else {
subjectId = item.getCreatorId().toString();
}
String subjectId = this.findUserCredential(userCredentials, item.getCreatorId());
String tenantCode = this.findTenantCode(tenants, item.getTenantId());
if (item.getIsActive().equals(IsActive.Active)) this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.PLAN_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode);
else this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.PLAN_COUNT.getValue(), AccountingValueType.Minus, subjectId, item.getTenantId(), tenantCode);
this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.PLAN_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode);
}
} finally {
this.tenantEntityManager.reloadTenantFilters();
@ -242,14 +234,13 @@ public class MaintenanceServiceImpl implements MaintenanceService {
try {
this.tenantEntityManager.disableTenantFilters();
List<PlanBlueprintEntity> items = this.queryFactory.query(PlanBlueprintQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(PlanBlueprint._id).ensure(PlanBlueprint._isActive));
List<PlanBlueprintEntity> items = this.queryFactory.query(PlanBlueprintQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(PlanBlueprint._id).ensure(PlanBlueprint._isActive).ensure(PlanBlueprint._belongsToCurrentTenant));
List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
for (PlanBlueprintEntity item : items) {
String subjectId = accountingProperties.getSubjectId();
String tenantCode = this.findTenantCode(tenants, item.getTenantId());
if (item.getIsActive().equals(IsActive.Active)) this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.BLUEPRINT_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode);
else this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.BLUEPRINT_COUNT.getValue(), AccountingValueType.Minus, subjectId, item.getTenantId(), tenantCode);
this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.BLUEPRINT_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode);
}
} finally {
this.tenantEntityManager.reloadTenantFilters();
@ -263,22 +254,14 @@ public class MaintenanceServiceImpl implements MaintenanceService {
try {
this.tenantEntityManager.disableTenantFilters();
List<DescriptionEntity> items = this.queryFactory.query(DescriptionQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Description._id).ensure(Description._isActive).ensure(Description._createdBy));
List<DescriptionEntity> items = this.queryFactory.query(DescriptionQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(Description._id).ensure(Description._isActive).ensure(Description._createdBy).ensure(Description._belongsToCurrentTenant));
List<UserCredentialEntity> userCredentials = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(items.stream().map(x -> x.getCreatedById()).distinct().collect(Collectors.toList())).collect();
List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
for (DescriptionEntity item : items) {
String subjectId;
if (userCredentials != null) {
UserCredentialEntity userCredential = userCredentials.stream().filter(x -> x.getUserId().equals(item.getCreatedById())).findFirst().orElse(null);
if (userCredential != null) subjectId = userCredential.getExternalId();
else subjectId = item.getCreatedById().toString();
} else {
subjectId = item.getCreatedById().toString();
}
String subjectId = this.findUserCredential(userCredentials, item.getCreatedById());
String tenantCode = this.findTenantCode(tenants, item.getTenantId());
if (item.getIsActive().equals(IsActive.Active)) this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode);
else this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_COUNT.getValue(), AccountingValueType.Minus, subjectId, item.getTenantId(), tenantCode);
this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode);
}
} finally {
this.tenantEntityManager.reloadTenantFilters();
@ -292,14 +275,13 @@ public class MaintenanceServiceImpl implements MaintenanceService {
try {
this.tenantEntityManager.disableTenantFilters();
List<DescriptionTemplateEntity> items = this.queryFactory.query(DescriptionTemplateQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(DescriptionTemplate._id).ensure(DescriptionTemplate._isActive));
List<DescriptionTemplateEntity> items = this.queryFactory.query(DescriptionTemplateQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(DescriptionTemplate._id).ensure(DescriptionTemplate._isActive).ensure(DescriptionTemplate._belongsToCurrentTenant));
List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
for (DescriptionTemplateEntity item : items) {
String subjectId = accountingProperties.getSubjectId();
String tenantCode = this.findTenantCode(tenants, item.getTenantId());
if (item.getIsActive().equals(IsActive.Active)) this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode);
else this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_COUNT.getValue(), AccountingValueType.Minus, subjectId, item.getTenantId(), tenantCode);
this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode);
}
} finally {
this.tenantEntityManager.reloadTenantFilters();
@ -313,14 +295,13 @@ public class MaintenanceServiceImpl implements MaintenanceService {
try {
this.tenantEntityManager.disableTenantFilters();
List<DescriptionTemplateTypeEntity> items = this.queryFactory.query(DescriptionTemplateTypeQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(DescriptionTemplateType._id).ensure(DescriptionTemplateType._isActive));
List<DescriptionTemplateTypeEntity> items = this.queryFactory.query(DescriptionTemplateTypeQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(DescriptionTemplateType._id).ensure(DescriptionTemplateType._isActive).ensure(DescriptionTemplateType._belongsToCurrentTenant));
List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
for (DescriptionTemplateTypeEntity item : items) {
String subjectId = accountingProperties.getSubjectId();
String tenantCode = this.findTenantCode(tenants, item.getTenantId());
if (item.getIsActive().equals(IsActive.Active)) this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_TYPE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode);
else this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_TYPE_COUNT.getValue(), AccountingValueType.Minus, subjectId, item.getTenantId(), tenantCode);
this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.DESCRIPTION_TEMPLATE_TYPE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode);
}
} finally {
this.tenantEntityManager.reloadTenantFilters();
@ -334,14 +315,13 @@ public class MaintenanceServiceImpl implements MaintenanceService {
try {
this.tenantEntityManager.disableTenantFilters();
List<PrefillingSourceEntity> items = this.queryFactory.query(PrefillingSourceQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(PrefillingSource._id).ensure(PrefillingSource._isActive));
List<PrefillingSourceEntity> items = this.queryFactory.query(PrefillingSourceQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(PrefillingSource._id).ensure(PrefillingSource._isActive).ensure(PrefillingSource._belongsToCurrentTenant));
List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
for (PrefillingSourceEntity item : items) {
String subjectId = accountingProperties.getSubjectId();
String tenantCode = this.findTenantCode(tenants, item.getTenantId());
if (item.getIsActive().equals(IsActive.Active)) this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.PREFILLING_SOURCES_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode);
else this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.PREFILLING_SOURCES_COUNT.getValue(), AccountingValueType.Minus, subjectId, item.getTenantId(), tenantCode);
this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.PREFILLING_SOURCES_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode);
}
} finally {
this.tenantEntityManager.reloadTenantFilters();
@ -355,14 +335,13 @@ public class MaintenanceServiceImpl implements MaintenanceService {
try {
this.tenantEntityManager.disableTenantFilters();
List<ReferenceTypeEntity> items = this.queryFactory.query(ReferenceTypeQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(ReferenceType._id).ensure(ReferenceType._isActive));
List<ReferenceTypeEntity> items = this.queryFactory.query(ReferenceTypeQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(ReferenceType._id).ensure(ReferenceType._isActive).ensure(ReferenceType._belongsToCurrentTenant));
List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
for (ReferenceTypeEntity item : items) {
String subjectId = accountingProperties.getSubjectId();
String tenantCode = this.findTenantCode(tenants, item.getTenantId());
if (item.getIsActive().equals(IsActive.Active)) this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.REFERENCE_TYPE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode);
else this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.REFERENCE_TYPE_COUNT.getValue(), AccountingValueType.Minus, subjectId, item.getTenantId(), tenantCode);
this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.REFERENCE_TYPE_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode);
}
} finally {
this.tenantEntityManager.reloadTenantFilters();
@ -376,22 +355,14 @@ public class MaintenanceServiceImpl implements MaintenanceService {
try {
this.tenantEntityManager.disableTenantFilters();
List<TenantUserEntity> items = this.queryFactory.query(TenantUserQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(TenantUserEntity._id).ensure(TenantUserEntity._userId).ensure(TenantUserEntity._isActive));
List<TenantUserEntity> items = this.queryFactory.query(TenantUserQuery.class).disableTracking().isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(TenantUserEntity._id).ensure(TenantUserEntity._userId).ensure(TenantUserEntity._isActive).ensure(TenantUserEntity._tenantId));
List<UserCredentialEntity> userCredentials = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(items.stream().map(x -> x.getUserId()).distinct().collect(Collectors.toList())).collect();
List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
for (TenantUserEntity item : items) {
String subjectId;
if (userCredentials != null) {
UserCredentialEntity userCredential = userCredentials.stream().filter(x -> x.getUserId().equals(item.getUserId())).findFirst().orElse(null);
if (userCredential != null) subjectId = userCredential.getExternalId();
else subjectId = item.getUserId().toString();
} else {
subjectId = item.getUserId().toString();
}
String subjectId = this.findUserCredential(userCredentials, item.getUserId());
String tenantCode = this.findTenantCode(tenants, item.getTenantId());
if (item.getIsActive().equals(IsActive.Active)) this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.USER_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode);
else this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.USER_COUNT.getValue(), AccountingValueType.Minus, subjectId, item.getTenantId(), tenantCode);
this.accountingEntryCreatedIntegrationEventHandler.handleAccountingEntry(UsageLimitTargetMetric.USER_COUNT.getValue(), AccountingValueType.Plus, subjectId, item.getTenantId(), tenantCode);
}
} finally {
this.tenantEntityManager.reloadTenantFilters();
@ -407,4 +378,14 @@ public class MaintenanceServiceImpl implements MaintenanceService {
return this.tenantScope.getDefaultTenantCode();
}
}
private String findUserCredential(List<UserCredentialEntity> userCredentials, UUID userId) {
if (userCredentials != null) {
UserCredentialEntity userCredential = userCredentials.stream().filter(x -> x.getUserId().equals(userId)).findFirst().orElse(null);
if (userCredential != null) return userCredential.getExternalId();
else return userId.toString();
} else {
return userId.toString();
}
}
}