FINERACT-2247: New command processing - Currency Management (org.apache.fineract.organisation.monetary)

This commit is contained in:
viktorpavlenko
2025-06-28 10:38:32 +02:00
committed by Aleksandar Vidakovic
parent 0629bb2d48
commit 4c3da5b06a
42 changed files with 404 additions and 538 deletions
@@ -0,0 +1,80 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.organisation.monetary.api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import java.util.UUID;
import java.util.function.Supplier;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.command.core.CommandPipeline;
import org.apache.fineract.infrastructure.core.service.DateUtils;
import org.apache.fineract.organisation.monetary.command.CurrencyUpdateCommand;
import org.apache.fineract.organisation.monetary.data.CurrencyConfigurationData;
import org.apache.fineract.organisation.monetary.data.CurrencyUpdateRequest;
import org.apache.fineract.organisation.monetary.data.CurrencyUpdateResponse;
import org.apache.fineract.organisation.monetary.service.OrganisationCurrencyReadPlatformService;
import org.springframework.stereotype.Component;
@Path("/v1/currencies")
@Component
@Tag(name = "Currency", description = "Application related configuration around viewing/updating the currencies permitted for use within the MFI.")
@RequiredArgsConstructor
public class CurrenciesApiResource {
private final OrganisationCurrencyReadPlatformService readPlatformService;
private final CommandPipeline commandPipeline;
@GET
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Retrieve Currency Configuration", description = """
Returns the list of currencies permitted for use AND the list of currencies not selected (but available for selection).
Example Requests:
currencies
currencies?fields=selectedCurrencyOptions
""")
public CurrencyConfigurationData retrieveCurrencies() {
return readPlatformService.retrieveCurrencyConfiguration();
}
@PUT
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Update Currency Configuration", description = "Updates the list of currencies permitted for use.")
public CurrencyUpdateResponse updateCurrencies(CurrencyUpdateRequest request) {
final var command = new CurrencyUpdateCommand();
command.setId(UUID.randomUUID());
command.setCreatedAt(DateUtils.getAuditOffsetDateTime());
command.setPayload(request);
final Supplier<CurrencyUpdateResponse> response = commandPipeline.send(command);
return response.get();
}
}
@@ -16,35 +16,13 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.organisation.monetary.data;
package org.apache.fineract.organisation.monetary.command;
import java.math.BigDecimal;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.fineract.command.core.Command;
import org.apache.fineract.organisation.monetary.data.CurrencyUpdateRequest;
/**
* Immutable data object representing currency.
*/
public class MoneyData {
private final String code;
private final BigDecimal amount;
private final int decimalPlaces;
public MoneyData(final String code, final BigDecimal amount, final int decimalPlaces) {
this.code = code;
this.amount = amount;
this.decimalPlaces = decimalPlaces;
}
public String getCode() {
return this.code;
}
public BigDecimal getAmount() {
return this.amount;
}
public int getDecimalPlaces() {
return this.decimalPlaces;
}
}
@Data
@EqualsAndHashCode(callSuper = true)
public class CurrencyUpdateCommand extends Command<CurrencyUpdateRequest> {}
@@ -16,14 +16,25 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.organisation.monetary.data.request;
package org.apache.fineract.organisation.monetary.data;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
public record CurrencyRequest(List<String> currencies) implements Serializable {
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CurrencyConfigurationData implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private List<CurrencyData> selectedCurrencyOptions;
private List<CurrencyData> currencyOptions;
}
@@ -18,26 +18,29 @@
*/
package org.apache.fineract.organisation.monetary.data;
import java.io.Serial;
import java.io.Serializable;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.apache.fineract.infrastructure.core.config.MapstructMapperConfig;
import org.apache.fineract.organisation.monetary.domain.MonetaryCurrency;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Immutable data object representing currency.
*/
@Getter
@EqualsAndHashCode
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CurrencyData implements Serializable {
private final String code;
private final String name;
private final int decimalPlaces;
private final Integer inMultiplesOf;
private final String displaySymbol;
private final String nameCode;
private final String displayLabel;
@Serial
private static final long serialVersionUID = 1L;
private String code;
private String name;
private int decimalPlaces;
private Integer inMultiplesOf;
private String displaySymbol;
private String nameCode;
private String displayLabel;
public static CurrencyData blank() {
return new CurrencyData("", "", 0, 0, "", "");
@@ -90,11 +93,4 @@ public class CurrencyData implements Serializable {
return builder.toString();
}
@org.mapstruct.Mapper(config = MapstructMapperConfig.class)
public interface Mapper {
default CurrencyData map(MonetaryCurrency source) {
return source.toData();
}
}
}
@@ -18,25 +18,26 @@
*/
package org.apache.fineract.organisation.monetary.data;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.io.Serial;
import java.io.Serializable;
import java.util.Collection;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Immutable data object for application currency.
*/
@Getter
@RequiredArgsConstructor
public class ApplicationCurrencyConfigurationData implements Serializable {
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CurrencyUpdateRequest implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@SuppressWarnings("unused")
private final Collection<CurrencyData> selectedCurrencyOptions;
@SuppressWarnings("unused")
private final Collection<CurrencyData> currencyOptions;
@NotNull(message = "{org.apache.fineract.organisation.monetary.currencies.not-null}")
@NotEmpty(message = "{org.apache.fineract.organisation.monetary.currencies.not-empty}")
private List<String> currencies;
}
@@ -0,0 +1,61 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.organisation.monetary.data;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CurrencyUpdateResponse implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Schema(example = """
[
"KES",
"BND",
"LBP",
"GHC",
"USD",
"XOF",
"AED",
"AMD"
]
""")
private List<String> currencies;
@Deprecated(forRemoval = true)
@JsonProperty("changes")
public Map<String, Object> getChanges() {
// TODO: remove this one day... we should never use hashmaps in such trivial cases!!!
return Map.of("currencies", currencies);
}
}
@@ -51,7 +51,7 @@ public class ApplicationCurrencyRepositoryWrapper {
}
final ApplicationCurrency applicationCurrency = ApplicationCurrency.from(defaultApplicationCurrency,
currency.getDigitsAfterDecimal(), currency.getCurrencyInMultiplesOf());
currency.getDigitsAfterDecimal(), currency.getInMultiplesOf());
return applicationCurrency;
}
@@ -20,8 +20,10 @@ package org.apache.fineract.organisation.monetary.domain;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.Getter;
import org.apache.fineract.organisation.monetary.data.CurrencyData;
@Getter
@Embeddable
public class MonetaryCurrency {
@@ -74,17 +76,4 @@ public class MonetaryCurrency {
}
return currencyData;
}
public String getCode() {
return this.code;
}
public int getDigitsAfterDecimal() {
return this.digitsAfterDecimal;
}
public Integer getCurrencyInMultiplesOf() {
return this.inMultiplesOf;
}
}
@@ -0,0 +1,43 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.organisation.monetary.handler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.command.core.Command;
import org.apache.fineract.command.core.CommandHandler;
import org.apache.fineract.organisation.monetary.data.CurrencyUpdateRequest;
import org.apache.fineract.organisation.monetary.data.CurrencyUpdateResponse;
import org.apache.fineract.organisation.monetary.service.CurrencyWritePlatformService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Component
@RequiredArgsConstructor
public class CurrencyUpdateCommandHandler implements CommandHandler<CurrencyUpdateRequest, CurrencyUpdateResponse> {
private final CurrencyWritePlatformService writePlatformService;
@Transactional
@Override
public CurrencyUpdateResponse handle(final Command<CurrencyUpdateRequest> command) {
return writePlatformService.updateAllowedCurrencies(command.getPayload());
}
}
@@ -0,0 +1,37 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.organisation.monetary.mapper;
import org.apache.fineract.infrastructure.core.config.MapstructMapperConfig;
import org.apache.fineract.organisation.monetary.data.CurrencyData;
import org.apache.fineract.organisation.monetary.domain.MonetaryCurrency;
import org.mapstruct.Mapping;
@org.mapstruct.Mapper(config = MapstructMapperConfig.class)
public interface CurrencyMapper {
@Mapping(target = "nameCode", ignore = true)
@Mapping(target = "name", ignore = true)
@Mapping(target = "displaySymbol", ignore = true)
@Mapping(target = "displayLabel", ignore = true)
@Mapping(source = "code", target = "code")
@Mapping(source = "digitsAfterDecimal", target = "decimalPlaces")
@Mapping(source = "inMultiplesOf", target = "inMultiplesOf")
CurrencyData map(MonetaryCurrency source);
}
@@ -1,79 +0,0 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.organisation.monetary.serialization;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.fineract.infrastructure.core.data.ApiParameterError;
import org.apache.fineract.infrastructure.core.data.DataValidatorBuilder;
import org.apache.fineract.infrastructure.core.exception.InvalidJsonException;
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public final class CurrencyCommandFromApiJsonDeserializer {
public static final String CURRENCIES = "currencies";
/**
* The parameters supported for this command.
*/
private static final Set<String> SUPPORTED_PARAMETERS = new HashSet<>(List.of(CURRENCIES));
private final FromJsonHelper fromApiJsonHelper;
@Autowired
public CurrencyCommandFromApiJsonDeserializer(final FromJsonHelper fromApiJsonHelper) {
this.fromApiJsonHelper = fromApiJsonHelper;
}
public void validateForUpdate(final String json) {
if (StringUtils.isBlank(json)) {
throw new InvalidJsonException();
}
final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, SUPPORTED_PARAMETERS);
final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource(CURRENCIES);
final JsonElement element = this.fromApiJsonHelper.parse(json);
final String[] currencies = this.fromApiJsonHelper.extractArrayNamed(CURRENCIES, element);
baseDataValidator.reset().parameter(CURRENCIES).value(currencies).arrayNotEmpty();
throwExceptionIfValidationWarningsExist(dataValidationErrors);
}
private void throwExceptionIfValidationWarningsExist(final List<ApiParameterError> dataValidationErrors) {
if (!dataValidationErrors.isEmpty()) {
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.",
dataValidationErrors);
}
}
}
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.organisation.monetary.serialization.gson;
package org.apache.fineract.organisation.monetary.serialization;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
@@ -29,6 +29,7 @@ import org.apache.fineract.organisation.monetary.domain.MonetaryCurrency;
import org.apache.fineract.organisation.monetary.domain.Money;
@AllArgsConstructor
@Deprecated(forRemoval = true)
public class MoneyDeserializer implements JsonDeserializer<Money> {
private final MathContext mc;
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.organisation.monetary.serialization.gson;
package org.apache.fineract.organisation.monetary.serialization;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
@@ -25,6 +25,7 @@ import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
import org.apache.fineract.organisation.monetary.domain.Money;
@Deprecated(forRemoval = true)
public class MoneySerializer implements JsonSerializer<Money> {
@Override
@@ -23,7 +23,6 @@ import java.sql.SQLException;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.infrastructure.core.domain.JdbcSupport;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
import org.apache.fineract.organisation.monetary.data.CurrencyData;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
@@ -31,15 +30,11 @@ import org.springframework.jdbc.core.RowMapper;
@RequiredArgsConstructor
public class CurrencyReadPlatformServiceImpl implements CurrencyReadPlatformService {
private final PlatformSecurityContext context;
private final JdbcTemplate jdbcTemplate;
private final CurrencyMapper currencyRowMapper = new CurrencyMapper();
private final CurrencyRowMapper currencyRowMapper = new CurrencyRowMapper();
@Override
public List<CurrencyData> retrieveAllowedCurrencies() {
this.context.authenticatedUser();
final String sql = "select " + this.currencyRowMapper.schema() + " from m_organisation_currency c order by c.name";
return this.jdbcTemplate.query(sql, this.currencyRowMapper); // NOSONAR
@@ -47,7 +42,6 @@ public class CurrencyReadPlatformServiceImpl implements CurrencyReadPlatformServ
@Override
public List<CurrencyData> retrieveAllPlatformCurrencies() {
final String sql = "select " + this.currencyRowMapper.schema() + " from m_currency c order by c.name";
return this.jdbcTemplate.query(sql, this.currencyRowMapper); // NOSONAR
@@ -55,13 +49,12 @@ public class CurrencyReadPlatformServiceImpl implements CurrencyReadPlatformServ
@Override
public CurrencyData retrieveCurrency(final String code) {
final String sql = "select " + this.currencyRowMapper.schema() + " from m_currency c where c.code = ? order by c.name";
return this.jdbcTemplate.queryForObject(sql, this.currencyRowMapper, new Object[] { code }); // NOSONAR
}
private static final class CurrencyMapper implements RowMapper<CurrencyData> {
private static final class CurrencyRowMapper implements RowMapper<CurrencyData> {
@Override
public CurrencyData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException {
@@ -18,11 +18,10 @@
*/
package org.apache.fineract.organisation.monetary.service;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.organisation.monetary.data.CurrencyUpdateRequest;
import org.apache.fineract.organisation.monetary.data.CurrencyUpdateResponse;
public interface CurrencyWritePlatformService {
CommandProcessingResult updateAllowedCurrencies(JsonCommand command);
CurrencyUpdateResponse updateAllowedCurrencies(CurrencyUpdateRequest request);
}
@@ -18,10 +18,10 @@
*/
package org.apache.fineract.organisation.monetary.service;
import org.apache.fineract.organisation.monetary.data.ApplicationCurrencyConfigurationData;
import org.apache.fineract.organisation.monetary.data.CurrencyConfigurationData;
public interface OrganisationCurrencyReadPlatformService {
ApplicationCurrencyConfigurationData retrieveCurrencyConfiguration();
CurrencyConfigurationData retrieveCurrencyConfiguration();
}
@@ -18,10 +18,8 @@
*/
package org.apache.fineract.organisation.monetary.service;
import java.util.Collection;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.organisation.monetary.data.ApplicationCurrencyConfigurationData;
import org.apache.fineract.organisation.monetary.data.CurrencyData;
import org.apache.fineract.organisation.monetary.data.CurrencyConfigurationData;
@RequiredArgsConstructor
public class OrganisationCurrencyReadPlatformServiceImpl implements OrganisationCurrencyReadPlatformService {
@@ -29,14 +27,15 @@ public class OrganisationCurrencyReadPlatformServiceImpl implements Organisation
private final CurrencyReadPlatformService currencyReadPlatformService;
@Override
public ApplicationCurrencyConfigurationData retrieveCurrencyConfiguration() {
public CurrencyConfigurationData retrieveCurrencyConfiguration() {
final Collection<CurrencyData> selectedCurrencyOptions = this.currencyReadPlatformService.retrieveAllowedCurrencies();
final Collection<CurrencyData> currencyOptions = this.currencyReadPlatformService.retrieveAllPlatformCurrencies();
final var selectedCurrencyOptions = currencyReadPlatformService.retrieveAllowedCurrencies();
final var currencyOptions = currencyReadPlatformService.retrieveAllPlatformCurrencies();
// remove selected currency options
currencyOptions.removeAll(selectedCurrencyOptions);
return new ApplicationCurrencyConfigurationData(selectedCurrencyOptions, currencyOptions);
return CurrencyConfigurationData.builder().selectedCurrencyOptions(selectedCurrencyOptions).currencyOptions(currencyOptions)
.build();
}
}