-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d27f815
commit 0961598
Showing
6 changed files
with
100 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Request } from 'express'; | ||
|
||
const backUpRates: { [key: string]: number } = { | ||
// As updated as possible in case the API fails | ||
EGP: 1, | ||
USD: 49.3, | ||
EUR: 52.81, | ||
GBP: 63.65, | ||
}; | ||
|
||
const validCurrencies: string[] = ['EGP', 'USD', 'EUR', 'GBP']; | ||
|
||
export default class currencyConverterService { | ||
static async getRequestCurrency(req: Request): Promise<string> { | ||
if (req.body.currency) { | ||
if (validCurrencies.includes(req.body.currency)) { | ||
return req.body.currency; | ||
} | ||
} | ||
return 'EGP'; | ||
} | ||
|
||
static async getConversionRate(to: string, from: string = 'EGP'): Promise<number> { | ||
try { | ||
const response = await fetch(`https://api.exchangeratesapi.io/latest?base=${from}`); | ||
const data = await response.json(); | ||
return data.rates[to]; | ||
} catch (error) { | ||
return backUpRates[to]; | ||
} | ||
} | ||
|
||
static async convertPrices(prices: number[], to: string, from: string = 'EGP'): Promise<number[]> { | ||
const conversionRate = await this.getConversionRate(to, from); | ||
return prices.map((price) => price / conversionRate); | ||
} | ||
|
||
static async convertPrice(price: number, to: string, from: string = 'EGP'): Promise<number> { | ||
const conversionRate = await this.getConversionRate(to, from); | ||
return price / conversionRate; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters