From ffdada0d4ebd99505c4f391a8a7a314876ba7ca0 Mon Sep 17 00:00:00 2001 From: Vincent Auger Date: Tue, 5 Nov 2024 17:51:37 -0400 Subject: [PATCH] chore: pint code format --- .../Expertise/SyncExpertiseWithScience.php | 2 +- app/Http/Controllers/AuthorController.php | 1 + .../Controllers/AuthorEmploymentController.php | 1 - .../Orcid/Requests/DeleteEmploymentRequest.php | 3 +-- .../Orcid/Requests/PutEmploymentRequest.php | 8 +++----- app/Jobs/SyncAuthorEmploymentWithOrcid.php | 18 +++++++++++------- app/Models/Author.php | 14 ++++++++++---- app/Models/AuthorEmployment.php | 1 + app/Policies/AuthorEmploymentPolicy.php | 7 ++++--- tests/Feature/Models/AuthorEmployementTest.php | 6 +++--- 10 files changed, 35 insertions(+), 26 deletions(-) diff --git a/app/Actions/Expertise/SyncExpertiseWithScience.php b/app/Actions/Expertise/SyncExpertiseWithScience.php index e55b40a8..e1dd98f4 100644 --- a/app/Actions/Expertise/SyncExpertiseWithScience.php +++ b/app/Actions/Expertise/SyncExpertiseWithScience.php @@ -22,7 +22,7 @@ public static function handle(): bool $termsToRemove = [ 'bio-informatics', 'biological', - 'Forage plant' + 'Forage plant', ]; // Taken from RESE guidance - retreived on: 2023-10-27 diff --git a/app/Http/Controllers/AuthorController.php b/app/Http/Controllers/AuthorController.php index 1a908352..de71281f 100644 --- a/app/Http/Controllers/AuthorController.php +++ b/app/Http/Controllers/AuthorController.php @@ -41,6 +41,7 @@ public function store(Request $request): JsonResource ]); $author = Author::create($validated); + return new AuthorResource($author); } diff --git a/app/Http/Controllers/AuthorEmploymentController.php b/app/Http/Controllers/AuthorEmploymentController.php index 83aa042d..ce2943d4 100644 --- a/app/Http/Controllers/AuthorEmploymentController.php +++ b/app/Http/Controllers/AuthorEmploymentController.php @@ -111,5 +111,4 @@ public function destroy(Author $author, AuthorEmployment $authorEmployment) return response()->noContent(); } - } diff --git a/app/Http/Integrations/Orcid/Requests/DeleteEmploymentRequest.php b/app/Http/Integrations/Orcid/Requests/DeleteEmploymentRequest.php index d2cceeb6..f29fa88a 100644 --- a/app/Http/Integrations/Orcid/Requests/DeleteEmploymentRequest.php +++ b/app/Http/Integrations/Orcid/Requests/DeleteEmploymentRequest.php @@ -16,7 +16,6 @@ class DeleteEmploymentRequest extends Request protected ActivitiesScopeEndpoint $endpoint = ActivitiesScopeEndpoint::EMPLOYMENT; - public function __construct(readonly protected string $putCode) { @@ -30,6 +29,6 @@ public function __construct(readonly protected string $putCode) */ public function resolveEndpoint(): string { - return $this->endpoint->value . '/' . $this->putCode; + return $this->endpoint->value.'/'.$this->putCode; } } diff --git a/app/Http/Integrations/Orcid/Requests/PutEmploymentRequest.php b/app/Http/Integrations/Orcid/Requests/PutEmploymentRequest.php index 571219de..4535256b 100644 --- a/app/Http/Integrations/Orcid/Requests/PutEmploymentRequest.php +++ b/app/Http/Integrations/Orcid/Requests/PutEmploymentRequest.php @@ -12,19 +12,17 @@ class PutEmploymentRequest extends Request implements HasBody { - use HasJsonBody; + /** * The HTTP method of the request */ protected Method $method = Method::PUT; - public function __construct( readonly protected EmploymentData $employmentData, protected ActivitiesScopeEndpoint $endpoint = ActivitiesScopeEndpoint::EMPLOYMENT - ) - { + ) { if (empty($this->employmentData->putCode)) { throw new OrcidIntegrationException('Putcode is required for this request'); } @@ -40,6 +38,6 @@ protected function defaultBody(): array */ public function resolveEndpoint(): string { - return $this->endpoint->value . '/' . $this->employmentData->putCode; + return $this->endpoint->value.'/'.$this->employmentData->putCode; } } diff --git a/app/Jobs/SyncAuthorEmploymentWithOrcid.php b/app/Jobs/SyncAuthorEmploymentWithOrcid.php index e8b134bd..157ce2e6 100644 --- a/app/Jobs/SyncAuthorEmploymentWithOrcid.php +++ b/app/Jobs/SyncAuthorEmploymentWithOrcid.php @@ -19,7 +19,9 @@ class SyncAuthorEmploymentWithOrcid implements ShouldQueue use Queueable; const SYNC_TYPE_CREATE = 'create'; + const SYNC_TYPE_UPDATE = 'update'; + const SYNC_TYPE_DELETE = 'delete'; public $tries = 3; @@ -45,8 +47,9 @@ public function handle(): void // Does the author have a valid ORCID connection? $author = $this->authorEmployment->author; - if(!$author->hasValidOrcidCredentials()){ + if (! $author->hasValidOrcidCredentials()) { Log::error('SyncAuthor: Author does not have valid ORCID credentials'); + return; } @@ -81,7 +84,7 @@ private function createEmploymentRecordInOrcid(OrcidMemberAPIConnector $connecto $request = new PostEmploymentRequest($employmentData); $response = $connector->send($request); - if($response->failed() || $response->status() !== 201){ + if ($response->failed() || $response->status() !== 201) { Log::error('SyncAuthor: Error creating employment record in ORCID', [ 'status' => $response->status(), 'headers' => $response->headers()->all(), @@ -89,12 +92,12 @@ private function createEmploymentRecordInOrcid(OrcidMemberAPIConnector $connecto $response->throw(); } - if(!$response->header('location')){ + if (! $response->header('location')) { throw new \Exception('No location header in response - cannot get putcode'); } $location = $response->header('location'); - $putcode = (int)Str::afterLast($location, '/'); + $putcode = (int) Str::afterLast($location, '/'); $this->authorEmployment->orcid_putcode = $putcode; $this->authorEmployment->orcid_updated_at = now(); @@ -112,13 +115,14 @@ private function updateEmploymentRecordInOrcid(OrcidMemberAPIConnector $connecto $request = new PutEmploymentRequest($employmentData); $response = $connector->send($request); - if($response->failed() || $response->status() !== 200){ + if ($response->failed() || $response->status() !== 200) { - if($response->status() === 404){ + if ($response->status() === 404) { Log::error('SyncAuthor: Employment record not found in ORCID - Likely Deleted', [ 'author_employment_id' => $this->authorEmployment->id, ]); $this->createEmploymentRecordInOrcid($connector); + return; } @@ -145,7 +149,7 @@ private function deleteEmploymentRecordInOrcid(OrcidMemberAPIConnector $connecto $request = new DeleteEmploymentRequest($putcode); $response = $connector->send($request); - if($response->failed() || $response->status() !== 204 || $response->status() !== 404){ + if ($response->failed() || $response->status() !== 204 || $response->status() !== 404) { Log::error('SyncAuthor: Error deleting employment record in ORCID', [ 'status' => $response->status(), 'headers' => $response->headers()->all(), diff --git a/app/Models/Author.php b/app/Models/Author.php index cb8d55c3..49d5106a 100644 --- a/app/Models/Author.php +++ b/app/Models/Author.php @@ -100,11 +100,17 @@ public function clearOrcidToken(): void */ public function hasValidOrcidCredentials(): bool { - if(!$this->orcid_verified) return false; - if(!$this->orcid_access_token) return false; - if($this->orcid_expires_at < now()) return false; + if (! $this->orcid_verified) { + return false; + } + if (! $this->orcid_access_token) { + return false; + } + if ($this->orcid_expires_at < now()) { + return false; + } - return true; + return true; } // Relationships diff --git a/app/Models/AuthorEmployment.php b/app/Models/AuthorEmployment.php index 8eed4796..54cc57d9 100644 --- a/app/Models/AuthorEmployment.php +++ b/app/Models/AuthorEmployment.php @@ -12,6 +12,7 @@ class AuthorEmployment extends Model { /** @use HasFactory<\Database\Factories\AuthorEmploymentFactory> */ use HasFactory; + use LogsActivity; use SoftDeletes; diff --git a/app/Policies/AuthorEmploymentPolicy.php b/app/Policies/AuthorEmploymentPolicy.php index 86931ef0..efa4ab0a 100644 --- a/app/Policies/AuthorEmploymentPolicy.php +++ b/app/Policies/AuthorEmploymentPolicy.php @@ -5,7 +5,6 @@ use App\Models\Author; use App\Models\AuthorEmployment; use App\Models\User; -use Illuminate\Auth\Access\Response; class AuthorEmploymentPolicy { @@ -30,7 +29,10 @@ public function view(User $user, AuthorEmployment $authorEmployment): bool */ public function create(User $user, Author $author): bool { - if ($user->author->id !== $author->id) return false; + if ($user->author->id !== $author->id) { + return false; + } + return $user->can('create_author_employments'); } @@ -49,5 +51,4 @@ public function delete(User $user, AuthorEmployment $authorEmployment): bool { return $user->author->id === $authorEmployment->author_id; } - } diff --git a/tests/Feature/Models/AuthorEmployementTest.php b/tests/Feature/Models/AuthorEmployementTest.php index f04c968d..165a7da5 100644 --- a/tests/Feature/Models/AuthorEmployementTest.php +++ b/tests/Feature/Models/AuthorEmployementTest.php @@ -38,7 +38,7 @@ }); -test('a user can create a new author employment record', function (){ +test('a user can create a new author employment record', function () { // mock queue to ensure job dispatched Queue::fake(); @@ -70,7 +70,7 @@ }); -test('a user can update an author employment record', function (){ +test('a user can update an author employment record', function () { Queue::fake(); @@ -102,7 +102,7 @@ }); -test('a user can delete an author employment record', function (){ +test('a user can delete an author employment record', function () { Queue::fake(); $user = User::factory()->create();