From a5692649a8de98f7197baae7754387b9e2424680 Mon Sep 17 00:00:00 2001 From: Gloomy Date: Wed, 23 Jul 2025 12:38:02 +0000 Subject: [PATCH] Refactor tax settings save endpoint to use DTO and validation --- .../Plugins/TaxSettingsController.php | 33 +++++++++++++++---- src/Dto/TaxSettingsDto.php | 33 +++++++++++++++++++ 2 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 src/Dto/TaxSettingsDto.php diff --git a/hesabixCore/src/Controller/Plugins/TaxSettingsController.php b/hesabixCore/src/Controller/Plugins/TaxSettingsController.php index b7fdd18b..4bd424e1 100644 --- a/hesabixCore/src/Controller/Plugins/TaxSettingsController.php +++ b/hesabixCore/src/Controller/Plugins/TaxSettingsController.php @@ -14,8 +14,9 @@ use Symfony\Component\Routing\Annotation\Route; use App\Entity\PluginTaxsettingsKey; use App\Entity\HesabdariDoc; use App\Entity\PluginTaxInvoice; +use App\Dto\TaxSettingsDto; +use Symfony\Component\Validator\Validator\ValidatorInterface; use DateTime; -use DateInterval; class TaxSettingsController extends AbstractController { @@ -51,7 +52,7 @@ class TaxSettingsController extends AbstractController } #[Route('/api/plugins/tax/settings/save', name: 'plugin_tax_settings_save', methods: ['POST'])] - public function plugin_tax_settings_save(Request $request, registryMGR $registryMGR, Access $access, Log $log, EntityManagerInterface $em): JsonResponse + public function plugin_tax_settings_save(Request $request, registryMGR $registryMGR, Access $access, Log $log, EntityManagerInterface $em, ValidatorInterface $validator): JsonResponse { $acc = $access->hasRole('plugTaxSettings'); if (!$acc) { @@ -59,6 +60,24 @@ class TaxSettingsController extends AbstractController } $params = $request->getPayload()->all(); + $dto = new TaxSettingsDto(); + $dto->taxMemoryId = $params['taxMemoryId'] ?? ''; + $dto->economicCode = $params['economicCode'] ?? ''; + $dto->privateKey = $params['privateKey'] ?? ''; + + $errors = $validator->validate($dto); + if (count($errors) > 0) { + $messages = []; + foreach ($errors as $error) { + $messages[$error->getPropertyPath()] = $error->getMessage(); + } + return $this->json([ + 'success' => false, + 'errors' => $messages, + 'message' => 'اطلاعات وارد شده معتبر نیست.' + ], 422); + } + $businessId = is_object($acc['bid']) ? $acc['bid']->getId() : $acc['bid']; $user = $this->getUser(); $userId = $user instanceof \App\Entity\User ? $user->getId() : null; @@ -69,12 +88,12 @@ class TaxSettingsController extends AbstractController $entity = new PluginTaxsettingsKey(); $entity->setBusinessId($businessId); $entity->setUserId($userId); - $entity->setCreatedAt(new DateTime()); + $entity->setCreatedAt(new \DateTime()); } - $entity->setPrivateKey($params['privateKey'] ?? ''); - $entity->setTaxMemoryId($params['taxMemoryId'] ?? null); - $entity->setEconomicCode($params['economicCode'] ?? null); - $entity->setUpdatedAt(new DateTime()); + $entity->setPrivateKey($dto->privateKey); + $entity->setTaxMemoryId($dto->taxMemoryId); + $entity->setEconomicCode($dto->economicCode); + $entity->setUpdatedAt(new \DateTime()); $em->persist($entity); $em->flush(); diff --git a/src/Dto/TaxSettingsDto.php b/src/Dto/TaxSettingsDto.php new file mode 100644 index 00000000..0212b7b8 --- /dev/null +++ b/src/Dto/TaxSettingsDto.php @@ -0,0 +1,33 @@ +