From d5a90219e8f8d44ac69e639a6e6bef74ebd4e954 Mon Sep 17 00:00:00 2001 From: Henning <leutz@pcsg.de> Date: Mon, 24 Mar 2025 15:17:58 +0100 Subject: [PATCH] feat(Currency.js): get default currency This commit introduces several changes in bin/Currency.js to make the code more readable and consistent: 1. Improved function declaration style: Rather than using `function(arg1, arg2) { .. }`, we now use `function (arg1, arg2) { .. }` for better style consistency. 2. Cleaned up unnecessary whitespace: Removed redundant lines to make the code cleaner and more readable. 3. Added a new function: Introduced a new function `getDefaultCurrency()` for fetching the default system currency. This asynchronous function returns a Promise that resolves to the code of the default currency. --- bin/Currency.js | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/bin/Currency.js b/bin/Currency.js index e8a5c8f..697696c 100644 --- a/bin/Currency.js +++ b/bin/Currency.js @@ -11,7 +11,7 @@ define('package/quiqqer/currency/bin/Currency', [ 'Ajax', 'package/quiqqer/currency/bin/classes/BulkConverting' -], function(QUI, QUIDOM, QUIAjax, BulkConverting) { +], function (QUI, QUIDOM, QUIAjax, BulkConverting) { 'use strict'; let Converter = null; @@ -19,7 +19,6 @@ define('package/quiqqer/currency/bin/Currency', [ let SYSTEM_CURRENCY = ''; let getCurrenciesFetch = false; - // package_quiqqer_currency_ajax_setUserCurrency if (typeof window.DEFAULT_CURRENCY !== 'undefined') { def = window.DEFAULT_CURRENCY; @@ -36,7 +35,7 @@ define('package/quiqqer/currency/bin/Currency', [ Extends: QUIDOM, Type: 'package/quiqqer/currency/bin/Currency', - initialize: function(options) { + initialize: function (options) { this.parent(options); this.$currency = def; @@ -56,9 +55,9 @@ define('package/quiqqer/currency/bin/Currency', [ * * @param {String} currencyCode */ - setCurrency: function(currencyCode) { - this.getCurrencies().then(function(currencies) { - const found = currencies.find(function(Currency) { + setCurrency: function (currencyCode) { + this.getCurrencies().then(function (currencies) { + const found = currencies.find(function (Currency) { return Currency.code === currencyCode; }); @@ -79,7 +78,7 @@ define('package/quiqqer/currency/bin/Currency', [ * @param {Boolean} [refresh] - Refresh data from database * @return {Promise} */ - getCurrency: function(currencyCode, refresh) { + getCurrency: function (currencyCode, refresh) { currencyCode = currencyCode || this.$currency; refresh = refresh || false; @@ -108,12 +107,13 @@ define('package/quiqqer/currency/bin/Currency', [ }); }, + /** * Return all available currencies * * @returns {Promise} */ - getCurrencies: function() { + getCurrencies: function () { if (Object.getLength(this.$currencies)) { return Promise.resolve(this.$currencies); } @@ -142,12 +142,33 @@ define('package/quiqqer/currency/bin/Currency', [ }); }, + /** + * Return all available currency types. + * + * @returns {Promise} + */ + getDefaultCurrency: function () { + if (SYSTEM_CURRENCY !== '') { + return Promise.resolve(SYSTEM_CURRENCY); + } + + return new Promise((resolve, reject) => { + QUIAjax.get('package_quiqqer_currency_ajax_getDefault', (result) => { + SYSTEM_CURRENCY = result.code; + resolve(result.code); + }, { + 'package': 'quiqqer/currency', + onError: reject + }); + }); + }, + /** * Return all available currency types. * * @returns {Promise<Array>} */ - getCurrencyTypes: function() { + getCurrencyTypes: function () { if (this.$currencyTypes.length) { return Promise.resolve(this.$currencyTypes); } @@ -171,7 +192,7 @@ define('package/quiqqer/currency/bin/Currency', [ * @param {String} currencyTo * @returns {Promise} */ - convert: function(amount, currencyFrom, currencyTo) { + convert: function (amount, currencyFrom, currencyTo) { currencyTo = currencyTo || this.$currency; currencyFrom = currencyFrom || this.$currency; @@ -193,7 +214,7 @@ define('package/quiqqer/currency/bin/Currency', [ * @param {String} currencyTo * @returns {Promise} */ - convertWithSign: function(amount, currencyFrom, currencyTo) { + convertWithSign: function (amount, currencyFrom, currencyTo) { currencyTo = currencyTo || this.$currency; currencyFrom = currencyFrom || this.$currency; -- GitLab