- 모델 4개: Approval, ApprovalStep, ApprovalForm, ApprovalLine - ApprovalService: 목록/CRUD/워크플로우(상신/승인/반려/회수) 비즈니스 로직 - ApprovalApiController: JSON API 엔드포인트 (기안함/결재함/완료함/참조함) - ApprovalController: Blade 뷰 컨트롤러 (HX-Redirect 처리) - 뷰 8개: drafts, pending, completed, references, create, edit, show - partials: _status-badge, _step-progress, _approval-line-editor - api.php/web.php 라우트 등록
289 lines
7.9 KiB
PHP
289 lines
7.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\ApprovalService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ApprovalApiController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly ApprovalService $service
|
|
) {}
|
|
|
|
// =========================================================================
|
|
// 목록
|
|
// =========================================================================
|
|
|
|
/**
|
|
* 기안함
|
|
*/
|
|
public function drafts(Request $request): JsonResponse
|
|
{
|
|
$result = $this->service->getMyDrafts(
|
|
$request->only(['search', 'status', 'is_urgent', 'date_from', 'date_to']),
|
|
(int) $request->get('per_page', 15)
|
|
);
|
|
|
|
return response()->json($result);
|
|
}
|
|
|
|
/**
|
|
* 결재 대기함
|
|
*/
|
|
public function pending(Request $request): JsonResponse
|
|
{
|
|
$result = $this->service->getPendingForMe(
|
|
auth()->id(),
|
|
$request->only(['search', 'is_urgent', 'date_from', 'date_to']),
|
|
(int) $request->get('per_page', 15)
|
|
);
|
|
|
|
return response()->json($result);
|
|
}
|
|
|
|
/**
|
|
* 처리 완료함
|
|
*/
|
|
public function completed(Request $request): JsonResponse
|
|
{
|
|
$result = $this->service->getCompletedByMe(
|
|
auth()->id(),
|
|
$request->only(['search', 'status', 'date_from', 'date_to']),
|
|
(int) $request->get('per_page', 15)
|
|
);
|
|
|
|
return response()->json($result);
|
|
}
|
|
|
|
/**
|
|
* 참조함
|
|
*/
|
|
public function references(Request $request): JsonResponse
|
|
{
|
|
$result = $this->service->getReferencesForMe(
|
|
auth()->id(),
|
|
$request->only(['search', 'date_from', 'date_to']),
|
|
(int) $request->get('per_page', 15)
|
|
);
|
|
|
|
return response()->json($result);
|
|
}
|
|
|
|
// =========================================================================
|
|
// CRUD
|
|
// =========================================================================
|
|
|
|
/**
|
|
* 상세 조회
|
|
*/
|
|
public function show(int $id): JsonResponse
|
|
{
|
|
$approval = $this->service->getApproval($id);
|
|
|
|
return response()->json(['success' => true, 'data' => $approval]);
|
|
}
|
|
|
|
/**
|
|
* 생성 (임시저장)
|
|
*/
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'form_id' => 'required|exists:approval_forms,id',
|
|
'title' => 'required|string|max:200',
|
|
'body' => 'nullable|string',
|
|
'is_urgent' => 'boolean',
|
|
'steps' => 'nullable|array',
|
|
'steps.*.user_id' => 'required_with:steps|exists:users,id',
|
|
'steps.*.step_type' => 'required_with:steps|in:approval,agreement,reference',
|
|
]);
|
|
|
|
$approval = $this->service->createApproval($request->all());
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '결재 문서가 저장되었습니다.',
|
|
'data' => $approval,
|
|
], 201);
|
|
}
|
|
|
|
/**
|
|
* 수정
|
|
*/
|
|
public function update(Request $request, int $id): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'title' => 'sometimes|string|max:200',
|
|
'body' => 'nullable|string',
|
|
'is_urgent' => 'boolean',
|
|
'steps' => 'nullable|array',
|
|
'steps.*.user_id' => 'required_with:steps|exists:users,id',
|
|
'steps.*.step_type' => 'required_with:steps|in:approval,agreement,reference',
|
|
]);
|
|
|
|
try {
|
|
$approval = $this->service->updateApproval($id, $request->all());
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '결재 문서가 수정되었습니다.',
|
|
'data' => $approval,
|
|
]);
|
|
} catch (\InvalidArgumentException $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => $e->getMessage(),
|
|
], 400);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 삭제
|
|
*/
|
|
public function destroy(int $id): JsonResponse
|
|
{
|
|
try {
|
|
$this->service->deleteApproval($id);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '결재 문서가 삭제되었습니다.',
|
|
]);
|
|
} catch (\InvalidArgumentException $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => $e->getMessage(),
|
|
], 400);
|
|
}
|
|
}
|
|
|
|
// =========================================================================
|
|
// 워크플로우
|
|
// =========================================================================
|
|
|
|
/**
|
|
* 상신
|
|
*/
|
|
public function submit(int $id): JsonResponse
|
|
{
|
|
try {
|
|
$approval = $this->service->submit($id);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '결재가 상신되었습니다.',
|
|
'data' => $approval,
|
|
]);
|
|
} catch (\InvalidArgumentException $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => $e->getMessage(),
|
|
], 400);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 승인
|
|
*/
|
|
public function approve(Request $request, int $id): JsonResponse
|
|
{
|
|
try {
|
|
$approval = $this->service->approve($id, $request->get('comment'));
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '승인되었습니다.',
|
|
'data' => $approval,
|
|
]);
|
|
} catch (\InvalidArgumentException $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => $e->getMessage(),
|
|
], 400);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 반려
|
|
*/
|
|
public function reject(Request $request, int $id): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'comment' => 'required|string|max:1000',
|
|
]);
|
|
|
|
try {
|
|
$approval = $this->service->reject($id, $request->get('comment'));
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '반려되었습니다.',
|
|
'data' => $approval,
|
|
]);
|
|
} catch (\InvalidArgumentException $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => $e->getMessage(),
|
|
], 400);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 회수
|
|
*/
|
|
public function cancel(int $id): JsonResponse
|
|
{
|
|
try {
|
|
$approval = $this->service->cancel($id);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => '결재가 회수되었습니다.',
|
|
'data' => $approval,
|
|
]);
|
|
} catch (\InvalidArgumentException $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => $e->getMessage(),
|
|
], 400);
|
|
}
|
|
}
|
|
|
|
// =========================================================================
|
|
// 유틸
|
|
// =========================================================================
|
|
|
|
/**
|
|
* 결재선 템플릿 목록
|
|
*/
|
|
public function lines(): JsonResponse
|
|
{
|
|
$lines = $this->service->getApprovalLines();
|
|
|
|
return response()->json(['success' => true, 'data' => $lines]);
|
|
}
|
|
|
|
/**
|
|
* 양식 목록
|
|
*/
|
|
public function forms(): JsonResponse
|
|
{
|
|
$forms = $this->service->getApprovalForms();
|
|
|
|
return response()->json(['success' => true, 'data' => $forms]);
|
|
}
|
|
|
|
/**
|
|
* 미처리 건수
|
|
*/
|
|
public function badgeCounts(): JsonResponse
|
|
{
|
|
$counts = $this->service->getBadgeCounts(auth()->id());
|
|
|
|
return response()->json(['success' => true, 'data' => $counts]);
|
|
}
|
|
}
|