add more accounting target metrics

This commit is contained in:
amentis 2024-07-16 17:49:01 +03:00
parent cd9a76070a
commit a6055d32f8
8 changed files with 71 additions and 21 deletions

View File

@ -21,7 +21,14 @@ public enum UsageLimitTargetMetric implements DatabaseEnum<String> {
FILE_TRANSFORMER_EXPORT_DESCRIPTIONS_EXECUTION_COUNT(TargetMetrics.FileTransformerExportDescriptionExecutionCount), FILE_TRANSFORMER_EXPORT_DESCRIPTIONS_EXECUTION_COUNT(TargetMetrics.FileTransformerExportDescriptionExecutionCount),
FILE_TRANSFORMER_EXPORT_DESCRIPTIONS_EXECUTION_COUNT_FOR(TargetMetrics.FileTransformerExportDescriptionExecutionCountFor_), FILE_TRANSFORMER_EXPORT_DESCRIPTIONS_EXECUTION_COUNT_FOR(TargetMetrics.FileTransformerExportDescriptionExecutionCountFor_),
FILE_TRANSFORMER_IMPORT_PLAN_EXECUTION_COUNT(TargetMetrics.FileTransformerImportPlanExecutionCount), FILE_TRANSFORMER_IMPORT_PLAN_EXECUTION_COUNT(TargetMetrics.FileTransformerImportPlanExecutionCount),
FILE_TRANSFORMER_IMPORT_PLAN_EXECUTION_COUNT_FOR(TargetMetrics.FileTransformerImportPlanExecutionCountFor_); FILE_TRANSFORMER_IMPORT_PLAN_EXECUTION_COUNT_FOR(TargetMetrics.FileTransformerImportPlanExecutionCountFor_),
EXPORT_PLAN_XML_EXECUTION_COUNT(TargetMetrics.ExportPlanXMLExecutionCount),
EXPORT_DESCRIPTION_XML_EXECUTION_COUNT(TargetMetrics.ExportDescriptionXMLExecutionCount),
EXPORT_BLUEPRINT_XML_EXECUTION_COUNT(TargetMetrics.ExportBlueprintXMLExecutionCount),
EXPORT_DESCRIPTION_TEMPLATE_XML_EXECUTION_COUNT(TargetMetrics.ExportDescriptionTemplateXMLExecutionCount),
IMPORT_PLAN_XML_EXECUTION_COUNT(TargetMetrics.ImportPlanXMLExecutionCount),
IMPORT_BLUEPRINT_XML_EXECUTION_COUNT(TargetMetrics.ImportBlueprintXMLExecutionCount),
IMPORT_DESCRIPTION_TEMPLATE_XML_EXECUTION_COUNT(TargetMetrics.ImportDescriptionTemplateXMLExecutionCount);
private final String value; private final String value;
public static class TargetMetrics { public static class TargetMetrics {
@ -41,6 +48,13 @@ public enum UsageLimitTargetMetric implements DatabaseEnum<String> {
public static final String FileTransformerExportDescriptionExecutionCountFor_ = "file_transformer_export_description_execution_count_for_"; public static final String FileTransformerExportDescriptionExecutionCountFor_ = "file_transformer_export_description_execution_count_for_";
public static final String FileTransformerImportPlanExecutionCount = "file_transformer_import_plan_execution_count"; public static final String FileTransformerImportPlanExecutionCount = "file_transformer_import_plan_execution_count";
public static final String FileTransformerImportPlanExecutionCountFor_ = "file_transformer_import_plan_execution_count_for_"; public static final String FileTransformerImportPlanExecutionCountFor_ = "file_transformer_import_plan_execution_count_for_";
public static final String ExportPlanXMLExecutionCount = "export_plan_xml_execution_count";
public static final String ExportDescriptionXMLExecutionCount = "export_description_xml_execution_count";
public static final String ExportBlueprintXMLExecutionCount = "export_blueprint_xml_execution_count";
public static final String ExportDescriptionTemplateXMLExecutionCount = "export_description_template_xml_execution_count";
public static final String ImportPlanXMLExecutionCount = "import_plan_xml_execution_count";
public static final String ImportBlueprintXMLExecutionCount = "import_blueprint_xml_execution_count";
public static final String ImportDescriptionTemplateXMLExecutionCount = "import_description_template_xml_execution_count";
} }
UsageLimitTargetMetric(String value) { UsageLimitTargetMetric(String value) {

View File

@ -5,11 +5,13 @@ import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry; import gr.cite.tools.logging.MapLogEntry;
import org.opencdmp.commons.enums.IsActive; import org.opencdmp.commons.enums.IsActive;
import org.opencdmp.commons.enums.UsageLimitTargetMetric;
import org.opencdmp.data.TenantEntityManager; import org.opencdmp.data.TenantEntityManager;
import org.opencdmp.data.TenantUserEntity; import org.opencdmp.data.TenantUserEntity;
import org.opencdmp.event.EventBroker; import org.opencdmp.event.EventBroker;
import org.opencdmp.event.UserRemovedFromTenantEvent; import org.opencdmp.event.UserRemovedFromTenantEvent;
import org.opencdmp.query.TenantUserQuery; import org.opencdmp.query.TenantUserQuery;
import org.opencdmp.service.accounting.AccountingService;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
@ -30,15 +32,17 @@ public class TenantUserDeleter implements Deleter {
private final TenantEntityManager entityManager; private final TenantEntityManager entityManager;
private final QueryFactory queryFactory; private final QueryFactory queryFactory;
private final EventBroker eventBroker; private final EventBroker eventBroker;
private final AccountingService accountingService;
@Autowired @Autowired
public TenantUserDeleter( public TenantUserDeleter(
TenantEntityManager entityManager, TenantEntityManager entityManager,
QueryFactory queryFactory, EventBroker eventBroker QueryFactory queryFactory, EventBroker eventBroker,
) { AccountingService accountingService) {
this.entityManager = entityManager; this.entityManager = entityManager;
this.queryFactory = queryFactory; this.queryFactory = queryFactory;
this.eventBroker = eventBroker; this.eventBroker = eventBroker;
this.accountingService = accountingService;
} }
public void deleteAndSaveByIds(List<UUID> ids) throws InvalidApplicationException { public void deleteAndSaveByIds(List<UUID> ids) throws InvalidApplicationException {
@ -70,6 +74,7 @@ public class TenantUserDeleter implements Deleter {
this.entityManager.merge(item); this.entityManager.merge(item);
logger.trace("updated item"); logger.trace("updated item");
this.eventBroker.emit(new UserRemovedFromTenantEvent(item.getUserId(), item.getTenantId())); this.eventBroker.emit(new UserRemovedFromTenantEvent(item.getUserId(), item.getTenantId()));
this.accountingService.decrease(UsageLimitTargetMetric.USER_COUNT.getValue());
} }
} }
} }

View File

@ -1184,6 +1184,7 @@ public class DescriptionServiceImpl implements DescriptionService {
if (data == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{id, Description.class.getSimpleName()}, LocaleContextHolder.getLocale())); if (data == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{id, Description.class.getSimpleName()}, LocaleContextHolder.getLocale()));
String xml = this.xmlHandlingService.toXml(this.exportXmlEntity(data.getId(), false)); String xml = this.xmlHandlingService.toXml(this.exportXmlEntity(data.getId(), false));
this.accountingService.increase(UsageLimitTargetMetric.EXPORT_DESCRIPTION_XML_EXECUTION_COUNT.getValue());
return this.responseUtilsService.buildResponseFileFromText(xml, data.getLabel() + ".xml"); return this.responseUtilsService.buildResponseFileFromText(xml, data.getLabel() + ".xml");
} }

View File

@ -729,6 +729,7 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic
persist.setHash(this.conventionService.hashValue(latestVersionDescriptionTemplate.getUpdatedAt())); persist.setHash(this.conventionService.hashValue(latestVersionDescriptionTemplate.getUpdatedAt()));
this.validatorFactory.validator(NewVersionDescriptionTemplatePersist.NewVersionDescriptionTemplatePersistValidator.class).validateForce(persist); this.validatorFactory.validator(NewVersionDescriptionTemplatePersist.NewVersionDescriptionTemplatePersistValidator.class).validateForce(persist);
this.accountingService.increase(UsageLimitTargetMetric.IMPORT_DESCRIPTION_TEMPLATE_XML_EXECUTION_COUNT.getValue());
return this.createNewVersion(persist, fields); return this.createNewVersion(persist, fields);
} }
} }
@ -902,6 +903,7 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic
if (data == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{id, DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale())); if (data == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{id, DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
String xml = this.xmlHandlingService.toXml(this.exportXmlEntity(id, false)); String xml = this.xmlHandlingService.toXml(this.exportXmlEntity(id, false));
this.accountingService.increase(UsageLimitTargetMetric.EXPORT_DESCRIPTION_TEMPLATE_XML_EXECUTION_COUNT.getValue());
return this.responseUtilsService.buildResponseFileFromText(xml, data.getLabel() + ".xml"); return this.responseUtilsService.buildResponseFileFromText(xml, data.getLabel() + ".xml");
} }

View File

@ -1635,6 +1635,7 @@ public class PlanServiceImpl implements PlanService {
if (data == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{id, Plan.class.getSimpleName()}, LocaleContextHolder.getLocale())); if (data == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{id, Plan.class.getSimpleName()}, LocaleContextHolder.getLocale()));
String xml = this.xmlHandlingService.toXml(this.exportXmlEntity(data.getId(), false)); String xml = this.xmlHandlingService.toXml(this.exportXmlEntity(data.getId(), false));
this.accountingService.increase(UsageLimitTargetMetric.EXPORT_PLAN_XML_EXECUTION_COUNT.getValue());
return this.responseUtilsService.buildResponseFileFromText(xml, data.getLabel() + ".xml"); return this.responseUtilsService.buildResponseFileFromText(xml, data.getLabel() + ".xml");
} }
@ -1863,6 +1864,8 @@ public class PlanServiceImpl implements PlanService {
} }
} }
this.accountingService.increase(UsageLimitTargetMetric.IMPORT_PLAN_XML_EXECUTION_COUNT.getValue());
return plan; return plan;
} }

View File

@ -490,6 +490,7 @@ public class PlanBlueprintServiceImpl implements PlanBlueprintService {
if (data == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{id, PlanBlueprint.class.getSimpleName()}, LocaleContextHolder.getLocale())); if (data == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{id, PlanBlueprint.class.getSimpleName()}, LocaleContextHolder.getLocale()));
String xml = this.xmlHandlingService.toXml(this.getExportXmlEntity(id, false)); String xml = this.xmlHandlingService.toXml(this.getExportXmlEntity(id, false));
this.accountingService.increase(UsageLimitTargetMetric.EXPORT_BLUEPRINT_XML_EXECUTION_COUNT.getValue());
return this.responseUtilsService.buildResponseFileFromText(xml, data.getLabel() + ".xml"); return this.responseUtilsService.buildResponseFileFromText(xml, data.getLabel() + ".xml");
} }
@ -674,6 +675,7 @@ public class PlanBlueprintServiceImpl implements PlanBlueprintService {
persist.setHash(this.conventionService.hashValue(latestVersionPlanBlueprint.getUpdatedAt())); persist.setHash(this.conventionService.hashValue(latestVersionPlanBlueprint.getUpdatedAt()));
this.validatorFactory.validator(NewVersionPlanBlueprintPersist.NewVersionPlanBlueprintPersistValidator.class).validateForce(persist); this.validatorFactory.validator(NewVersionPlanBlueprintPersist.NewVersionPlanBlueprintPersistValidator.class).validateForce(persist);
this.accountingService.increase(UsageLimitTargetMetric.IMPORT_BLUEPRINT_XML_EXECUTION_COUNT.getValue());
return this.createNewVersion(persist, fields); return this.createNewVersion(persist, fields);
} }
} }

View File

@ -21,6 +21,7 @@ import org.opencdmp.authorization.AuthorizationFlags;
import org.opencdmp.authorization.ClaimNames; import org.opencdmp.authorization.ClaimNames;
import org.opencdmp.authorization.Permission; import org.opencdmp.authorization.Permission;
import org.opencdmp.commons.enums.IsActive; import org.opencdmp.commons.enums.IsActive;
import org.opencdmp.commons.enums.UsageLimitTargetMetric;
import org.opencdmp.commons.scope.tenant.TenantScope; import org.opencdmp.commons.scope.tenant.TenantScope;
import org.opencdmp.convention.ConventionService; import org.opencdmp.convention.ConventionService;
import org.opencdmp.data.*; import org.opencdmp.data.*;
@ -38,7 +39,9 @@ import org.opencdmp.model.persist.TenantPersist;
import org.opencdmp.query.TenantQuery; import org.opencdmp.query.TenantQuery;
import org.opencdmp.query.UserCredentialQuery; import org.opencdmp.query.UserCredentialQuery;
import org.opencdmp.query.UserRoleQuery; import org.opencdmp.query.UserRoleQuery;
import org.opencdmp.service.accounting.AccountingService;
import org.opencdmp.service.keycloak.KeycloakService; import org.opencdmp.service.keycloak.KeycloakService;
import org.opencdmp.service.usagelimit.UsageLimitService;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
@ -83,17 +86,19 @@ public class TenantServiceImpl implements TenantService {
private final CurrentPrincipalResolver currentPrincipalResolver; private final CurrentPrincipalResolver currentPrincipalResolver;
private final ClaimExtractor claimExtractor; private final ClaimExtractor claimExtractor;
private final EventBroker eventBroker; private final EventBroker eventBroker;
private final UsageLimitService usageLimitService;
private final AccountingService accountingService;
@Autowired @Autowired
public TenantServiceImpl( public TenantServiceImpl(
TenantEntityManager entityManager, TenantEntityManager entityManager,
AuthorizationService authorizationService, AuthorizationService authorizationService,
DeleterFactory deleterFactory, DeleterFactory deleterFactory,
BuilderFactory builderFactory, BuilderFactory builderFactory,
ConventionService conventionService, ConventionService conventionService,
MessageSource messageSource, MessageSource messageSource,
ErrorThesaurusProperties errors, TenantTouchedIntegrationEventHandler tenantTouchedIntegrationEventHandler, TenantRemovalIntegrationEventHandler tenantRemovalIntegrationEventHandler, UserTouchedIntegrationEventHandler userTouchedIntegrationEventHandler, KeycloakService keycloakService, AuthorizationConfiguration authorizationConfiguration, TenantScope tenantScope, QueryFactory queryFactory, CurrentPrincipalResolver currentPrincipalResolver, ClaimExtractor claimExtractor, EventBroker eventBroker) { ErrorThesaurusProperties errors, TenantTouchedIntegrationEventHandler tenantTouchedIntegrationEventHandler, TenantRemovalIntegrationEventHandler tenantRemovalIntegrationEventHandler, UserTouchedIntegrationEventHandler userTouchedIntegrationEventHandler, KeycloakService keycloakService, AuthorizationConfiguration authorizationConfiguration, TenantScope tenantScope, QueryFactory queryFactory, CurrentPrincipalResolver currentPrincipalResolver, ClaimExtractor claimExtractor, EventBroker eventBroker, UsageLimitService usageLimitService, AccountingService accountingService) {
this.entityManager = entityManager; this.entityManager = entityManager;
this.authorizationService = authorizationService; this.authorizationService = authorizationService;
this.deleterFactory = deleterFactory; this.deleterFactory = deleterFactory;
@ -111,6 +116,8 @@ public class TenantServiceImpl implements TenantService {
this.currentPrincipalResolver = currentPrincipalResolver; this.currentPrincipalResolver = currentPrincipalResolver;
this.claimExtractor = claimExtractor; this.claimExtractor = claimExtractor;
this.eventBroker = eventBroker; this.eventBroker = eventBroker;
this.usageLimitService = usageLimitService;
this.accountingService = accountingService;
} }
@Override @Override
@ -177,6 +184,7 @@ public class TenantServiceImpl implements TenantService {
List<String> keycloakIdsToAddToTenantGroup = new ArrayList<>(); List<String> keycloakIdsToAddToTenantGroup = new ArrayList<>();
for (UUID userId : existingItems.stream().map(UserRoleEntity::getUserId).distinct().toList()) { for (UUID userId : existingItems.stream().map(UserRoleEntity::getUserId).distinct().toList()) {
this.usageLimitService.checkIncrease(UsageLimitTargetMetric.USER_COUNT);
TenantUserEntity tenantUserEntity = new TenantUserEntity(); TenantUserEntity tenantUserEntity = new TenantUserEntity();
tenantUserEntity.setId(UUID.randomUUID()); tenantUserEntity.setId(UUID.randomUUID());
tenantUserEntity.setUserId(userId); tenantUserEntity.setUserId(userId);
@ -186,7 +194,8 @@ public class TenantServiceImpl implements TenantService {
tenantUserEntity.setUpdatedAt(Instant.now()); tenantUserEntity.setUpdatedAt(Instant.now());
this.entityManager.persist(tenantUserEntity); this.entityManager.persist(tenantUserEntity);
this.eventBroker.emit(new UserAddedToTenantEvent(tenantUserEntity.getUserId(), tenantUserEntity.getTenantId())); this.eventBroker.emit(new UserAddedToTenantEvent(tenantUserEntity.getUserId(), tenantUserEntity.getTenantId()));
this.accountingService.increase(UsageLimitTargetMetric.USER_COUNT.getValue());
UserCredentialEntity userCredential = userCredentialEntities.stream().filter(x-> !this.conventionService.isNullOrEmpty(x.getExternalId()) && x.getUserId().equals(userId)).findFirst().orElse(null); UserCredentialEntity userCredential = userCredentialEntities.stream().filter(x-> !this.conventionService.isNullOrEmpty(x.getExternalId()) && x.getUserId().equals(userId)).findFirst().orElse(null);
if (userCredential == null) continue; if (userCredential == null) continue;
UserRoleEntity item = new UserRoleEntity(); UserRoleEntity item = new UserRoleEntity();

View File

@ -60,9 +60,11 @@ import org.opencdmp.model.referencetype.ReferenceType;
import org.opencdmp.model.user.User; import org.opencdmp.model.user.User;
import org.opencdmp.model.usercredential.UserCredential; import org.opencdmp.model.usercredential.UserCredential;
import org.opencdmp.query.*; import org.opencdmp.query.*;
import org.opencdmp.service.accounting.AccountingService;
import org.opencdmp.service.actionconfirmation.ActionConfirmationService; import org.opencdmp.service.actionconfirmation.ActionConfirmationService;
import org.opencdmp.service.elastic.ElasticService; import org.opencdmp.service.elastic.ElasticService;
import org.opencdmp.service.keycloak.KeycloakService; import org.opencdmp.service.keycloak.KeycloakService;
import org.opencdmp.service.usagelimit.UsageLimitService;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
@ -114,6 +116,8 @@ public class UserServiceImpl implements UserService {
private final AuthorizationConfiguration authorizationConfiguration; private final AuthorizationConfiguration authorizationConfiguration;
private final TenantScope tenantScope; private final TenantScope tenantScope;
private final AnnotationEntityTouchedIntegrationEventHandler annotationEntityTouchedIntegrationEventHandler; private final AnnotationEntityTouchedIntegrationEventHandler annotationEntityTouchedIntegrationEventHandler;
private final UsageLimitService usageLimitService;
private final AccountingService accountingService;
@Autowired @Autowired
public UserServiceImpl( public UserServiceImpl(
TenantEntityManager entityManager, TenantEntityManager entityManager,
@ -126,7 +130,7 @@ public class UserServiceImpl implements UserService {
EventBroker eventBroker, EventBroker eventBroker,
JsonHandlingService jsonHandlingService, JsonHandlingService jsonHandlingService,
XmlHandlingService xmlHandlingService, QueryFactory queryFactory, XmlHandlingService xmlHandlingService, QueryFactory queryFactory,
UserScope userScope, KeycloakService keycloakService, ActionConfirmationService actionConfirmationService, NotificationProperties notificationProperties, NotifyIntegrationEventHandler eventHandler, ValidatorFactory validatorFactory, ElasticService elasticService, UserTouchedIntegrationEventHandler userTouchedIntegrationEventHandler, UserRemovalIntegrationEventHandler userRemovalIntegrationEventHandler, AuthorizationConfiguration authorizationConfiguration, TenantScope tenantScope, AnnotationEntityTouchedIntegrationEventHandler annotationEntityTouchedIntegrationEventHandler) { UserScope userScope, KeycloakService keycloakService, ActionConfirmationService actionConfirmationService, NotificationProperties notificationProperties, NotifyIntegrationEventHandler eventHandler, ValidatorFactory validatorFactory, ElasticService elasticService, UserTouchedIntegrationEventHandler userTouchedIntegrationEventHandler, UserRemovalIntegrationEventHandler userRemovalIntegrationEventHandler, AuthorizationConfiguration authorizationConfiguration, TenantScope tenantScope, AnnotationEntityTouchedIntegrationEventHandler annotationEntityTouchedIntegrationEventHandler, UsageLimitService usageLimitService, AccountingService accountingService) {
this.entityManager = entityManager; this.entityManager = entityManager;
this.authorizationService = authorizationService; this.authorizationService = authorizationService;
this.deleterFactory = deleterFactory; this.deleterFactory = deleterFactory;
@ -150,6 +154,8 @@ public class UserServiceImpl implements UserService {
this.authorizationConfiguration = authorizationConfiguration; this.authorizationConfiguration = authorizationConfiguration;
this.tenantScope = tenantScope; this.tenantScope = tenantScope;
this.annotationEntityTouchedIntegrationEventHandler = annotationEntityTouchedIntegrationEventHandler; this.annotationEntityTouchedIntegrationEventHandler = annotationEntityTouchedIntegrationEventHandler;
this.usageLimitService = usageLimitService;
this.accountingService = accountingService;
} }
//region persist //region persist
@ -410,6 +416,7 @@ public class UserServiceImpl implements UserService {
} }
if (!hasTenantUser && !model.getRoles().isEmpty() && !this.tenantScope.isDefaultTenant()){ if (!hasTenantUser && !model.getRoles().isEmpty() && !this.tenantScope.isDefaultTenant()){
this.usageLimitService.checkIncrease(UsageLimitTargetMetric.USER_COUNT);
TenantUserEntity tenantUserEntity = new TenantUserEntity(); TenantUserEntity tenantUserEntity = new TenantUserEntity();
tenantUserEntity.setId(UUID.randomUUID()); tenantUserEntity.setId(UUID.randomUUID());
tenantUserEntity.setUserId(userId); tenantUserEntity.setUserId(userId);
@ -420,6 +427,7 @@ public class UserServiceImpl implements UserService {
this.entityManager.persist(tenantUserEntity); this.entityManager.persist(tenantUserEntity);
this.eventBroker.emit(new UserAddedToTenantEvent(tenantUserEntity.getUserId(), tenantUserEntity.getTenantId())); this.eventBroker.emit(new UserAddedToTenantEvent(tenantUserEntity.getUserId(), tenantUserEntity.getTenantId()));
this.accountingService.increase(UsageLimitTargetMetric.USER_COUNT.getValue());
} }
this.entityManager.flush(); this.entityManager.flush();
@ -768,9 +776,11 @@ public class UserServiceImpl implements UserService {
tenantUsersToDelete.add(userTenantUser); tenantUsersToDelete.add(userTenantUser);
} else { } else {
this.eventBroker.emit(new UserRemovedFromTenantEvent(userTenantUser.getUserId(), userTenantUser.getTenantId())); this.eventBroker.emit(new UserRemovedFromTenantEvent(userTenantUser.getUserId(), userTenantUser.getTenantId()));
this.accountingService.decrease(UsageLimitTargetMetric.USER_COUNT.getValue());
userTenantUser.setUserId(newUser.getId()); userTenantUser.setUserId(newUser.getId());
this.entityManager.merge(userTenantUser); this.entityManager.merge(userTenantUser);
this.eventBroker.emit(new UserAddedToTenantEvent(userTenantUser.getUserId(), userTenantUser.getTenantId())); this.eventBroker.emit(new UserAddedToTenantEvent(userTenantUser.getUserId(), userTenantUser.getTenantId()));
this.accountingService.increase(UsageLimitTargetMetric.USER_COUNT.getValue());
} }
} }
this.deleterFactory.deleter(TenantUserDeleter.class).delete(tenantUsersToDelete); this.deleterFactory.deleter(TenantUserDeleter.class).delete(tenantUsersToDelete);
@ -1053,15 +1063,19 @@ public class UserServiceImpl implements UserService {
if (userCredential == null) throw new MyApplicationException(); if (userCredential == null) throw new MyApplicationException();
if (tenant != null){ if (tenant != null){
TenantUserEntity tenantUserEntity = new TenantUserEntity(); boolean hasTenantUser = this.queryFactory.query(TenantUserQuery.class).tenantIds(tenant.getId()).isActive(IsActive.Active).userIds(userId).count() > 0;
tenantUserEntity.setId(UUID.randomUUID()); if (!hasTenantUser) {
tenantUserEntity.setUserId(userId); TenantUserEntity tenantUserEntity = new TenantUserEntity();
tenantUserEntity.setIsActive(IsActive.Active); tenantUserEntity.setId(UUID.randomUUID());
tenantUserEntity.setTenantId(tenant.getId()); tenantUserEntity.setUserId(userId);
tenantUserEntity.setCreatedAt(Instant.now()); tenantUserEntity.setIsActive(IsActive.Active);
tenantUserEntity.setUpdatedAt(Instant.now()); tenantUserEntity.setTenantId(tenant.getId());
this.entityManager.persist(tenantUserEntity); tenantUserEntity.setCreatedAt(Instant.now());
this.eventBroker.emit(new UserAddedToTenantEvent(tenantUserEntity.getUserId(), tenantUserEntity.getTenantId())); tenantUserEntity.setUpdatedAt(Instant.now());
this.entityManager.persist(tenantUserEntity);
this.eventBroker.emit(new UserAddedToTenantEvent(tenantUserEntity.getUserId(), tenantUserEntity.getTenantId()));
this.accountingService.increase(UsageLimitTargetMetric.USER_COUNT.getValue());
}
} }