From f32a12a3e4d5f14fe9543d7c459dbc6dc06a7a75 Mon Sep 17 00:00:00 2001 From: Ruben van der Linde Date: Sat, 19 Oct 2024 22:34:09 +0200 Subject: [PATCH 1/2] getting the directory pages to work --- lib/Controller/DirectoryController.php | 7 ++-- lib/Service/DirectoryService.php | 36 +++++++++++++++------ src/entities/listing/listing.mock.ts | 2 ++ src/entities/listing/listing.ts | 4 +-- src/entities/listing/listing.types.ts | 1 + src/sidebars/directory/DirectorySideBar.vue | 8 ++--- src/views/directory/DirectoryList.vue | 4 +-- 7 files changed, 41 insertions(+), 21 deletions(-) diff --git a/lib/Controller/DirectoryController.php b/lib/Controller/DirectoryController.php index 2c7e31b1..8440ad78 100644 --- a/lib/Controller/DirectoryController.php +++ b/lib/Controller/DirectoryController.php @@ -71,11 +71,12 @@ public function index(): JSONResponse public function update(): JSONResponse { // Get the URL from the request parameters - $url = $this->request->getParam('url'); - + $url = $this->request->getParam('directory'); + + // http://directory.opencatalogi.nl // Check if the URL parameter is provided if (empty($url) === true) { - return new JSONResponse(['error' => 'url parameter is required'], 400); + return new JSONResponse(['error' => 'directory parameter is required'], 400); } // Sync the external directory with the provided URL diff --git a/lib/Service/DirectoryService.php b/lib/Service/DirectoryService.php index f4193b42..14bcb9b5 100644 --- a/lib/Service/DirectoryService.php +++ b/lib/Service/DirectoryService.php @@ -15,6 +15,7 @@ use OCP\IURLGenerator; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; +use Symfony\Component\Uid\Uuid; /** * Service class for handling directory-related operations @@ -140,8 +141,14 @@ private function getDirectoryFromCatalog(Catalog|array $catalog): array $catalog = $catalog->jsonSerialize(); } - // Set id to uuid - $catalog['id'] = $catalog['uuid']; + // Set id to uuid if it's not a valid UUID and uuid field exists with a valid UUID + if ( + (!isset($catalog['id']) && isset($catalog['uuid'])) + || + (!Uuid::isValid($catalog['id']) && isset($catalog['uuid']) && Uuid::isValid($catalog['uuid'])) + ) { + $catalog['id'] = $catalog['uuid']; + } // Remove unneeded fields unset($catalog['image'], $catalog['uuid']); @@ -187,7 +194,7 @@ private function getListings(): array public function getDirectories(): array { // Get all the listings - $listings = $this->objectService->getObjects(objectType: 'listing', extend: ['publicationTypes','organization']); + $listings = $this->objectService->getObjects(objectType: 'listing'); $listings = array_map([$this, 'getDirectoryFromListing'], $listings); // TODO: Define when a listed item should not be shown (e.g. when secret or trusted is true), this is a product decision @@ -250,7 +257,7 @@ public function doCronSync(): array { */ public function validateExternalListing(array $listing): bool { - if (empty($listing['uuid']) === true) { + if (empty($listing['id']) || !Uuid::isValid($listing['id'])) { return false; } @@ -305,23 +312,31 @@ public function syncExternalDirectory(string $url): array $result = $this->client->get($url); } + $results = json_decode($result->getBody()->getContents(), true); $addedListings = []; $updatedListings = []; - + $invalidListings = []; foreach ($results['results'] as $listing) { // Validate the listing (Note: at this point 'uuid' has been moved to the 'id' field in each $listing) if ($this->validateExternalListing($listing) === false) { + $invalidListings[] = $listing['directory'].'/'.$listing['id']; continue; } // Check if we already have this listing // TODO: This is tricky because it requires a local database call so won't work with open registers - $oldListing = $this->listingMapper->findByCatalogIdAndDirectory($listing['uuid'], $listing['directory']); - if ($oldListing !== null) { - $this->updateListing($listing, $oldListing); + $oldListing = $this->objectService->getObjects( + objectType: 'listing', + limit: 1, + filters: [ + ['id'=>$listing['id'], 'directory'=>$listing['directory']] + ] + ); + if ($oldListing !== null && is_array($oldListing) && !empty($oldListing)) { + $this->updateListing($listing, $oldListing[0]); // @todo listing will be added to updatedList even if nothing changed... - $updatedListings[] = $listing['directory'].'/'.$listing['uuid']; + $updatedListings[] = $listing['directory'].'/'.$listing['id']; continue; } @@ -329,10 +344,11 @@ public function syncExternalDirectory(string $url): array // Save the new listing $listingObject = $this->objectService->saveObject('listing', $listing); $listing = $listingObject->jsonSerialize(); - $addedListings[] = $listing['directory'].'/'.$listing['uuid']; + $addedListings[] = $listing['directory'].'/'.$listing['id']; } return [ + 'invalidListings' => $invalidListings, 'addedListings' => $addedListings, 'updatedListings' => $updatedListings, 'total' => count($addedListings) + count($updatedListings) diff --git a/src/entities/listing/listing.mock.ts b/src/entities/listing/listing.mock.ts index f350c0bd..a90adb17 100644 --- a/src/entities/listing/listing.mock.ts +++ b/src/entities/listing/listing.mock.ts @@ -16,6 +16,7 @@ export const mockListingsData = (): TListing[] => [ lastSync: '2024-07-25T00:00:00Z', default: true, available: false, + publicationTypes: [], organization: { // full data id: '1', title: 'Decat', @@ -41,6 +42,7 @@ export const mockListingsData = (): TListing[] => [ lastSync: '', default: true, available: false, + publicationTypes: [], organization: { // full data id: '1', title: 'Decat', diff --git a/src/entities/listing/listing.ts b/src/entities/listing/listing.ts index 36d69e8e..38246d66 100644 --- a/src/entities/listing/listing.ts +++ b/src/entities/listing/listing.ts @@ -18,7 +18,7 @@ export class Listing implements TListing { public available: boolean public default: boolean public organization: string|TOrganization - + public publicationTypes: any[] constructor(data: TListing) { this.hydrate(data) } @@ -39,7 +39,7 @@ export class Listing implements TListing { this.available = data.available || true this.default = data.default || false this.organization = data.organization || '' - + this.publicationTypes = data.publicationTypes || [] } /* istanbul ignore next */ diff --git a/src/entities/listing/listing.types.ts b/src/entities/listing/listing.types.ts index 1f2d2688..01cc49ee 100644 --- a/src/entities/listing/listing.types.ts +++ b/src/entities/listing/listing.types.ts @@ -15,4 +15,5 @@ export type TListing = { available: boolean default: boolean organization: string|TOrganization + publicationTypes: any[] } diff --git a/src/sidebars/directory/DirectorySideBar.vue b/src/sidebars/directory/DirectorySideBar.vue index 6eb37a10..4a42038c 100644 --- a/src/sidebars/directory/DirectorySideBar.vue +++ b/src/sidebars/directory/DirectorySideBar.vue @@ -106,11 +106,11 @@ import { navigationStore, directoryStore, publicationTypeStore } from '../../sto Welke publicatietype zou u uit deze catalogus willen overnemen?
- - {{ publicationTypeSingular.title ?? publicationTypeSingular.source ?? publicationTypeSingular }} + {{ publicationType.title ?? publicationType.source ?? publicationType }}
diff --git a/src/views/directory/DirectoryList.vue b/src/views/directory/DirectoryList.vue index 25d86750..d0d1ed77 100644 --- a/src/views/directory/DirectoryList.vue +++ b/src/views/directory/DirectoryList.vue @@ -42,8 +42,8 @@ import { navigationStore, directoryStore } from '../../store/store.js' :key="`${listing}${i}`" :name="listing.name ?? listing.title" :active="directoryStore.listingItem?.id === listing?.id" - :details="listing?.organization?.title || 'Geen organisatie'" - :counter-number="listing?.publicationType?.length || '0'" + :details="listing.organization?.title || 'Geen organisatie'" + :counter-number="listing.publicationTypes?.length || '0'" :force-display-actions="true" @click="setActive(listing)">