refactor(posts): replace PostStatusGuard with PostStatusRules for editing and deletion checks
- Removed the PostStatusGuard class and replaced its usage with the new PostStatusRules utility across multiple controllers and actions, enhancing code organization and maintainability. - Updated error message handling to utilize the centralized method in PostStatusRules, ensuring consistency in user feedback. - Deleted associated tests for PostStatusGuard, reflecting the removal of the class.
This commit is contained in:
parent
7854596579
commit
72c9c93a85
8 changed files with 21 additions and 20 deletions
|
|
@ -9,6 +9,7 @@
|
|||
use App\Jobs\PublishPost;
|
||||
use App\Models\Post;
|
||||
use App\Models\Workspace;
|
||||
use App\Support\PostStatusRules;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
|
@ -20,7 +21,7 @@ class UpdatePost
|
|||
*/
|
||||
public static function execute(Workspace $workspace, Post $post, array $data): array
|
||||
{
|
||||
if (PostStatusGuard::blocksEditing($post)) {
|
||||
if (PostStatusRules::blocksEditing($post)) {
|
||||
return ['post' => $post, 'action' => PostAction::Finalized];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@
|
|||
|
||||
use App\Actions\Post\CreatePost;
|
||||
use App\Actions\Post\DeletePost;
|
||||
use App\Actions\Post\PostStatusGuard;
|
||||
use App\Actions\Post\UpdatePost;
|
||||
use App\Enums\Media\Type as MediaType;
|
||||
use App\Enums\Post\Action as PostAction;
|
||||
|
|
@ -20,6 +19,7 @@
|
|||
use App\Http\Resources\Api\PostResource;
|
||||
use App\Models\Post;
|
||||
use App\Services\Post\MediaAttacher;
|
||||
use App\Support\PostStatusRules;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
|
|
@ -70,7 +70,7 @@ public function update(UpdatePostRequest $request, Post $post): PostResource|Jso
|
|||
|
||||
if (data_get($result, 'action') === PostAction::Finalized) {
|
||||
return response()->json(
|
||||
['message' => PostStatusGuard::editBlockedMessage()],
|
||||
['message' => PostStatusRules::editBlockedMessage()],
|
||||
Response::HTTP_UNPROCESSABLE_ENTITY
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,11 +4,11 @@
|
|||
|
||||
namespace App\Http\Controllers\App;
|
||||
|
||||
use App\Actions\Post\PostStatusGuard;
|
||||
use App\Enums\Media\Source;
|
||||
use App\Http\Requests\App\Ai\RegeneratePostMediaImageRequest;
|
||||
use App\Jobs\Ai\RegeneratePostMediaImage;
|
||||
use App\Models\Post;
|
||||
use App\Support\PostStatusRules;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Str;
|
||||
|
|
@ -22,9 +22,9 @@ public function regenerate(RegeneratePostMediaImageRequest $request, Post $post,
|
|||
|
||||
$workspace = $request->user()->currentWorkspace;
|
||||
|
||||
if (PostStatusGuard::blocksEditing($post)) {
|
||||
if (PostStatusRules::blocksEditing($post)) {
|
||||
return response()->json([
|
||||
'message' => PostStatusGuard::editBlockedMessage(),
|
||||
'message' => PostStatusRules::editBlockedMessage(),
|
||||
], Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@
|
|||
use App\Actions\Post\CreatePost;
|
||||
use App\Actions\Post\DeletePost;
|
||||
use App\Actions\Post\DuplicatePost;
|
||||
use App\Actions\Post\PostStatusGuard;
|
||||
use App\Actions\Post\SyncPostPlatforms;
|
||||
use App\Actions\Post\UpdatePost;
|
||||
use App\Enums\Post\Action as PostAction;
|
||||
|
|
@ -23,6 +22,7 @@
|
|||
use App\Services\Post\PostMetricsFetcher;
|
||||
use App\Services\Social\PinterestPublisher;
|
||||
use App\Services\Social\TikTokCreatorInfo;
|
||||
use App\Support\PostStatusRules;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
|
|
@ -220,7 +220,7 @@ public function edit(Request $request, Post $post): Response|RedirectResponse
|
|||
|
||||
$this->authorize('update', $post);
|
||||
|
||||
if (PostStatusGuard::blocksEditing($post)) {
|
||||
if (PostStatusRules::blocksEditing($post)) {
|
||||
return redirect()->route('app.posts.show', $post);
|
||||
}
|
||||
|
||||
|
|
@ -314,7 +314,7 @@ public function destroy(Request $request, Post $post): RedirectResponse
|
|||
|
||||
$this->authorize('delete', $post);
|
||||
|
||||
if (PostStatusGuard::blocksDeletion($post)) {
|
||||
if (PostStatusRules::blocksDeletion($post)) {
|
||||
session()->flash('flash.banner', __('posts.flash.cannot_delete_published'));
|
||||
session()->flash('flash.bannerStyle', 'danger');
|
||||
|
||||
|
|
|
|||
|
|
@ -4,12 +4,12 @@
|
|||
|
||||
namespace App\Mcp\Tools\Post;
|
||||
|
||||
use App\Actions\Post\PostStatusGuard;
|
||||
use App\Actions\Post\UpdatePost;
|
||||
use App\Enums\Post\Action as PostAction;
|
||||
use App\Enums\Post\Status;
|
||||
use App\Http\Resources\Api\PostResource;
|
||||
use App\Models\Post;
|
||||
use App\Support\PostStatusRules;
|
||||
use Illuminate\Contracts\JsonSchema\JsonSchema;
|
||||
use Laravel\Mcp\Request;
|
||||
use Laravel\Mcp\Response;
|
||||
|
|
@ -49,7 +49,7 @@ public function handle(Request $request): Response|ResponseFactory
|
|||
]);
|
||||
|
||||
if (data_get($result, 'action') === PostAction::Finalized) {
|
||||
return Response::error(PostStatusGuard::editBlockedMessage());
|
||||
return Response::error(PostStatusRules::editBlockedMessage());
|
||||
}
|
||||
|
||||
/** @var Post $updated */
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
|
||||
namespace App\Mcp\Tools\Post;
|
||||
|
||||
use App\Actions\Post\PostStatusGuard;
|
||||
use App\Actions\Post\UpdatePost;
|
||||
use App\Enums\Post\Action as PostAction;
|
||||
use App\Enums\Post\Status;
|
||||
|
|
@ -12,6 +11,7 @@
|
|||
use App\Http\Resources\Api\PostResource;
|
||||
use App\Models\Post;
|
||||
use App\Rules\ContentTypeMatchesPostPlatform;
|
||||
use App\Support\PostStatusRules;
|
||||
use Illuminate\Contracts\JsonSchema\JsonSchema;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Laravel\Mcp\Request;
|
||||
|
|
@ -56,7 +56,7 @@ public function handle(Request $request): Response|ResponseFactory
|
|||
$result = UpdatePost::execute($workspace, $post, $payload);
|
||||
|
||||
if (data_get($result, 'action') === PostAction::Finalized) {
|
||||
return Response::error(PostStatusGuard::editBlockedMessage());
|
||||
return Response::error(PostStatusRules::editBlockedMessage());
|
||||
}
|
||||
|
||||
/** @var Post $updated */
|
||||
|
|
|
|||
|
|
@ -2,12 +2,12 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Actions\Post;
|
||||
namespace App\Support;
|
||||
|
||||
use App\Enums\Post\Status as PostStatus;
|
||||
use App\Models\Post;
|
||||
|
||||
class PostStatusGuard
|
||||
class PostStatusRules
|
||||
{
|
||||
private const EDIT_BLOCKED_MESSAGE_KEY = 'posts.cannot_edit_finalized';
|
||||
|
||||
|
|
@ -2,14 +2,14 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Actions\Post\PostStatusGuard;
|
||||
use App\Enums\Post\Status as PostStatus;
|
||||
use App\Models\Post;
|
||||
use App\Support\PostStatusRules;
|
||||
|
||||
test('blocks editing for terminal statuses', function (PostStatus $status) {
|
||||
$post = Post::factory()->make(['status' => $status]);
|
||||
|
||||
expect(PostStatusGuard::blocksEditing($post))->toBeTrue();
|
||||
expect(PostStatusRules::blocksEditing($post))->toBeTrue();
|
||||
})->with([
|
||||
PostStatus::Publishing,
|
||||
PostStatus::Published,
|
||||
|
|
@ -20,7 +20,7 @@
|
|||
test('allows editing for non terminal statuses', function (PostStatus $status) {
|
||||
$post = Post::factory()->make(['status' => $status]);
|
||||
|
||||
expect(PostStatusGuard::blocksEditing($post))->toBeFalse();
|
||||
expect(PostStatusRules::blocksEditing($post))->toBeFalse();
|
||||
})->with([
|
||||
PostStatus::Draft,
|
||||
PostStatus::Scheduled,
|
||||
|
|
@ -29,7 +29,7 @@
|
|||
test('blocks deletion for published statuses', function (PostStatus $status) {
|
||||
$post = Post::factory()->make(['status' => $status]);
|
||||
|
||||
expect(PostStatusGuard::blocksDeletion($post))->toBeTrue();
|
||||
expect(PostStatusRules::blocksDeletion($post))->toBeTrue();
|
||||
})->with([
|
||||
PostStatus::Publishing,
|
||||
PostStatus::Published,
|
||||
|
|
@ -39,7 +39,7 @@
|
|||
test('allows deletion for draft, scheduled and failed statuses', function (PostStatus $status) {
|
||||
$post = Post::factory()->make(['status' => $status]);
|
||||
|
||||
expect(PostStatusGuard::blocksDeletion($post))->toBeFalse();
|
||||
expect(PostStatusRules::blocksDeletion($post))->toBeFalse();
|
||||
})->with([
|
||||
PostStatus::Draft,
|
||||
PostStatus::Scheduled,
|
||||
Loading…
Reference in a new issue