feat(blog): implement medical review gate for blog posts

- Added new fields to the Blog entity: sources, review_status, reviewer, reviewed_at, review_note, and topic_slug.
- Created API endpoints for reviewing blog posts: GET /api/v1/admin/blog/review-queue and POST /api/v1/admin/blog/{uuid}/review.
- Updated BlogController to handle review logic, including approval and rejection of posts.
- Introduced BlogReviewPage component for admin interface to manage blog reviews.
- Added migration to update the database schema for new fields.
- Implemented tests for review queue functionality and review decision handling.
This commit is contained in:
hamed
2026-07-23 21:01:58 +03:30
parent 2abf915f95
commit 14730e43ce
10 changed files with 746 additions and 0 deletions
+107
View File
@@ -221,11 +221,26 @@ class BlogController extends BaseController
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'title و body الزامی است', 422);
}
// Idempotency for the content pipeline: a topic_slug is a stable (specialty,
// angle) key. A re-run must not create a duplicate — return the existing post.
$topicSlug = isset($data['topic_slug']) ? trim((string) $data['topic_slug']) : '';
if ($topicSlug !== '') {
$existing = $this->blogRepo->findByTopicSlug($topicSlug);
if ($existing !== null) {
return $this->success(['data' => $existing->toArray()], 200);
}
}
$blog = new Blog($user, $title, $body);
if (!empty($data['summary'])) $blog->setSummary($data['summary']);
if (!empty($data['tags'])) $blog->setTags((array)$data['tags']);
if (!empty($data['sources'])) $blog->setSources((array)$data['sources']);
if (!empty($data['status'])) $blog->setStatus($data['status']);
if (!empty($data['image_url'])) $blog->setImageUrl($data['image_url']);
if ($topicSlug !== '') $blog->setTopicSlug($topicSlug);
// review_status: null means "manual admin post". The pipeline sends
// "pending_review" so the post enters the doctor review queue.
if (!empty($data['review_status'])) $blog->setReviewStatus($data['review_status']);
// نبودِ city_id یعنی سراسری — پس همیشه اعمال می‌شود، نه فقط وقتی مقدار دارد.
$blog->setCity($this->resolveCity($data['city_id'] ?? null));
@@ -310,6 +325,98 @@ class BlogController extends BaseController
return $this->success(['data' => $blog->toArray()]);
}
// ── Medical-review gate ───────────────────────────────────────────────────
#[OA\Get(
path: '/api/v1/admin/blog/review-queue',
summary: 'List blog drafts awaiting doctor review (admin only)',
security: [['bearerAuth' => []]],
parameters: [
new OA\Parameter(name: 'page', in: 'query', required: false, schema: new OA\Schema(type: 'integer', default: 1)),
new OA\Parameter(name: 'limit', in: 'query', required: false, schema: new OA\Schema(type: 'integer', default: 20, maximum: 50)),
],
responses: [
new OA\Response(response: 200, description: 'Paginated review queue'),
new OA\Response(response: 401, description: 'Unauthorized'),
new OA\Response(response: 403, description: 'Forbidden — admin role required'),
]
)]
#[IsGranted('ROLE_ADMIN')]
#[Route('/api/v1/admin/blog/review-queue', methods: ['GET'])]
public function reviewQueue(Request $request): JsonResponse
{
$page = max(1, (int) $request->query->get('page', 1));
$limit = min(50, max(1, (int) $request->query->get('limit', 20)));
$items = array_map(
fn(Blog $b) => $b->toListArray(),
$this->blogRepo->findByReviewStatus(Blog::REVIEW_PENDING, $page, $limit)
);
$total = $this->blogRepo->countByReviewStatus(Blog::REVIEW_PENDING);
return $this->paginated($items, $total, $page, $limit);
}
#[OA\Post(
path: '/api/v1/admin/blog/{uuid}/review',
summary: 'Approve or reject a blog draft (admin only). Approving may publish it.',
security: [['bearerAuth' => []]],
requestBody: new OA\RequestBody(
required: true,
content: new OA\JsonContent(
required: ['decision'],
properties: [
new OA\Property(property: 'decision', type: 'string', enum: ['approved', 'rejected']),
new OA\Property(property: 'note', type: 'string', nullable: true, description: 'Reviewer note (required message on rejection).'),
new OA\Property(property: 'publish', type: 'boolean', nullable: true, description: 'On approval, publish immediately (default true).'),
]
)
),
parameters: [
new OA\Parameter(name: 'uuid', in: 'path', required: true, schema: new OA\Schema(type: 'string', format: 'uuid')),
],
responses: [
new OA\Response(response: 200, description: 'Review decision recorded'),
new OA\Response(response: 401, description: 'Unauthorized'),
new OA\Response(response: 403, description: 'Forbidden — admin role required'),
new OA\Response(response: 404, description: 'Blog post not found'),
new OA\Response(response: 422, description: 'Invalid decision'),
]
)]
#[IsGranted('ROLE_ADMIN')]
#[Route('/api/v1/admin/blog/{uuid}/review', methods: ['POST'])]
public function review(string $uuid, Request $request, #[CurrentUser] User $user): JsonResponse
{
$blog = $this->blogRepo->findByUuid($uuid);
if ($blog === null) {
return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'مقاله یافت نشد', 404);
}
$data = json_decode($request->getContent(), true) ?? [];
$decision = $data['decision'] ?? null;
$note = isset($data['note']) ? trim((string) $data['note']) : null;
if (!in_array($decision, [Blog::REVIEW_APPROVED, Blog::REVIEW_REJECTED], true)) {
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'decision باید approved یا rejected باشد', 422);
}
if ($decision === Blog::REVIEW_REJECTED && ($note === null || $note === '')) {
return $this->error(ErrorCodes::ERR_VALIDATION_002, 'برای رد مقاله، درج دلیل الزامی است', 422);
}
$blog->applyReview($decision, $user, $note);
// Approving publishes by default; rejecting keeps the post as a draft.
if ($decision === Blog::REVIEW_APPROVED && ($data['publish'] ?? true)) {
$blog->setStatus(Blog::STATUS_PUBLISHED);
} elseif ($decision === Blog::REVIEW_REJECTED) {
$blog->setStatus(Blog::STATUS_DRAFT);
}
$this->blogRepo->save($blog);
return $this->success(['data' => $blog->toArray()]);
}
#[OA\Delete(
path: '/api/v1/blog/{uuid}',
summary: 'Delete a blog post (admin only)',