|
| 1 | +# Countries & Currencies Module |
| 2 | + |
| 3 | +This module provides comprehensive management of countries, currencies, and exchange rates for global operations in ManageHub. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Countries Management**: Full CRUD operations for countries with ISO codes |
| 8 | +- **Currencies Management**: Currency management with exchange rates to base currency |
| 9 | +- **Exchange Rates**: Historical and current exchange rate tracking between currencies |
| 10 | +- **Currency Conversion**: Real-time currency conversion functionality |
| 11 | +- **Base Currency**: Support for a designated base currency (USD by default) |
| 12 | + |
| 13 | +## Database Schema |
| 14 | + |
| 15 | +### Countries Table |
| 16 | +- `id`: UUID primary key |
| 17 | +- `iso2Code`: ISO 3166-1 alpha-2 country code (e.g., 'US', 'CA') |
| 18 | +- `iso3Code`: ISO 3166-1 alpha-3 country code (e.g., 'USA', 'CAN') |
| 19 | +- `name`: Full country name |
| 20 | +- `commonName`: Common name variant |
| 21 | +- `numericCode`: ISO 3166-1 numeric code |
| 22 | +- `callingCode`: Country calling code |
| 23 | +- `capital`: Capital city |
| 24 | +- `region`: Geographic region |
| 25 | +- `subregion`: Geographic subregion |
| 26 | +- `area`: Area in square kilometers |
| 27 | +- `population`: Population count |
| 28 | +- `isActive`: Active status |
| 29 | +- `isDeleted`: Soft delete flag |
| 30 | + |
| 31 | +### Currencies Table |
| 32 | +- `id`: UUID primary key |
| 33 | +- `code`: ISO 4217 currency code (e.g., 'USD', 'EUR') |
| 34 | +- `name`: Full currency name |
| 35 | +- `symbol`: Currency symbol |
| 36 | +- `exchangeRate`: Exchange rate to base currency |
| 37 | +- `isBaseCurrency`: Whether this is the base currency |
| 38 | +- `isActive`: Active status |
| 39 | +- `isDeleted`: Soft delete flag |
| 40 | +- `decimalPlaces`: Number of decimal places for this currency |
| 41 | +- `countryId`: Optional link to country |
| 42 | + |
| 43 | +### Exchange Rates Table |
| 44 | +- `id`: UUID primary key |
| 45 | +- `fromCurrencyId`: Source currency ID |
| 46 | +- `toCurrencyId`: Target currency ID |
| 47 | +- `rate`: Exchange rate value |
| 48 | +- `effectiveDate`: When this rate becomes effective |
| 49 | +- `expiryDate`: Optional expiry date |
| 50 | +- `source`: Source of the exchange rate (e.g., 'ECB', 'Federal Reserve') |
| 51 | +- `isActive`: Active status |
| 52 | +- `isDeleted`: Soft delete flag |
| 53 | + |
| 54 | +## API Endpoints |
| 55 | + |
| 56 | +### Countries |
| 57 | +- `POST /countries-currencies/countries` - Create a new country |
| 58 | +- `GET /countries-currencies/countries` - Get all countries |
| 59 | +- `GET /countries-currencies/countries/:id` - Get country by ID |
| 60 | +- `GET /countries-currencies/countries/code/:code` - Get country by ISO code |
| 61 | +- `PATCH /countries-currencies/countries/:id` - Update country |
| 62 | +- `DELETE /countries-currencies/countries/:id` - Delete country (soft delete) |
| 63 | + |
| 64 | +### Currencies |
| 65 | +- `POST /countries-currencies/currencies` - Create a new currency |
| 66 | +- `GET /countries-currencies/currencies` - Get all currencies |
| 67 | +- `GET /countries-currencies/currencies/:id` - Get currency by ID |
| 68 | +- `GET /countries-currencies/currencies/code/:code` - Get currency by code |
| 69 | +- `PATCH /countries-currencies/currencies/:id` - Update currency |
| 70 | +- `DELETE /countries-currencies/currencies/:id` - Delete currency (soft delete) |
| 71 | + |
| 72 | +### Exchange Rates |
| 73 | +- `POST /countries-currencies/exchange-rates` - Create a new exchange rate |
| 74 | +- `GET /countries-currencies/exchange-rates` - Get all exchange rates |
| 75 | +- `GET /countries-currencies/exchange-rates/:id` - Get exchange rate by ID |
| 76 | +- `GET /countries-currencies/exchange-rates/current?from=:fromId&to=:toId` - Get current exchange rate |
| 77 | +- `PATCH /countries-currencies/exchange-rates/:id` - Update exchange rate |
| 78 | +- `DELETE /countries-currencies/exchange-rates/:id` - Delete exchange rate (soft delete) |
| 79 | + |
| 80 | +### Utility Endpoints |
| 81 | +- `GET /countries-currencies/base-currency` - Get the base currency |
| 82 | +- `GET /countries-currencies/convert?from=:fromId&to=:toId&amount=:amount` - Convert amount between currencies |
| 83 | + |
| 84 | +## Usage Examples |
| 85 | + |
| 86 | +### Creating a Country |
| 87 | +```json |
| 88 | +POST /countries-currencies/countries |
| 89 | +{ |
| 90 | + "iso2Code": "US", |
| 91 | + "iso3Code": "USA", |
| 92 | + "name": "United States of America", |
| 93 | + "commonName": "United States", |
| 94 | + "callingCode": "+1", |
| 95 | + "capital": "Washington, D.C.", |
| 96 | + "region": "Americas", |
| 97 | + "subregion": "North America", |
| 98 | + "area": 9833517.85, |
| 99 | + "population": 331002651 |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +### Creating a Currency |
| 104 | +```json |
| 105 | +POST /countries-currencies/currencies |
| 106 | +{ |
| 107 | + "code": "USD", |
| 108 | + "name": "US Dollar", |
| 109 | + "symbol": "$", |
| 110 | + "exchangeRate": 1.0, |
| 111 | + "isBaseCurrency": true, |
| 112 | + "decimalPlaces": 2, |
| 113 | + "countryId": "550e8400-e29b-41d4-a716-446655440001" |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +### Creating an Exchange Rate |
| 118 | +```json |
| 119 | +POST /countries-currencies/exchange-rates |
| 120 | +{ |
| 121 | + "fromCurrencyId": "650e8400-e29b-41d4-a716-446655440001", |
| 122 | + "toCurrencyId": "650e8400-e29b-41d4-a716-446655440004", |
| 123 | + "rate": 0.85, |
| 124 | + "effectiveDate": "2024-01-01T00:00:00Z", |
| 125 | + "source": "ECB" |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +### Converting Currency |
| 130 | +``` |
| 131 | +GET /countries-currencies/convert?from=650e8400-e29b-41d4-a716-446655440001&to=650e8400-e29b-41d4-a716-446655440004&amount=100 |
| 132 | +``` |
| 133 | + |
| 134 | +## Business Rules |
| 135 | + |
| 136 | +1. **Country ISO Codes**: Must be unique across the system |
| 137 | +2. **Currency Codes**: Must be unique and follow ISO 4217 standard |
| 138 | +3. **Base Currency**: Only one currency can be set as base currency at a time |
| 139 | +4. **Exchange Rates**: Cannot have duplicate rates for the same currency pair and effective date |
| 140 | +5. **Effective Dates**: Cannot be set in the past |
| 141 | +6. **Soft Deletes**: All entities support soft deletion |
| 142 | + |
| 143 | +## Integration |
| 144 | + |
| 145 | +This module can be integrated with: |
| 146 | +- **Assets Module**: Link assets to default currencies |
| 147 | +- **Companies Module**: Set company default currencies |
| 148 | +- **Procurement Module**: Handle multi-currency purchases |
| 149 | +- **Financial Reports**: Generate reports in different currencies |
| 150 | + |
| 151 | +## Migration |
| 152 | + |
| 153 | +The module includes a migration file that: |
| 154 | +- Creates the necessary database tables |
| 155 | +- Sets up indexes for optimal performance |
| 156 | +- Creates foreign key relationships |
| 157 | +- Inserts initial sample data for testing |
| 158 | + |
| 159 | +Run the migration with: |
| 160 | +```bash |
| 161 | +npm run migration:run |
| 162 | +``` |
0 commit comments