refactor(api) : remove TypeMachine skeleton system, fix ModelType serialization
- Remove TypeMachine, TypeMachineComponentRequirement, TypeMachinePieceRequirement, TypeMachineProductRequirement entities and related repositories/state processor - Replace MachineSkeletonController with MachineStructureController - Link CustomField directly to Machine instead of TypeMachine - Add migration to drop TypeMachine tables and migrate custom fields to machines - Fix ModelType serialization: Annotation\Groups → Attribute\Groups (Symfony 8 compat) and add product:read, composant:read, piece:read groups for embedded category display - Fix Profile: same Annotation → Attribute import - Fix SearchFilter: partial → ipartial on Comment and Document - Update frontend submodule (remove skeleton pages/components, simplify machine creation) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,211 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Entity\CustomField;
|
||||
use App\Entity\ModelType;
|
||||
use App\Entity\TypeMachine;
|
||||
use App\Entity\TypeMachineComponentRequirement;
|
||||
use App\Entity\TypeMachinePieceRequirement;
|
||||
use App\Entity\TypeMachineProductRequirement;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
use function array_key_exists;
|
||||
use function is_string;
|
||||
|
||||
final class TypeMachinePutProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly RequestStack $requestStack,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $uriVariables
|
||||
* @param array<string, mixed> $context
|
||||
*/
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): TypeMachine
|
||||
{
|
||||
$typeMachine = $this->em->getRepository(TypeMachine::class)->find($uriVariables['id']);
|
||||
|
||||
if (!$typeMachine) {
|
||||
throw new NotFoundHttpException('Type de machine non trouvé.');
|
||||
}
|
||||
|
||||
// Guard: cannot edit if machines are linked
|
||||
if (!$typeMachine->getMachines()->isEmpty()) {
|
||||
throw new HttpException(422, 'Ce type de machine ne peut pas être modifié car des machines y sont rattachées.');
|
||||
}
|
||||
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
$payload = json_decode($request->getContent(), true) ?? [];
|
||||
|
||||
$this->updateScalarProperties($typeMachine, $payload);
|
||||
|
||||
if (array_key_exists('customFields', $payload)) {
|
||||
$this->replaceCustomFields($typeMachine, $payload['customFields'] ?? []);
|
||||
}
|
||||
|
||||
if (array_key_exists('componentRequirements', $payload)) {
|
||||
$this->replaceComponentRequirements($typeMachine, $payload['componentRequirements'] ?? []);
|
||||
}
|
||||
|
||||
if (array_key_exists('pieceRequirements', $payload)) {
|
||||
$this->replacePieceRequirements($typeMachine, $payload['pieceRequirements'] ?? []);
|
||||
}
|
||||
|
||||
if (array_key_exists('productRequirements', $payload)) {
|
||||
$this->replaceProductRequirements($typeMachine, $payload['productRequirements'] ?? []);
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return $typeMachine;
|
||||
}
|
||||
|
||||
private function updateScalarProperties(TypeMachine $typeMachine, array $payload): void
|
||||
{
|
||||
if (isset($payload['name'])) {
|
||||
$typeMachine->setName($payload['name']);
|
||||
}
|
||||
|
||||
if (array_key_exists('description', $payload)) {
|
||||
$typeMachine->setDescription($payload['description']);
|
||||
}
|
||||
|
||||
if (array_key_exists('category', $payload)) {
|
||||
$typeMachine->setCategory($payload['category']);
|
||||
}
|
||||
|
||||
if (array_key_exists('maintenanceFrequency', $payload)) {
|
||||
$typeMachine->setMaintenanceFrequency($payload['maintenanceFrequency']);
|
||||
}
|
||||
|
||||
if (array_key_exists('components', $payload)) {
|
||||
$typeMachine->setComponents($payload['components']);
|
||||
}
|
||||
|
||||
if (array_key_exists('criticalParts', $payload)) {
|
||||
$typeMachine->setCriticalParts($payload['criticalParts']);
|
||||
}
|
||||
|
||||
if (array_key_exists('machinePieces', $payload)) {
|
||||
$typeMachine->setMachinePieces($payload['machinePieces']);
|
||||
}
|
||||
|
||||
if (array_key_exists('specifications', $payload)) {
|
||||
$typeMachine->setSpecifications($payload['specifications']);
|
||||
}
|
||||
}
|
||||
|
||||
private function replaceCustomFields(TypeMachine $typeMachine, array $fieldsData): void
|
||||
{
|
||||
foreach ($typeMachine->getCustomFields()->toArray() as $old) {
|
||||
$typeMachine->removeCustomField($old);
|
||||
}
|
||||
|
||||
foreach ($fieldsData as $index => $data) {
|
||||
$field = new CustomField();
|
||||
$field->setName($data['name'] ?? '');
|
||||
$field->setType($data['type'] ?? 'text');
|
||||
$field->setRequired($data['required'] ?? false);
|
||||
$field->setOptions($data['options'] ?? null);
|
||||
$field->setOrderIndex($data['orderIndex'] ?? $index);
|
||||
$typeMachine->addCustomField($field);
|
||||
}
|
||||
}
|
||||
|
||||
private function replaceComponentRequirements(TypeMachine $typeMachine, array $requirementsData): void
|
||||
{
|
||||
foreach ($typeMachine->getComponentRequirements()->toArray() as $old) {
|
||||
$typeMachine->removeComponentRequirement($old);
|
||||
}
|
||||
|
||||
foreach ($requirementsData as $index => $data) {
|
||||
$req = new TypeMachineComponentRequirement();
|
||||
$req->setLabel($data['label'] ?? null);
|
||||
$req->setMinCount($data['minCount'] ?? 1);
|
||||
$req->setMaxCount($data['maxCount'] ?? null);
|
||||
$req->setRequired($data['required'] ?? true);
|
||||
$req->setAllowNewModels($data['allowNewModels'] ?? true);
|
||||
$req->setOrderIndex($data['orderIndex'] ?? $index);
|
||||
|
||||
$modelType = $this->resolveModelType($data['typeComposant'] ?? null);
|
||||
if ($modelType) {
|
||||
$req->setTypeComposant($modelType);
|
||||
}
|
||||
|
||||
$typeMachine->addComponentRequirement($req);
|
||||
}
|
||||
}
|
||||
|
||||
private function replacePieceRequirements(TypeMachine $typeMachine, array $requirementsData): void
|
||||
{
|
||||
foreach ($typeMachine->getPieceRequirements()->toArray() as $old) {
|
||||
$typeMachine->removePieceRequirement($old);
|
||||
}
|
||||
|
||||
foreach ($requirementsData as $index => $data) {
|
||||
$req = new TypeMachinePieceRequirement();
|
||||
$req->setLabel($data['label'] ?? null);
|
||||
$req->setMinCount($data['minCount'] ?? 0);
|
||||
$req->setMaxCount($data['maxCount'] ?? null);
|
||||
$req->setRequired($data['required'] ?? false);
|
||||
$req->setAllowNewModels($data['allowNewModels'] ?? true);
|
||||
$req->setOrderIndex($data['orderIndex'] ?? $index);
|
||||
|
||||
$modelType = $this->resolveModelType($data['typePiece'] ?? null);
|
||||
if ($modelType) {
|
||||
$req->setTypePiece($modelType);
|
||||
}
|
||||
|
||||
$typeMachine->addPieceRequirement($req);
|
||||
}
|
||||
}
|
||||
|
||||
private function replaceProductRequirements(TypeMachine $typeMachine, array $requirementsData): void
|
||||
{
|
||||
foreach ($typeMachine->getProductRequirements()->toArray() as $old) {
|
||||
$typeMachine->removeProductRequirement($old);
|
||||
}
|
||||
|
||||
foreach ($requirementsData as $index => $data) {
|
||||
$req = new TypeMachineProductRequirement();
|
||||
$req->setLabel($data['label'] ?? null);
|
||||
$req->setMinCount($data['minCount'] ?? 0);
|
||||
$req->setMaxCount($data['maxCount'] ?? null);
|
||||
$req->setRequired($data['required'] ?? false);
|
||||
$req->setAllowNewModels($data['allowNewModels'] ?? true);
|
||||
$req->setOrderIndex($data['orderIndex'] ?? $index);
|
||||
|
||||
$modelType = $this->resolveModelType($data['typeProduct'] ?? null);
|
||||
if ($modelType) {
|
||||
$req->setTypeProduct($modelType);
|
||||
}
|
||||
|
||||
$typeMachine->addProductRequirement($req);
|
||||
}
|
||||
}
|
||||
|
||||
private function resolveModelType(mixed $value): ?ModelType
|
||||
{
|
||||
if (!$value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$id = $value;
|
||||
|
||||
if (is_string($value) && preg_match('#/api/model_types/(.+)$#', $value, $matches)) {
|
||||
$id = $matches[1];
|
||||
}
|
||||
|
||||
return $this->em->getReference(ModelType::class, $id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user