From 023ac4bf208c1ef247dc8140ac7b2d2f6ef6a480 Mon Sep 17 00:00:00 2001 From: Juan24 <93536474+juanbroder24@users.noreply.github.com> Date: Tue, 24 Feb 2026 14:40:43 -0300 Subject: [PATCH] Add organisation controller, transformer, routes Introduce OrganisationTransformer and OrganisationController to expose legacy organisation APIs using Fractal. Transformer accepts an 'unpublished' config to control inclusion of unpublished translations. Controller fetches organisations via the repository, loads details, handles errors with logging, and returns transformed JSON for collection and item endpoints. Update Organisation model to include country_code in the fillable attributes. Register new routes: GET v1/org/ (list), GET v1/org/{code} (detail) and add whatnow/{id} route. --- .../Transformers/OrganisationTransformer.php | 58 ++++++++++ .../Controllers/OrganisationController.php | 108 ++++++++++++++++++ legacy/app/Models/Organisation.php | 3 +- routes/api.php | 3 + 4 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 legacy/app/Classes/Transformers/OrganisationTransformer.php create mode 100644 legacy/app/Http/Controllers/OrganisationController.php diff --git a/legacy/app/Classes/Transformers/OrganisationTransformer.php b/legacy/app/Classes/Transformers/OrganisationTransformer.php new file mode 100644 index 0000000..74beac7 --- /dev/null +++ b/legacy/app/Classes/Transformers/OrganisationTransformer.php @@ -0,0 +1,58 @@ +unpublished = $configuration['unpublished']; + } + } + + /** + * Turn this item object into a generic array + * + * @param Organisation $model + * @return array + */ + public function transform(Organisation $model) + { + $response = [ + 'countryCode' => $model->country_code, + 'name' => $model->org_name, + 'url' => $model->attribution_url, + 'imageUrl' => $model->attribution_file_name ? $model->getAttributionImageUrl() : null, + 'translations' => null, + ]; + + if ($model->details->count()) { + $response['translations'] = []; + foreach ($model->details as $detail) { + if ($this->unpublished || $detail->published) { + $response['translations'][$detail->language_code] = [ + 'languageCode' => $detail->language_code, + 'name' => $detail->org_name, + 'attributionMessage' => $detail->attribution_message, + 'published' => (bool) $detail->published, + ]; + } + } + } + + return $response; + } +} + diff --git a/legacy/app/Http/Controllers/OrganisationController.php b/legacy/app/Http/Controllers/OrganisationController.php new file mode 100644 index 0000000..a26a66e --- /dev/null +++ b/legacy/app/Http/Controllers/OrganisationController.php @@ -0,0 +1,108 @@ +orgRepo = $orgRepo; + $this->request = $request; + $this->manager = $manager; + } + + /** + * @param Request $request + * @return \Symfony\Component\HttpFoundation\Response + */ + public function getAll(Request $request) + { + try { + /** @var Collection $orgs */ + $orgs = $this->orgRepo->all(); + } catch (\Exception $e) { + Log::error('Could not get Organisations list', ['message' => $e->getMessage()]); + return response()->json([ + 'status' => 500, + 'error_message' => 'Could not get Organisations list', + 'errors' => [], + ], 500); + } + + $orgs->each(function (Organisation $org) { + $org->load('details'); + }); + + $resource = new FractalCollection($orgs, new OrganisationTransformer([ + 'unpublished' => $request->header('x-api-key') ? false : true, + ])); + + $response = $this->manager->createData($resource); + return response()->json($response->toArray(), 200); + } + + /** + * @param string $code + * @param Request $request + * @return \Symfony\Component\HttpFoundation\Response + */ + public function getById($code, Request $request) + { + try { + /** @var Organisation $org */ + $org = $this->orgRepo->findByCountryCode($code); + } catch (\Exception $e) { + Log::error('Organisation not found', ['message' => $e->getMessage()]); + return response()->json([ + 'status' => 404, + 'error_message' => 'Organisation does not exist', + 'errors' => ['No matching organisation for country code'], + ], 404); + } + + $org->load('details'); + + $resource = new Item($org, new OrganisationTransformer([ + 'unpublished' => $request->header('x-api-key') ? false : true, + ])); + + $response = $this->manager->createData($resource); + return response()->json($response->toArray(), 200); + } +} + diff --git a/legacy/app/Models/Organisation.php b/legacy/app/Models/Organisation.php index e438fbe..709fa7e 100644 --- a/legacy/app/Models/Organisation.php +++ b/legacy/app/Models/Organisation.php @@ -24,7 +24,8 @@ class Organisation extends Model 'org_name', 'oid', 'attribution_url', - 'attribution_file_name' + 'attribution_file_name', + 'country_code', ]; public function alerts() diff --git a/routes/api.php b/routes/api.php index 766de6b..5874aac 100644 --- a/routes/api.php +++ b/routes/api.php @@ -95,7 +95,10 @@ }); Route::group(['middleware' => 'ApiAuth', 'prefix' => 'v1'], function () { + Route::get('org/', '\\App\\Legacy\\Http\\Controllers\\OrganisationController@getAll'); Route::get('org/{code}/whatnow', '\\App\\Legacy\\Http\\Controllers\\WhatNowController@getFeed'); + Route::get('org/{code}', '\\App\\Legacy\\Http\\Controllers\\OrganisationController@getById'); + Route::get('whatnow/{id}', '\\App\\Legacy\\Http\\Controllers\\WhatNowController@getPublishedById'); Route::any('{any}', function () { return response()->json([