Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvdlinde committed Oct 19, 2024
2 parents 65002ec + a7445ac commit f8a8154
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 25 deletions.
5 changes: 3 additions & 2 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
],
'routes' => [
// Custom
['name' => 'listing#synchronise', 'url' => '/api/listing/synchronise/{id?}', 'verb' => 'POST'],
['name' => 'listing#synchronise', 'url' => '/api/listings/synchronise/{id?}', 'verb' => 'POST'],
['name' => 'directory#index', 'url' => '/api/directory', 'verb' => 'GET'],
['name' => 'directory#view', 'url' => '/api/directory/{id}', 'verb' => 'GET'],
['name' => 'directory#show', 'url' => '/api/directory/{id}', 'verb' => 'GET'],
['name' => 'directory#update', 'url' => '/api/directory', 'verb' => 'POST'],
['name' => 'directory#publicationType', 'url' => '/api/directory/publication_type/{id}', 'verb' => 'GET'],
// Dashboard
['name' => 'dashboard#index', 'url' => '/index', 'verb' => 'GET'],
['name' => 'dashboard#page', 'url' => '/', 'verb' => 'GET'],
Expand Down
31 changes: 27 additions & 4 deletions lib/Controller/DirectoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use GuzzleHttp\Exception\GuzzleException;
use OCA\OpenCatalogi\Db\ListingMapper;
use OCA\OpenCatalogi\Service\DirectoryService;
use OCA\OpenCatalogi\Service\ObjectService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
Expand All @@ -28,13 +29,15 @@ class DirectoryController extends Controller
* @param IAppConfig $config The app configuration
* @param ListingMapper $listingMapper The listing mapper
* @param DirectoryService $directoryService The directory service
* @param ObjectService $objectService The object service
*/
public function __construct(
$appName,
IRequest $request,
private readonly IAppConfig $config,
private readonly ListingMapper $listingMapper,
private readonly DirectoryService $directoryService
private readonly DirectoryService $directoryService,
private readonly ObjectService $objectService
)
{
parent::__construct($appName, $request);
Expand Down Expand Up @@ -71,11 +74,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
Expand All @@ -101,4 +105,23 @@ public function show(string|int $id): JSONResponse
return new JSONResponse([]);
}

/**
* Get a specific publication type
*
* @NoAdminRequired
* @NoCSRFRequired
* @param string|int $id The ID of the publication type to retrieve
* @return JSONResponse The JSON response containing the publication type details
*/
public function publicationType(string|int $id): JSONResponse
{
try {
$publicationType = $this->objectService->get('publicationType', $id);
return new JSONResponse($publicationType);
} catch (DoesNotExistException $e) {
return new JSONResponse(['error' => 'Publication type not found'], 404);
} catch (\Exception $e) {
return new JSONResponse(['error' => 'An error occurred while retrieving the publication type'], 500);
}
}
}
36 changes: 26 additions & 10 deletions lib/Service/DirectoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -305,34 +312,43 @@ 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;
}

// 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)
Expand Down
2 changes: 2 additions & 0 deletions src/entities/listing/listing.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -41,6 +42,7 @@ export const mockListingsData = (): TListing[] => [
lastSync: '',
default: true,
available: false,
publicationTypes: [],
organization: { // full data
id: '1',
title: 'Decat',
Expand Down
4 changes: 2 additions & 2 deletions src/entities/listing/listing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions src/entities/listing/listing.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export type TListing = {
available: boolean
default: boolean
organization: string|TOrganization
publicationTypes: any[]
}
10 changes: 5 additions & 5 deletions src/sidebars/directory/DirectorySideBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ import { navigationStore, directoryStore, publicationTypeStore } from '../../sto
</template>
Welke publicatietype zou u uit deze catalogus willen overnemen?
<div v-if="!loading">
<NcCheckboxRadioSwitch v-for="(publicationTypeSingular, i) in directoryStore.listingItem.publicationType"
:key="`${publicationTypeSingular}${i}`"
:checked.sync="checkedPublicationTypeObject[publicationTypeSingular]"
<NcCheckboxRadioSwitch v-for="(publicationType, i) in directoryStore.listingItem.publicationTypes"
:key="`${publicationType}${i}`"
:checked.sync="checkedPublicationTypeObject[publicationType]"
type="switch">
{{ publicationTypeSingular.title ?? publicationTypeSingular.source ?? publicationTypeSingular }}
{{ publicationType.title ?? publicationType.source ?? publicationType }}
</NcCheckboxRadioSwitch>
</div>
<NcLoadingIcon v-if="loading" :size="20" />
Expand Down Expand Up @@ -319,7 +319,7 @@ export default {
synDirectroy() {
this.syncLoading = true
fetch(
`/index.php/apps/opencatalogi/api/directory/${directoryStore.listingItem.id}/sync`,
`/index.php/apps/opencatalogi/api/listings/synchronise/${directoryStore.listingItem.id}`,
{
method: 'GET',
},
Expand Down
4 changes: 2 additions & 2 deletions src/views/directory/DirectoryList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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)">
<template #icon>
Expand Down

0 comments on commit f8a8154

Please sign in to comment.