diff --git a/app/Services/ChecklistTemplateService.php b/app/Services/ChecklistTemplateService.php index a0ca96e..018928f 100644 --- a/app/Services/ChecklistTemplateService.php +++ b/app/Services/ChecklistTemplateService.php @@ -93,17 +93,59 @@ public function save(int $id, array $data): array $template->refresh(); $this->auditLogger->log( + $this->tenantId(), self::AUDIT_TARGET, $template->id, 'updated', $before, - $template->toArray(), - $this->apiUserId() + $template->toArray() ); return $this->getByType($template->type); } + /** + * 항목 완료 토글 + */ + public function toggleItem(int $id, string $subItemId): array + { + $template = ChecklistTemplate::findOrFail($id); + $categories = $template->categories; + $toggled = null; + + foreach ($categories as &$category) { + if (empty($category['subItems'])) { + continue; + } + foreach ($category['subItems'] as &$subItem) { + if ($subItem['id'] === $subItemId) { + $subItem['is_completed'] = ! ($subItem['is_completed'] ?? false); + $subItem['completed_at'] = $subItem['is_completed'] ? now()->toIso8601String() : null; + $toggled = $subItem; + break 2; + } + } + unset($subItem); + } + unset($category); + + if (! $toggled) { + throw new NotFoundHttpException(__('error.not_found')); + } + + $template->update([ + 'categories' => $categories, + 'updated_by' => $this->apiUserId(), + ]); + + return [ + 'id' => $toggled['id'], + 'name' => $toggled['name'], + 'is_completed' => $toggled['is_completed'], + 'completed_at' => $toggled['completed_at'], + ]; + } + /** * 항목별 파일 목록 조회 */ @@ -152,7 +194,7 @@ public function uploadDocument(int $templateId, string $subItemId, $uploadedFile ); // 파일 저장 - Storage::disk('tenant')->put($filePath, file_get_contents($uploadedFile->getPathname())); + Storage::disk('r2')->put($filePath, file_get_contents($uploadedFile->getPathname())); // DB 레코드 생성 $file = File::create([