From f2053724f5367678822686f44faa6c0b98ac2c71 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Wed, 7 Aug 2024 09:36:56 +0200 Subject: [PATCH 01/10] Merge add and create endpoints --- lib/Controller/DirectoryController.php | 47 +++----------------------- 1 file changed, 5 insertions(+), 42 deletions(-) diff --git a/lib/Controller/DirectoryController.php b/lib/Controller/DirectoryController.php index 6d9c5dd3..32268bf4 100644 --- a/lib/Controller/DirectoryController.php +++ b/lib/Controller/DirectoryController.php @@ -72,19 +72,6 @@ public function page(?string $getParameter) } - /** - * @PublicPage - * @NoCSRFRequired - */ - public function add(?string $url, DirectoryService $directoryService): JSONResponse - { - $directories = []; - $directoryService->registerToExternalDirectory(url: $url, externalDirectories: $directories); - - return new JSONResponse(['listingsAdded' => $directories]); - } - - /** * @PublicPage * @NoCSRFRequired @@ -143,39 +130,15 @@ public function show(string|int $id, ObjectService $objectService, DirectoryServ /** - * @NoAdminRequired + * @PublicPage * @NoCSRFRequired */ - public function create(ObjectService $objectService, DirectoryService $directoryService): JSONResponse + public function create(string $directory, DirectoryService $directoryService): JSONResponse { + $directories = []; + $directoryService->registerToExternalDirectory(url: $directory, externalDirectories: $directories); - $data = $this->request->getParams(); - - foreach($data as $key => $value) { - if(str_starts_with($key, '_')) { - unset($data[$key]); - } - } - - if($this->config->hasKey($this->appName, 'mongoStorage') === false - || $this->config->getValueString($this->appName, 'mongoStorage') !== '1' - ) { - return new JSONResponse($this->listingMapper->createFromArray(object: $data)); - } - - $dbConfig['base_uri'] = $this->config->getValueString(app: $this->appName, key: 'mongodbLocation'); - $dbConfig['headers']['api-key'] = $this->config->getValueString(app: $this->appName, key: 'mongodbKey'); - $dbConfig['mongodbCluster'] = $this->config->getValueString(app: $this->appName, key: 'mongodbCluster'); - - $data['_schema'] = 'directory'; - - $returnData = $objectService->saveObject( - data: $data, - config: $dbConfig - ); - - // get post from requests - return new JSONResponse($returnData); + return new JSONResponse(['results' => $directories]); } /** From 0bcaae08ae8440880cf654c6a52350f25834da3f Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Wed, 7 Aug 2024 11:14:29 +0200 Subject: [PATCH 02/10] Fix bugs from online test --- lib/Service/DirectoryService.php | 7 +++++-- lib/Service/ObjectService.php | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/Service/DirectoryService.php b/lib/Service/DirectoryService.php index 53b06534..75d994a1 100644 --- a/lib/Service/DirectoryService.php +++ b/lib/Service/DirectoryService.php @@ -140,6 +140,9 @@ public function listDirectory(array $filters = [], int $limit = 30, int $offset if ($this->config->hasKey($this->appName, 'mongoStorage') === false || $this->config->getValueString($this->appName, 'mongoStorage') !== '1' ) { + $filters['catalog_id'] = $filters['catalogId']; + unset($filters['catalogId']); + return $this->listingMapper->findAll(limit: $limit, offset: $offset, filters: $filters); } $filters['_schema'] = 'directory'; @@ -232,9 +235,9 @@ public function listCatalog (array $catalog): array 'mongodbCluster' => $this->config->getValueString($this->appName, 'mongodbCluster') ]; - $data['_schema'] = 'catalog'; + $listing['_schema'] = 'directory'; - $returnData = $this->objectService->saveObject($data, $dbConfig); + $returnData = $this->objectService->saveObject($listing, $dbConfig); return $catalog; } catch (\Exception $e) { $catalog['listed'] = false; diff --git a/lib/Service/ObjectService.php b/lib/Service/ObjectService.php index f1660beb..630bdd49 100644 --- a/lib/Service/ObjectService.php +++ b/lib/Service/ObjectService.php @@ -117,7 +117,7 @@ public function findObject(array $filters, array $config): array associative: true ); - return ['document' => $result]; + return $result['document']; } From 50678480e9cd418f1be7a48b2ba598da86026a92 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Wed, 7 Aug 2024 09:17:39 +0000 Subject: [PATCH 03/10] Update src from PHP Codesniffer --- src/store/modules/configuration.js | 10 +++++----- src/store/modules/configuration.spec.js | 10 +++++----- src/store/modules/configuration.specs.js | 12 ++++++------ src/store/modules/metadata.js | 14 +++++++------- src/store/modules/metadata.spec.js | 16 ++++++++-------- src/store/modules/organisation.js | 10 +++++----- src/store/modules/organisation.spec.js | 12 ++++++------ src/store/modules/search.js | 20 ++++++++++---------- src/store/modules/theme.js | 10 +++++----- src/store/modules/theme.spec.js | 12 ++++++------ 10 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/store/modules/configuration.js b/src/store/modules/configuration.js index a1cb0449..d4561a4b 100644 --- a/src/store/modules/configuration.js +++ b/src/store/modules/configuration.js @@ -27,16 +27,16 @@ export const useConfigurationStore = defineStore( ) .then( (response) => { - response.json().then( - (data) => { - this.setConfigurationItem(data) + response.json().then( + (data) => { + this.setConfigurationItem(data) } - ) + ) } ) .catch( (err) => { - console.error(err) + console.error(err) } ) }, diff --git a/src/store/modules/configuration.spec.js b/src/store/modules/configuration.spec.js index 674586b6..814184e0 100644 --- a/src/store/modules/configuration.spec.js +++ b/src/store/modules/configuration.spec.js @@ -5,15 +5,15 @@ import { useConfigurationStore } from './configuration.js' describe( 'Configuration Store', () => { - beforeEach( - () => { - setActivePinia(createPinia()) + beforeEach( + () => { + setActivePinia(createPinia()) } - ) + ) it( 'sets configuration item correctly', () => { - const store = useConfigurationStore() + const store = useConfigurationStore() store.setConfigurationItem(mockConfiguration()[0]) diff --git a/src/store/modules/configuration.specs.js b/src/store/modules/configuration.specs.js index aeb143d3..62920f59 100644 --- a/src/store/modules/configuration.specs.js +++ b/src/store/modules/configuration.specs.js @@ -6,15 +6,15 @@ import { useConfigurationStore } from './configuration.js'; describe( 'Metadata Store', () => { - beforeEach( - () => { - setActivePinia(createPinia()) + beforeEach( + () => { + setActivePinia(createPinia()) } - ) + ) it( 'sets configuration item correctly', () => { - const store = useConfigurationStore() + const store = useConfigurationStore() store.setConfigurationItem(testData[0]) @@ -38,7 +38,7 @@ describe( it( 'sets configuration list correctly', () => { - const store = useConfigurationStore() + const store = useConfigurationStore() store.setConfigurationItem(testData) diff --git a/src/store/modules/metadata.js b/src/store/modules/metadata.js index cb8c0ffd..0de1c28e 100644 --- a/src/store/modules/metadata.js +++ b/src/store/modules/metadata.js @@ -41,19 +41,19 @@ export const useMetadataStore = defineStore( ) .then( (response) => { - response.json().then( - (data) => { - this.metaDataList = data.results.map( - (metadataItem) => new Metadata(metadataItem), - ) + response.json().then( + (data) => { + this.metaDataList = data.results.map( + (metadataItem) => new Metadata(metadataItem), + ) return data } - ) + ) } ) .catch( (err) => { - console.error(err) + console.error(err) return err } ) diff --git a/src/store/modules/metadata.spec.js b/src/store/modules/metadata.spec.js index 514b5062..38b1a8ab 100644 --- a/src/store/modules/metadata.spec.js +++ b/src/store/modules/metadata.spec.js @@ -6,15 +6,15 @@ import { Metadata, mockMetadata } from '../../entities/index.js' describe( 'Metadata Store', () => { - beforeEach( - () => { - setActivePinia(createPinia()) + beforeEach( + () => { + setActivePinia(createPinia()) } - ) + ) it( 'sets metadata item correctly', () => { - const store = useMetadataStore() + const store = useMetadataStore() store.setMetaDataItem(mockMetadata()[0]) @@ -27,7 +27,7 @@ describe( it( 'sets metadata item with string "properties" property', () => { - const store = useMetadataStore() + const store = useMetadataStore() // stringify json data const mockData = mockMetadata()[0] @@ -44,7 +44,7 @@ describe( it( 'sets metadata list correctly', () => { - const store = useMetadataStore() + const store = useMetadataStore() store.setMetaDataList(mockMetadata()) @@ -57,7 +57,7 @@ describe( it( 'get metadata property from key', () => { - const store = useMetadataStore() + const store = useMetadataStore() store.setMetaDataItem(mockMetadata()[0]) store.setMetadataDataKey('test') diff --git a/src/store/modules/organisation.js b/src/store/modules/organisation.js index 61b952b7..3844677c 100644 --- a/src/store/modules/organisation.js +++ b/src/store/modules/organisation.js @@ -33,16 +33,16 @@ export const useOrganisationStore = defineStore( ) .then( (response) => { - response.json().then( - (data) => { - this.setOrganisationList(data.results) + response.json().then( + (data) => { + this.setOrganisationList(data.results) } - ) + ) } ) .catch( (err) => { - console.error(err) + console.error(err) } ) }, diff --git a/src/store/modules/organisation.spec.js b/src/store/modules/organisation.spec.js index 27385c8e..b14a260b 100644 --- a/src/store/modules/organisation.spec.js +++ b/src/store/modules/organisation.spec.js @@ -6,15 +6,15 @@ import { useOrganisationStore } from './organisation.js'; describe( 'Organisation Store', () => { - beforeEach( - () => { - setActivePinia(createPinia()) + beforeEach( + () => { + setActivePinia(createPinia()) } - ) + ) it( 'sets organisation item correctly', () => { - const store = useOrganisationStore() + const store = useOrganisationStore() store.setOrganisationItem(mockOrganisation()[0]) @@ -38,7 +38,7 @@ describe( it( 'sets organisation list correctly', () => { - const store = useOrganisationStore() + const store = useOrganisationStore() store.setOrganisationList(mockOrganisation()) diff --git a/src/store/modules/search.js b/src/store/modules/search.js index db326f1e..cd1a532b 100644 --- a/src/store/modules/search.js +++ b/src/store/modules/search.js @@ -27,22 +27,22 @@ export const useSearchStore = defineStore( ) .then( (response) => { - response.json().then( - (data) => { - if (data?.code === 403 && data?.message) { - this.searchError = data.message - console.log(this.searchError) - } else { - this.searchError = '' // Clear any previous errors - } + response.json().then( + (data) => { + if (data?.code === 403 && data?.message) { + this.searchError = data.message + console.log(this.searchError) + } else { + this.searchError = '' // Clear any previous errors + } this.searchResults = data } - ) + ) } ) .catch( (err) => { - this.searchError = err.message || 'An error occurred' + this.searchError = err.message || 'An error occurred' console.error(err.message ?? err) } ) diff --git a/src/store/modules/theme.js b/src/store/modules/theme.js index e7fe9bd1..0cecf1b0 100644 --- a/src/store/modules/theme.js +++ b/src/store/modules/theme.js @@ -33,16 +33,16 @@ export const useThemeStore = defineStore( ) .then( (response) => { - response.json().then( - (data) => { - this.setThemeList(data.results) + response.json().then( + (data) => { + this.setThemeList(data.results) } - ) + ) } ) .catch( (err) => { - console.error(err) + console.error(err) } ) }, diff --git a/src/store/modules/theme.spec.js b/src/store/modules/theme.spec.js index 34931d81..cb7b85cc 100644 --- a/src/store/modules/theme.spec.js +++ b/src/store/modules/theme.spec.js @@ -6,15 +6,15 @@ import { mockTheme, Theme } from '../../entities/index.js' describe( 'Theme Store', () => { - beforeEach( - () => { - setActivePinia(createPinia()) + beforeEach( + () => { + setActivePinia(createPinia()) } - ) + ) it( 'sets theme item correctly', () => { - const store = useThemeStore() + const store = useThemeStore() store.setThemeItem(mockTheme()[0]) @@ -38,7 +38,7 @@ describe( it( 'sets theme list correctly', () => { - const store = useThemeStore() + const store = useThemeStore() store.setThemeList(mockTheme()) From 375d48ac2603877877318bd67b51032ce6cc9914 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Wed, 7 Aug 2024 11:30:15 +0200 Subject: [PATCH 04/10] return empty array if there are no aggregations --- lib/Service/ElasticSearchService.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/Service/ElasticSearchService.php b/lib/Service/ElasticSearchService.php index da7bcdf6..73bf354c 100644 --- a/lib/Service/ElasticSearchService.php +++ b/lib/Service/ElasticSearchService.php @@ -191,6 +191,8 @@ public function searchObject(array $filters, array $config): array $return = ['results' => array_map(callback: [$this, 'formatResults'], array: $result['hits']['hits'])]; if(isset($result['aggregations']) === true) { $return['facets'] = array_map([$this, 'mapAggregationResults'], $result['aggregations']); + } else { + $return['facets'] = []; } return $return; From 15b63bd25b59f92fc933cc3608983b655b5041fe Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Wed, 7 Aug 2024 11:30:48 +0200 Subject: [PATCH 05/10] format datetime --- lib/Db/Listing.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Db/Listing.php b/lib/Db/Listing.php index edcdc382..de0825ad 100644 --- a/lib/Db/Listing.php +++ b/lib/Db/Listing.php @@ -78,7 +78,7 @@ public function jsonSerialize(): array 'metadata' => $this->metadata, 'catalogId' => $this->catalogId, 'status' => $this->status, - 'lastSync' => $this->lastSync, + 'lastSync' => $this->lastSync->format('c'), 'default' => $this->default, 'available' => $this->available, ]; From 896c9ecae270f2ff69f3680401c437106f16f39f Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Wed, 7 Aug 2024 09:33:36 +0000 Subject: [PATCH 06/10] Update src from PHP Codesniffer --- src/store/modules/configuration.js | 10 +++++----- src/store/modules/configuration.spec.js | 10 +++++----- src/store/modules/configuration.specs.js | 12 ++++++------ src/store/modules/metadata.js | 14 +++++++------- src/store/modules/metadata.spec.js | 16 ++++++++-------- src/store/modules/organisation.js | 10 +++++----- src/store/modules/organisation.spec.js | 12 ++++++------ src/store/modules/search.js | 20 ++++++++++---------- src/store/modules/theme.js | 10 +++++----- src/store/modules/theme.spec.js | 12 ++++++------ 10 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/store/modules/configuration.js b/src/store/modules/configuration.js index d4561a4b..a1cb0449 100644 --- a/src/store/modules/configuration.js +++ b/src/store/modules/configuration.js @@ -27,16 +27,16 @@ export const useConfigurationStore = defineStore( ) .then( (response) => { - response.json().then( - (data) => { - this.setConfigurationItem(data) + response.json().then( + (data) => { + this.setConfigurationItem(data) } - ) + ) } ) .catch( (err) => { - console.error(err) + console.error(err) } ) }, diff --git a/src/store/modules/configuration.spec.js b/src/store/modules/configuration.spec.js index 814184e0..674586b6 100644 --- a/src/store/modules/configuration.spec.js +++ b/src/store/modules/configuration.spec.js @@ -5,15 +5,15 @@ import { useConfigurationStore } from './configuration.js' describe( 'Configuration Store', () => { - beforeEach( - () => { - setActivePinia(createPinia()) + beforeEach( + () => { + setActivePinia(createPinia()) } - ) + ) it( 'sets configuration item correctly', () => { - const store = useConfigurationStore() + const store = useConfigurationStore() store.setConfigurationItem(mockConfiguration()[0]) diff --git a/src/store/modules/configuration.specs.js b/src/store/modules/configuration.specs.js index 62920f59..aeb143d3 100644 --- a/src/store/modules/configuration.specs.js +++ b/src/store/modules/configuration.specs.js @@ -6,15 +6,15 @@ import { useConfigurationStore } from './configuration.js'; describe( 'Metadata Store', () => { - beforeEach( - () => { - setActivePinia(createPinia()) + beforeEach( + () => { + setActivePinia(createPinia()) } - ) + ) it( 'sets configuration item correctly', () => { - const store = useConfigurationStore() + const store = useConfigurationStore() store.setConfigurationItem(testData[0]) @@ -38,7 +38,7 @@ describe( it( 'sets configuration list correctly', () => { - const store = useConfigurationStore() + const store = useConfigurationStore() store.setConfigurationItem(testData) diff --git a/src/store/modules/metadata.js b/src/store/modules/metadata.js index 0de1c28e..cb8c0ffd 100644 --- a/src/store/modules/metadata.js +++ b/src/store/modules/metadata.js @@ -41,19 +41,19 @@ export const useMetadataStore = defineStore( ) .then( (response) => { - response.json().then( - (data) => { - this.metaDataList = data.results.map( - (metadataItem) => new Metadata(metadataItem), - ) + response.json().then( + (data) => { + this.metaDataList = data.results.map( + (metadataItem) => new Metadata(metadataItem), + ) return data } - ) + ) } ) .catch( (err) => { - console.error(err) + console.error(err) return err } ) diff --git a/src/store/modules/metadata.spec.js b/src/store/modules/metadata.spec.js index 38b1a8ab..514b5062 100644 --- a/src/store/modules/metadata.spec.js +++ b/src/store/modules/metadata.spec.js @@ -6,15 +6,15 @@ import { Metadata, mockMetadata } from '../../entities/index.js' describe( 'Metadata Store', () => { - beforeEach( - () => { - setActivePinia(createPinia()) + beforeEach( + () => { + setActivePinia(createPinia()) } - ) + ) it( 'sets metadata item correctly', () => { - const store = useMetadataStore() + const store = useMetadataStore() store.setMetaDataItem(mockMetadata()[0]) @@ -27,7 +27,7 @@ describe( it( 'sets metadata item with string "properties" property', () => { - const store = useMetadataStore() + const store = useMetadataStore() // stringify json data const mockData = mockMetadata()[0] @@ -44,7 +44,7 @@ describe( it( 'sets metadata list correctly', () => { - const store = useMetadataStore() + const store = useMetadataStore() store.setMetaDataList(mockMetadata()) @@ -57,7 +57,7 @@ describe( it( 'get metadata property from key', () => { - const store = useMetadataStore() + const store = useMetadataStore() store.setMetaDataItem(mockMetadata()[0]) store.setMetadataDataKey('test') diff --git a/src/store/modules/organisation.js b/src/store/modules/organisation.js index 3844677c..61b952b7 100644 --- a/src/store/modules/organisation.js +++ b/src/store/modules/organisation.js @@ -33,16 +33,16 @@ export const useOrganisationStore = defineStore( ) .then( (response) => { - response.json().then( - (data) => { - this.setOrganisationList(data.results) + response.json().then( + (data) => { + this.setOrganisationList(data.results) } - ) + ) } ) .catch( (err) => { - console.error(err) + console.error(err) } ) }, diff --git a/src/store/modules/organisation.spec.js b/src/store/modules/organisation.spec.js index b14a260b..27385c8e 100644 --- a/src/store/modules/organisation.spec.js +++ b/src/store/modules/organisation.spec.js @@ -6,15 +6,15 @@ import { useOrganisationStore } from './organisation.js'; describe( 'Organisation Store', () => { - beforeEach( - () => { - setActivePinia(createPinia()) + beforeEach( + () => { + setActivePinia(createPinia()) } - ) + ) it( 'sets organisation item correctly', () => { - const store = useOrganisationStore() + const store = useOrganisationStore() store.setOrganisationItem(mockOrganisation()[0]) @@ -38,7 +38,7 @@ describe( it( 'sets organisation list correctly', () => { - const store = useOrganisationStore() + const store = useOrganisationStore() store.setOrganisationList(mockOrganisation()) diff --git a/src/store/modules/search.js b/src/store/modules/search.js index cd1a532b..db326f1e 100644 --- a/src/store/modules/search.js +++ b/src/store/modules/search.js @@ -27,22 +27,22 @@ export const useSearchStore = defineStore( ) .then( (response) => { - response.json().then( - (data) => { - if (data?.code === 403 && data?.message) { - this.searchError = data.message - console.log(this.searchError) - } else { - this.searchError = '' // Clear any previous errors - } + response.json().then( + (data) => { + if (data?.code === 403 && data?.message) { + this.searchError = data.message + console.log(this.searchError) + } else { + this.searchError = '' // Clear any previous errors + } this.searchResults = data } - ) + ) } ) .catch( (err) => { - this.searchError = err.message || 'An error occurred' + this.searchError = err.message || 'An error occurred' console.error(err.message ?? err) } ) diff --git a/src/store/modules/theme.js b/src/store/modules/theme.js index 0cecf1b0..e7fe9bd1 100644 --- a/src/store/modules/theme.js +++ b/src/store/modules/theme.js @@ -33,16 +33,16 @@ export const useThemeStore = defineStore( ) .then( (response) => { - response.json().then( - (data) => { - this.setThemeList(data.results) + response.json().then( + (data) => { + this.setThemeList(data.results) } - ) + ) } ) .catch( (err) => { - console.error(err) + console.error(err) } ) }, diff --git a/src/store/modules/theme.spec.js b/src/store/modules/theme.spec.js index cb7b85cc..34931d81 100644 --- a/src/store/modules/theme.spec.js +++ b/src/store/modules/theme.spec.js @@ -6,15 +6,15 @@ import { mockTheme, Theme } from '../../entities/index.js' describe( 'Theme Store', () => { - beforeEach( - () => { - setActivePinia(createPinia()) + beforeEach( + () => { + setActivePinia(createPinia()) } - ) + ) it( 'sets theme item correctly', () => { - const store = useThemeStore() + const store = useThemeStore() store.setThemeItem(mockTheme()[0]) @@ -38,7 +38,7 @@ describe( it( 'sets theme list correctly', () => { - const store = useThemeStore() + const store = useThemeStore() store.setThemeList(mockTheme()) From 0d527f83b83226f14989a3cb90cd9fff48f3ab31 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Wed, 7 Aug 2024 11:44:43 +0200 Subject: [PATCH 07/10] Try prevent infinite loop --- lib/Service/SearchService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Service/SearchService.php b/lib/Service/SearchService.php index 446ac2fd..a67c27de 100644 --- a/lib/Service/SearchService.php +++ b/lib/Service/SearchService.php @@ -102,7 +102,7 @@ public function search(array $parameters, array $elasticConfig, array $dbConfig, foreach($directory as $instance) { if( $instance['default'] === false - && isset($parameters['.catalogi']) === true + || isset($parameters['.catalogi']) === true && in_array($instance['catalogId'], $parameters['.catalogi']) === false ) { continue; From 8f568a9f1ce9686c6b2fed2dcd001fa98d2dd701 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Wed, 7 Aug 2024 09:50:08 +0000 Subject: [PATCH 08/10] Update src from PHP Codesniffer --- src/store/modules/configuration.js | 10 +++++----- src/store/modules/configuration.spec.js | 10 +++++----- src/store/modules/configuration.specs.js | 12 ++++++------ src/store/modules/metadata.js | 14 +++++++------- src/store/modules/metadata.spec.js | 16 ++++++++-------- src/store/modules/organisation.js | 10 +++++----- src/store/modules/organisation.spec.js | 12 ++++++------ src/store/modules/search.js | 20 ++++++++++---------- src/store/modules/theme.js | 10 +++++----- src/store/modules/theme.spec.js | 12 ++++++------ 10 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/store/modules/configuration.js b/src/store/modules/configuration.js index a1cb0449..d4561a4b 100644 --- a/src/store/modules/configuration.js +++ b/src/store/modules/configuration.js @@ -27,16 +27,16 @@ export const useConfigurationStore = defineStore( ) .then( (response) => { - response.json().then( - (data) => { - this.setConfigurationItem(data) + response.json().then( + (data) => { + this.setConfigurationItem(data) } - ) + ) } ) .catch( (err) => { - console.error(err) + console.error(err) } ) }, diff --git a/src/store/modules/configuration.spec.js b/src/store/modules/configuration.spec.js index 674586b6..814184e0 100644 --- a/src/store/modules/configuration.spec.js +++ b/src/store/modules/configuration.spec.js @@ -5,15 +5,15 @@ import { useConfigurationStore } from './configuration.js' describe( 'Configuration Store', () => { - beforeEach( - () => { - setActivePinia(createPinia()) + beforeEach( + () => { + setActivePinia(createPinia()) } - ) + ) it( 'sets configuration item correctly', () => { - const store = useConfigurationStore() + const store = useConfigurationStore() store.setConfigurationItem(mockConfiguration()[0]) diff --git a/src/store/modules/configuration.specs.js b/src/store/modules/configuration.specs.js index aeb143d3..62920f59 100644 --- a/src/store/modules/configuration.specs.js +++ b/src/store/modules/configuration.specs.js @@ -6,15 +6,15 @@ import { useConfigurationStore } from './configuration.js'; describe( 'Metadata Store', () => { - beforeEach( - () => { - setActivePinia(createPinia()) + beforeEach( + () => { + setActivePinia(createPinia()) } - ) + ) it( 'sets configuration item correctly', () => { - const store = useConfigurationStore() + const store = useConfigurationStore() store.setConfigurationItem(testData[0]) @@ -38,7 +38,7 @@ describe( it( 'sets configuration list correctly', () => { - const store = useConfigurationStore() + const store = useConfigurationStore() store.setConfigurationItem(testData) diff --git a/src/store/modules/metadata.js b/src/store/modules/metadata.js index cb8c0ffd..0de1c28e 100644 --- a/src/store/modules/metadata.js +++ b/src/store/modules/metadata.js @@ -41,19 +41,19 @@ export const useMetadataStore = defineStore( ) .then( (response) => { - response.json().then( - (data) => { - this.metaDataList = data.results.map( - (metadataItem) => new Metadata(metadataItem), - ) + response.json().then( + (data) => { + this.metaDataList = data.results.map( + (metadataItem) => new Metadata(metadataItem), + ) return data } - ) + ) } ) .catch( (err) => { - console.error(err) + console.error(err) return err } ) diff --git a/src/store/modules/metadata.spec.js b/src/store/modules/metadata.spec.js index 514b5062..38b1a8ab 100644 --- a/src/store/modules/metadata.spec.js +++ b/src/store/modules/metadata.spec.js @@ -6,15 +6,15 @@ import { Metadata, mockMetadata } from '../../entities/index.js' describe( 'Metadata Store', () => { - beforeEach( - () => { - setActivePinia(createPinia()) + beforeEach( + () => { + setActivePinia(createPinia()) } - ) + ) it( 'sets metadata item correctly', () => { - const store = useMetadataStore() + const store = useMetadataStore() store.setMetaDataItem(mockMetadata()[0]) @@ -27,7 +27,7 @@ describe( it( 'sets metadata item with string "properties" property', () => { - const store = useMetadataStore() + const store = useMetadataStore() // stringify json data const mockData = mockMetadata()[0] @@ -44,7 +44,7 @@ describe( it( 'sets metadata list correctly', () => { - const store = useMetadataStore() + const store = useMetadataStore() store.setMetaDataList(mockMetadata()) @@ -57,7 +57,7 @@ describe( it( 'get metadata property from key', () => { - const store = useMetadataStore() + const store = useMetadataStore() store.setMetaDataItem(mockMetadata()[0]) store.setMetadataDataKey('test') diff --git a/src/store/modules/organisation.js b/src/store/modules/organisation.js index 61b952b7..3844677c 100644 --- a/src/store/modules/organisation.js +++ b/src/store/modules/organisation.js @@ -33,16 +33,16 @@ export const useOrganisationStore = defineStore( ) .then( (response) => { - response.json().then( - (data) => { - this.setOrganisationList(data.results) + response.json().then( + (data) => { + this.setOrganisationList(data.results) } - ) + ) } ) .catch( (err) => { - console.error(err) + console.error(err) } ) }, diff --git a/src/store/modules/organisation.spec.js b/src/store/modules/organisation.spec.js index 27385c8e..b14a260b 100644 --- a/src/store/modules/organisation.spec.js +++ b/src/store/modules/organisation.spec.js @@ -6,15 +6,15 @@ import { useOrganisationStore } from './organisation.js'; describe( 'Organisation Store', () => { - beforeEach( - () => { - setActivePinia(createPinia()) + beforeEach( + () => { + setActivePinia(createPinia()) } - ) + ) it( 'sets organisation item correctly', () => { - const store = useOrganisationStore() + const store = useOrganisationStore() store.setOrganisationItem(mockOrganisation()[0]) @@ -38,7 +38,7 @@ describe( it( 'sets organisation list correctly', () => { - const store = useOrganisationStore() + const store = useOrganisationStore() store.setOrganisationList(mockOrganisation()) diff --git a/src/store/modules/search.js b/src/store/modules/search.js index db326f1e..cd1a532b 100644 --- a/src/store/modules/search.js +++ b/src/store/modules/search.js @@ -27,22 +27,22 @@ export const useSearchStore = defineStore( ) .then( (response) => { - response.json().then( - (data) => { - if (data?.code === 403 && data?.message) { - this.searchError = data.message - console.log(this.searchError) - } else { - this.searchError = '' // Clear any previous errors - } + response.json().then( + (data) => { + if (data?.code === 403 && data?.message) { + this.searchError = data.message + console.log(this.searchError) + } else { + this.searchError = '' // Clear any previous errors + } this.searchResults = data } - ) + ) } ) .catch( (err) => { - this.searchError = err.message || 'An error occurred' + this.searchError = err.message || 'An error occurred' console.error(err.message ?? err) } ) diff --git a/src/store/modules/theme.js b/src/store/modules/theme.js index e7fe9bd1..0cecf1b0 100644 --- a/src/store/modules/theme.js +++ b/src/store/modules/theme.js @@ -33,16 +33,16 @@ export const useThemeStore = defineStore( ) .then( (response) => { - response.json().then( - (data) => { - this.setThemeList(data.results) + response.json().then( + (data) => { + this.setThemeList(data.results) } - ) + ) } ) .catch( (err) => { - console.error(err) + console.error(err) } ) }, diff --git a/src/store/modules/theme.spec.js b/src/store/modules/theme.spec.js index 34931d81..cb7b85cc 100644 --- a/src/store/modules/theme.spec.js +++ b/src/store/modules/theme.spec.js @@ -6,15 +6,15 @@ import { mockTheme, Theme } from '../../entities/index.js' describe( 'Theme Store', () => { - beforeEach( - () => { - setActivePinia(createPinia()) + beforeEach( + () => { + setActivePinia(createPinia()) } - ) + ) it( 'sets theme item correctly', () => { - const store = useThemeStore() + const store = useThemeStore() store.setThemeItem(mockTheme()[0]) @@ -38,7 +38,7 @@ describe( it( 'sets theme list correctly', () => { - const store = useThemeStore() + const store = useThemeStore() store.setThemeList(mockTheme()) From db87d04c56f8b24d271bc698752470349f98be87 Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Wed, 7 Aug 2024 14:14:41 +0200 Subject: [PATCH 09/10] Add condition to not request local --- lib/Service/SearchService.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/Service/SearchService.php b/lib/Service/SearchService.php index a67c27de..a24ee48c 100644 --- a/lib/Service/SearchService.php +++ b/lib/Service/SearchService.php @@ -4,6 +4,7 @@ use GuzzleHttp\Client; use GuzzleHttp\Promise\Utils; +use OCP\IURLGenerator; use Symfony\Component\Uid\Uuid; class SearchService @@ -18,6 +19,7 @@ class SearchService public function __construct( private readonly ElasticSearchService $elasticService, private readonly DirectoryService $directoryService, + private readonly IURLGenerator $urlGenerator, ) { $this->client = new Client(); } @@ -98,12 +100,14 @@ public function search(array $parameters, array $elasticConfig, array $dbConfig, $searchEndpoints = []; + $promises = []; foreach($directory as $instance) { if( $instance['default'] === false || isset($parameters['.catalogi']) === true && in_array($instance['catalogId'], $parameters['.catalogi']) === false + || $instance['search'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->linkToRoute(routeName:"opencatalogi.directory.index")) ) { continue; } From e9e3cd78500062012badd92b0f3449db25fa16ee Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Wed, 7 Aug 2024 15:02:20 +0200 Subject: [PATCH 10/10] Add preflight cors --- appinfo/routes.php | 3 +- lib/Controller/SearchController.php | 59 ++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index 5593d3a3..3a3e2056 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -21,6 +21,7 @@ ['name' => 'directory#page', 'url' => '/directory', 'verb' => 'GET'], ['name' => 'directory#add', 'url' => '/api/directory/add', 'verb' => 'POST'], ['name' => 'configuration#index', 'url' => '/configuration', 'verb' => 'GET'], - ['name' => 'configuration#create', 'url' => '/configuration', 'verb' => 'POST'] + ['name' => 'configuration#create', 'url' => '/configuration', 'verb' => 'POST'], + ['name' => 'search#preflighted_cors', 'url' => '/api/{path}', 'verb' => 'OPTIONS', 'requirements' => ['path' => '.+']] ], ]; diff --git a/lib/Controller/SearchController.php b/lib/Controller/SearchController.php index 27732845..e8f6f186 100644 --- a/lib/Controller/SearchController.php +++ b/lib/Controller/SearchController.php @@ -5,36 +5,60 @@ use OCA\OpenCatalogi\Service\ElasticSearchService; use OCA\OpenCatalogi\Db\PublicationMapper; use OCA\OpenCatalogi\Service\SearchService; +use OCP\AppFramework\ApiController; use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\Attribute\NoCSRFRequired; +use OCP\AppFramework\Http\Attribute\PublicPage; +use OCP\AppFramework\Http\Response; use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\JSONResponse; use OCP\IAppConfig; use OCP\IRequest; - class SearchController extends Controller { - const TEST_ARRAY = [ - "d9e1467e-fc55-44c8-bf5c-bf139ac10eda" => [ - "id" => "d9e1467e-fc55-44c8-bf5c-bf139ac10eda", - "name" => "Search one", - "summary" => "summary for one" - ], - "e9d0131b-06c4-4d20-aa17-3b2aaad186d7" => [ - "id" => "e9d0131b-06c4-4d20-aa17-3b2aaad186d7", - "name" => "Search two", - "summary" => "summary for two" - ] - ]; public function __construct( $appName, IRequest $request, private readonly PublicationMapper $publicationMapper, - private readonly IAppConfig $config) - { - parent::__construct($appName, $request); + private readonly IAppConfig $config, + $corsMethods = 'PUT, POST, GET, DELETE, PATCH', + $corsAllowedHeaders = 'Authorization, Content-Type, Accept', + $corsMaxAge = 1728000 + ) { + parent::__construct($appName, $request); + $this->corsMethods = $corsMethods; + $this->corsAllowedHeaders = $corsAllowedHeaders; + $this->corsMaxAge = $corsMaxAge; } + /** + * This method implements a preflighted cors response for you that you can + * link to for the options request + * + * @NoAdminRequired + * @NoCSRFRequired + * @PublicPage + * @since 7.0.0 + */ + #[NoCSRFRequired] + #[PublicPage] + public function preflightedCors() { + if (isset($this->request->server['HTTP_ORIGIN'])) { + $origin = $this->request->server['HTTP_ORIGIN']; + } else { + $origin = '*'; + } + + $response = new Response(); + $response->addHeader('Access-Control-Allow-Origin', $origin); + $response->addHeader('Access-Control-Allow-Methods', $this->corsMethods); + $response->addHeader('Access-Control-Max-Age', (string)$this->corsMaxAge); + $response->addHeader('Access-Control-Allow-Headers', $this->corsAllowedHeaders); + $response->addHeader('Access-Control-Allow-Credentials', 'false'); + return $response; + } + /** * @NoAdminRequired * @NoCSRFRequired @@ -51,10 +75,11 @@ public function page(?string $getParameter) [] ); } - + /** * @PublicPage * @NoCSRFRequired + * @CORS */ public function index(SearchService $searchService): JSONResponse {