From bb41316430f4385ea89dbda57d18097a776978e9 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Tue, 4 Apr 2023 15:51:51 +0200
Subject: [PATCH] Bug 33408: Fetch sysprefs from svc/config/systempreferences

It will be easier to mock/set them from cypress tests.

Sponsored-by: BULAC - http://www.bulac.fr/
---
 .../prog/js/vue/components/ERM/Main.vue       | 132 +++++++++++-------
 .../fetch/system-preferences-api-client.js    |   4 +
 svc/config/systempreferences                  |  30 ++++
 3 files changed, 116 insertions(+), 50 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/Main.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/Main.vue
index ae4b5b56a58..7267659e0d1 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/Main.vue
+++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/ERM/Main.vue
@@ -1,5 +1,5 @@
 <template>
-    <div v-if="ERMModule">
+    <div v-if="initialized">
         <div id="sub-header">
             <Breadcrumb />
             <Help />
@@ -140,8 +140,6 @@ export default {
             vendorStore,
             AVStore,
             setError,
-            erm_providers,
-            ERMModule,
             loading,
             loaded,
         }
@@ -149,62 +147,96 @@ export default {
     data() {
         return {
             component: "agreement",
+            initialized: false,
+            ERMModule: null,
+            erm_providers: [],
         }
     },
     beforeCreate() {
-        if (!this.ERMModule) {
-            return this.setError(
-                this.$__(
-                    'The e-resource management module is disabled, turn on <a href="/cgi-bin/koha/admin/preferences.pl?tab=&op=search&searchfield=ERMModule">ERMModule</a> to use it'
-                ),
-                false
-            )
-        }
         this.loading()
 
-        const acq_client = APIClient.acquisition
-        acq_client.vendors.getAll().then(
-            vendors => {
-                this.vendorStore.vendors = vendors
-                this.initialized = true
-            },
-            error => {}
-        )
-
-        const av_client = APIClient.authorised_values
-        const authorised_values = {
-            av_agreement_statuses: "ERM_AGREEMENT_STATUS",
-            av_agreement_closure_reasons: "ERM_AGREEMENT_CLOSURE_REASON",
-            av_agreement_renewal_priorities: "ERM_AGREEMENT_RENEWAL_PRIORITY",
-            av_user_roles: "ERM_USER_ROLES",
-            av_license_types: "ERM_LICENSE_TYPE",
-            av_license_statuses: "ERM_LICENSE_STATUS",
-            av_agreement_license_statuses: "ERM_AGREEMENT_LICENSE_STATUS",
-            av_agreement_license_location: "ERM_AGREEMENT_LICENSE_LOCATION",
-            av_package_types: "ERM_PACKAGE_TYPE",
-            av_package_content_types: "ERM_PACKAGE_CONTENT_TYPE",
-            av_title_publication_types: "ERM_TITLE_PUBLICATION_TYPE",
-        }
+        const fetch_config = () => {
+            let promises = []
 
-        let av_cat_array = Object.keys(authorised_values).map(function (
-            av_cat
-        ) {
-            return '"' + authorised_values[av_cat] + '"'
-        })
-
-        av_client.values
-            .getCategoriesWithValues(av_cat_array)
-            .then(av_categories => {
-                Object.entries(authorised_values).forEach(
-                    ([av_var, av_cat]) => {
-                        const av_match = av_categories.find(
-                            element => element.category_name == av_cat
+            const acq_client = APIClient.acquisition
+            promises.push(
+                acq_client.vendors.getAll().then(
+                    vendors => {
+                        this.vendorStore.vendors = vendors
+                    },
+                    error => {}
+                )
+            )
+
+            const av_client = APIClient.authorised_values
+            const authorised_values = {
+                av_agreement_statuses: "ERM_AGREEMENT_STATUS",
+                av_agreement_closure_reasons: "ERM_AGREEMENT_CLOSURE_REASON",
+                av_agreement_renewal_priorities:
+                    "ERM_AGREEMENT_RENEWAL_PRIORITY",
+                av_user_roles: "ERM_USER_ROLES",
+                av_license_types: "ERM_LICENSE_TYPE",
+                av_license_statuses: "ERM_LICENSE_STATUS",
+                av_agreement_license_statuses: "ERM_AGREEMENT_LICENSE_STATUS",
+                av_agreement_license_location: "ERM_AGREEMENT_LICENSE_LOCATION",
+                av_package_types: "ERM_PACKAGE_TYPE",
+                av_package_content_types: "ERM_PACKAGE_CONTENT_TYPE",
+                av_title_publication_types: "ERM_TITLE_PUBLICATION_TYPE",
+            }
+
+            let av_cat_array = Object.keys(authorised_values).map(function (
+                av_cat
+            ) {
+                return '"' + authorised_values[av_cat] + '"'
+            })
+
+            promises.push(
+                av_client.values
+                    .getCategoriesWithValues(av_cat_array)
+                    .then(av_categories => {
+                        Object.entries(authorised_values).forEach(
+                            ([av_var, av_cat]) => {
+                                const av_match = av_categories.find(
+                                    element => element.category_name == av_cat
+                                )
+                                this.AVStore[av_var] =
+                                    av_match.authorised_values
+                            }
                         )
-                        this.AVStore[av_var] = av_match.authorised_values
-                    }
+                    })
+            )
+
+            promises.push(
+                sysprefs_client.sysprefs.get("ERMProviders").then(
+                    providers => {
+                        this.erm_providers = providers.value.split(",")
+                    },
+                    error => {}
                 )
+            )
+            return Promise.all(promises)
+        }
+
+        const sysprefs_client = APIClient.sysprefs
+        sysprefs_client.sysprefs
+            .get("ERMModule")
+            .then(value => {
+                this.ERMModule = value
+                if (!this.ERMModule) {
+                    this.loaded()
+                    return this.setError(
+                        this.$__(
+                            'The e-resource management module is disabled, turn on <a href="/cgi-bin/koha/admin/preferences.pl?tab=&op=search&searchfield=ERMModule">ERMModule</a> to use it'
+                        ),
+                        false
+                    )
+                }
+                return fetch_config()
+            })
+            .then(() => {
+                this.loaded()
+                this.initialized = true
             })
-            .then(() => this.loaded())
     },
     components: {
         Breadcrumb,
diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/system-preferences-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/system-preferences-api-client.js
index 1e4cd3f7a70..f11906630fc 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/system-preferences-api-client.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/system-preferences-api-client.js
@@ -9,6 +9,10 @@ export class SysprefAPIClient extends HttpClient {
 
     get sysprefs() {
         return {
+            get: (variable) =>
+                this.get({
+                    endpoint: `/?pref=${variable}`,
+                }),
             update: (variable, value) =>
                 this.post({
                     endpoint: "",
diff --git a/svc/config/systempreferences b/svc/config/systempreferences
index acaf3e485f9..367f220b44f 100755
--- a/svc/config/systempreferences
+++ b/svc/config/systempreferences
@@ -70,6 +70,35 @@ sub set_preference {
     C4::Service->return_success( $response );
 }
 
+=head2 get_preference
+
+=over 4
+
+=item url path
+
+GET /svc/config/systempreferences/$preference
+
+=item url query
+
+preference=$pref_name
+
+=back
+
+Used to get a single system preference.
+
+=cut
+
+sub get_preference {
+    my $preference = scalar $query->param('pref');
+
+    my $value = C4::Context->preference( $preference );
+    $response->param( value => $value );
+
+    C4::Service->return_success( $response );
+}
+
+
+
 =head2 set_preferences
 
 =over 4
@@ -109,4 +138,5 @@ sub set_preferences {
 C4::Service->dispatch(
     [ 'POST /([A-Za-z0-9_-]+)', [ 'value' ], \&set_preference ],
     [ 'POST /', [], \&set_preferences ],
+    [ 'GET /', [ 'pref' ], \&get_preference],
 );
-- 
2.25.1