Files
sam-api/app/Services/Design/ModelVersionService.php
hskwon 785e367472 feat: 통합 감사 로그 도입 및 조회 API/스케줄러 추가
- DB: 감사 로그 테이블(audit_logs) 마이그레이션 및 인덱스 추가
- Config: audit.php 추가(AUDIT_RETENTION_DAYS, AUDIT_LOG_READS 토글)
- Model/Service: AuditLog 모델, AuditLogger 서비스 생성
- 도메인 훅: ModelVersion.release(released), BomTemplate upsert/update/delete/replaceItems/clone 기록, diff 조회는 설정 기반 기록
- API: GET /api/v1/design/audit-logs 추가(FormRequest/Service/Controller, 필터 page/size/target_type/target_id/action/actor_id/from/to/sort/order)
- Swagger: 감사 로그 조회 문서 추가(Design Audit 태그)
- Console: audit:prune 커맨드 추가 및 스케줄러 매일 03:10 실행 등록(시스템 크론 schedule:run 필요)
- Fix: PruneAuditLogs import 충돌 제거(Google ServiceControl AuditLog 제거)
2025-09-11 14:39:55 +09:00

120 lines
3.6 KiB
PHP

<?php
namespace App\Services\Design;
use App\Models\Design\DesignModel;
use App\Models\Design\ModelVersion;
use App\Services\Service;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ModelVersionService extends Service
{
/** 특정 모델의 버전 목록 */
public function listByModel(int $modelId)
{
$tenantId = $this->tenantId();
$model = DesignModel::query()
->where('tenant_id', $tenantId)
->find($modelId);
if (!$model) {
throw new NotFoundHttpException(__('error.not_found'));
}
return ModelVersion::query()
->where('tenant_id', $tenantId)
->where('model_id', $modelId)
->orderBy('version_no')
->get();
}
/** DRAFT 생성 (version_no 자동/수동) */
public function createDraft(int $modelId, array $extra = []): ModelVersion
{
$tenantId = $this->tenantId();
$model = DesignModel::query()
->where('tenant_id', $tenantId)
->find($modelId);
if (!$model) {
throw new NotFoundHttpException(__('error.not_found'));
}
return DB::transaction(function () use ($tenantId, $model, $extra) {
$versionNo = $extra['version_no'] ?? null;
if ($versionNo === null) {
$max = ModelVersion::query()
->where('model_id', $model->id)
->max('version_no');
$versionNo = (int)($max ?? 0) + 1;
} else {
$exists = ModelVersion::query()
->where('model_id', $model->id)
->where('version_no', $versionNo)
->exists();
if ($exists) {
throw ValidationException::withMessages(['version_no' => __('error.duplicate')]);
}
}
return ModelVersion::create([
'tenant_id' => $tenantId,
'model_id' => $model->id,
'version_no' => $versionNo,
'status' => 'DRAFT',
'is_active' => true,
'notes' => $extra['notes'] ?? null,
'effective_from' => $extra['effective_from'] ?? null,
'effective_to' => $extra['effective_to'] ?? null,
]);
});
}
/** RELEASED 전환 */
public function release(int $versionId): ModelVersion
{
$tenantId = $this->tenantId();
$mv = ModelVersion::query()
->where('tenant_id', $tenantId)
->find($versionId);
if (!$mv) {
throw new NotFoundHttpException(__('error.not_found'));
}
if ($mv->status === 'RELEASED') {
return $mv; // 멱등
}
// 릴리즈 전 유효성 검사
app(\App\Services\Design\BomTemplateService::class)->validateForRelease($mv->id);
$before = [
'status' => $mv->status,
'effective_from' => $mv->effective_from ? $mv->effective_from->toISOString() : null,
];
$mv->status = 'RELEASED';
$mv->effective_from = $mv->effective_from ?? now();
$mv->save();
// 감사 로그
app(\App\Services\Audit\AuditLogger::class)->log(
tenantId: $tenantId,
targetType: 'model_version',
targetId: $mv->id,
action: 'released',
before: $before,
after: ['status' => $mv->status, 'effective_from' => $mv->effective_from->toISOString()]
);
return $mv;
}
}