Skip to content

Commit

Permalink
feat: update exchange rate when saving api key
Browse files Browse the repository at this point in the history
  • Loading branch information
ellite committed Feb 19, 2024
1 parent 4a5f5ca commit bf8a989
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
33 changes: 18 additions & 15 deletions endpoints/currency/update_exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@
require_once '../../includes/connect_endpoint.php';

$shouldUpdate = true;
$query = "SELECT date FROM last_exchange_update";
$result = $db->querySingle($query);

if ($result) {
$lastUpdateDate = new DateTime($result);
$currentDate = new DateTime();
$lastUpdateDateString = $lastUpdateDate->format('Y-m-d');
$currentDateString = $currentDate->format('Y-m-d');
$shouldUpdate = $lastUpdateDateString < $currentDateString;
}

if (!$shouldUpdate) {
echo "Rates are current, no need to update.";
exit;
if (isset($_GET['force']) && $_GET['force'] === "true") {
$shouldUpdate = true;
} else {
$query = "SELECT date FROM last_exchange_update";
$result = $db->querySingle($query);

if ($result) {
$lastUpdateDate = new DateTime($result);
$currentDate = new DateTime();
$lastUpdateDateString = $lastUpdateDate->format('Y-m-d');
$currentDateString = $currentDate->format('Y-m-d');
$shouldUpdate = $lastUpdateDateString < $currentDateString;
}

if (!$shouldUpdate) {
echo "Rates are current, no need to update.";
exit;
}
}

$query = "SELECT api_key, provider FROM fixer";
Expand Down Expand Up @@ -52,10 +57,8 @@
]);
$response = file_get_contents($api_url, false, $context);
} else {

$api_url = "http://data.fixer.io/api/latest?access_key=". $apiKey . "&base=EUR&symbols=" . $codes;
$response = file_get_contents($api_url);

}

$apiData = json_decode($response, true);
Expand Down
27 changes: 20 additions & 7 deletions endpoints/user/save_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
session_start();

function update_exchange_rate($db) {
$query = "SELECT api_key FROM fixer";
$query = "SELECT api_key, provider FROM fixer";
$result = $db->query($query);

if ($result) {
$row = $result->fetchArray(SQLITE3_ASSOC);

if ($row) {
$apiKey = $row['api_key'];
$provider = $row['provider'];

$codes = "";
$query = "SELECT id, name, symbol, code FROM currencies";
Expand All @@ -29,8 +30,20 @@ function update_exchange_rate($db) {
$mainCurrencyCode = $row['code'];
$mainCurrencyId = $row['main_currency'];

$api_url = "http://data.fixer.io/api/latest?access_key=". $apiKey . "&base=EUR&symbols=" . $codes;
$response = file_get_contents($api_url);
if ($provider === 1) {
$api_url = "https://api.apilayer.com/fixer/latest?base=EUR&symbols=" . $codes;
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => 'apikey: ' . $apiKey,
]
]);
$response = file_get_contents($api_url, false, $context);
} else {
$api_url = "http://data.fixer.io/api/latest?access_key=". $apiKey . "&base=EUR&symbols=" . $codes;
$response = file_get_contents($api_url);
}

$apiData = json_decode($response, true);

$mainCurrencyToEUR = $apiData['rates'][$mainCurrencyCode];
Expand Down Expand Up @@ -125,9 +138,9 @@ function update_exchange_rate($db) {
if ($result) {
$cookieExpire = time() + (30 * 24 * 60 * 60);
$oldLanguage = isset($_COOKIE['language']) ? $_COOKIE['language'] : "en";
$root = str_replace('/endpoints/user', '', dirname($_SERVER['PHP_SELF']));
$root = $root == '' ? '/' : $root;
setcookie('language', $language, $cookieExpire, $root);
$root = str_replace('/endpoints/user', '', dirname($_SERVER['PHP_SELF']));
$root = $root == '' ? '/' : $root;
setcookie('language', $language, $cookieExpire, $root);
if ($username != $oldUsername) {
$_SESSION['username'] = $username;
if (isset($_COOKIE['wallos_login'])) {
Expand Down Expand Up @@ -168,4 +181,4 @@ function update_exchange_rate($db) {
echo json_encode($response);
exit();
}
?>
?>
2 changes: 1 addition & 1 deletion includes/version.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php
$version = "v1.2.5";
$version = "v1.3.0";
?>
2 changes: 2 additions & 0 deletions scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ function addFixerKeyButton() {
if (data.success) {
showSuccessMessage(data.message);
document.getElementById("addFixerKey").disabled = false;
// update currency exchange rates
fetch("endpoints/currency/update_exchange.php?force=true");
} else {
showErrorMessage(data.message);
document.getElementById("addFixerKey").disabled = false;
Expand Down

0 comments on commit bf8a989

Please sign in to comment.