diff --git a/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingService.java b/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingService.java index 0c1a5659d..927467a50 100644 --- a/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingService.java +++ b/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingService.java @@ -8,7 +8,7 @@ import java.util.UUID; public interface AccountingService { - Integer getCurrentMetricValue(UsageLimitTargetMetric metric, DefinitionEntity definition) throws InvalidApplicationException; + Integer getCurrentMetricValue(UsageLimitTargetMetric metric, DefinitionEntity definition, boolean userIdEnabled) throws InvalidApplicationException; void set(String metric, UUID tenantId, String tenantCode, Integer value) throws InvalidApplicationException; diff --git a/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingServiceImpl.java index 0d7c1f423..7d2a73672 100644 --- a/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingServiceImpl.java @@ -129,7 +129,7 @@ public class AccountingServiceImpl implements AccountingService { }); } - public Integer getCurrentMetricValue(UsageLimitTargetMetric metric, DefinitionEntity definition) throws InvalidApplicationException { + public Integer getCurrentMetricValue(UsageLimitTargetMetric metric, DefinitionEntity definition, boolean userIdEnabled) throws InvalidApplicationException { if (this.isEnabled) { AccountingClient accountingClient = null; try { @@ -173,12 +173,14 @@ public class AccountingServiceImpl implements AccountingService { throw new RuntimeException(e); } - lookup.setGroupingFields(new FieldSet(List.of( - AccountingEntryCreatedIntegrationEvent._serviceId, - AccountingEntryCreatedIntegrationEvent._action, - AccountingEntryCreatedIntegrationEvent._resource, - AccountingEntryCreatedIntegrationEvent._userId - ))); + List fields = new ArrayList<>(); + fields.add(AccountingEntryCreatedIntegrationEvent._serviceId); + fields.add(AccountingEntryCreatedIntegrationEvent._action); + fields.add(AccountingEntryCreatedIntegrationEvent._resource); + + if (userIdEnabled) fields.add(AccountingEntryCreatedIntegrationEvent._userId); + + lookup.setGroupingFields(new FieldSet(fields)); lookup.setProject(new FieldSet(this.accountingProperties.getProjectFields())); diff --git a/backend/core/src/main/java/org/opencdmp/service/maintenance/MaintenanceServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/maintenance/MaintenanceServiceImpl.java index 0626fa10a..d82d24bb0 100644 --- a/backend/core/src/main/java/org/opencdmp/service/maintenance/MaintenanceServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/maintenance/MaintenanceServiceImpl.java @@ -381,11 +381,15 @@ public class MaintenanceServiceImpl implements MaintenanceService { private void calculateReset(UsageLimitTargetMetric usageLimitTargetMetric, long itemsCount, UUID tenantId ,String tenantCode) throws InvalidApplicationException { try { this.tenantScope.setTempTenant(this.entityManager, tenantId, tenantCode); - Integer currentValue = this.accountingService.getCurrentMetricValue(usageLimitTargetMetric, null); - if (currentValue > itemsCount) { - this.accountingService.set(usageLimitTargetMetric.getValue(), tenantId, tenantCode, currentValue - (int) itemsCount); - } - if (currentValue < itemsCount) { + Integer currentValue = this.accountingService.getCurrentMetricValue(usageLimitTargetMetric, null, false); + if (currentValue == 0) this.accountingService.set(usageLimitTargetMetric.getValue(), tenantId, tenantCode, (int) itemsCount); + else if (currentValue < itemsCount) { + if (currentValue < 0) { + this.accountingService.set(usageLimitTargetMetric.getValue(), tenantId, tenantCode, Math.abs(currentValue - (int) itemsCount)); + } else { + this.accountingService.set(usageLimitTargetMetric.getValue(), tenantId, tenantCode, currentValue - (int) itemsCount); + } + } else if (currentValue > itemsCount) { this.accountingService.set(usageLimitTargetMetric.getValue(), tenantId, tenantCode, (int) itemsCount - currentValue); } } finally { diff --git a/backend/core/src/main/java/org/opencdmp/service/usagelimit/UsageLimitServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/usagelimit/UsageLimitServiceImpl.java index 69700d6f5..234b317dc 100644 --- a/backend/core/src/main/java/org/opencdmp/service/usagelimit/UsageLimitServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/usagelimit/UsageLimitServiceImpl.java @@ -184,7 +184,7 @@ public class UsageLimitServiceImpl implements UsageLimitService { DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(DefinitionEntity.class, usageLimitEntity.getDefinition()); if (definition == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{usageLimitEntity.getId(), DefinitionEntity.class.getSimpleName()}, LocaleContextHolder.getLocale())); - Integer currentValue = this.accountingService.getCurrentMetricValue(metric, definition); + Integer currentValue = this.accountingService.getCurrentMetricValue(metric, definition, true); if (currentValue >= usageLimitEntity.getValue()) throw new MyValidationException(this.errors.getUsageLimitException().getCode(), usageLimitEntity.getLabel()); } } catch (InvalidApplicationException e) {