I’m new to Domain-Driven Design and I’m struggling with a problem in my sales order system. Here’s the issue:
Our system lets users input orders with multiple lines. Each line can have an amount in our base currency or a foreign one. If it’s foreign, we need to convert it using monthly exchange rates.
I know this conversion is a business rule and should be in the domain layer. But I also want the form to auto-calculate the base currency amount when users enter a foreign amount. This seems like it needs frontend logic, which goes against the ‘keep business rules out of the frontend’ principle.
How can I make this work? Is there a way to keep the form interactive without putting business logic in the frontend? Or am I thinking about this all wrong?
Here’s a basic example of what I’m trying to do:
function calculateBaseAmount(foreignAmount, exchangeRate) {
// This feels like business logic in the frontend
return foreignAmount * exchangeRate;
}
// Using it in a React component
const OrderLineForm = () => {
const [foreignAmount, setForeignAmount] = useState(0);
const [baseAmount, setBaseAmount] = useState(0);
const exchangeRate = 1.2; // Simplified, would actually come from backend
const handleForeignAmountChange = (e) => {
const newForeignAmount = parseFloat(e.target.value);
setForeignAmount(newForeignAmount);
setBaseAmount(calculateBaseAmount(newForeignAmount, exchangeRate));
};
// Rest of the component...
}
Any advice would be really helpful!