src/Controller/ApiPluginController.php line 413

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\AuctionAccount;
  4. use App\Entity\AuctionBid;
  5. use App\Entity\PluginEventLog;
  6. use App\Entity\PluginLotData;
  7. use App\Entity\PluginPing;
  8. use App\Entity\PluginToken;
  9. use App\Entity\PluginUser;
  10. use App\Entity\PluginUserAuctionAccountAssigment;
  11. use App\Entity\ProdAuctionBid;
  12. use App\Entity\ProdPluginEventLog;
  13. use App\Entity\ProxyAccount;
  14. use App\Exception\Plugin\BadRequestException;
  15. use App\Exception\Plugin\InvalidTokenException;
  16. use App\Repository\AccountCodeRepository;
  17. use App\Form\PluginUserType;
  18. use App\Repository\PluginTokenRepository;
  19. use App\Repository\PluginUserRepository;
  20. use App\Service\Utils\CommonUtils;
  21. use App\Service\Utils\FileUploader;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  23. use Doctrine\ORM\EntityManagerInterface;
  24. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  25. use Symfony\Component\HttpFoundation\File\UploadedFile;
  26. use Symfony\Component\HttpFoundation\JsonResponse;
  27. use Symfony\Component\HttpFoundation\Request;
  28. use Symfony\Component\HttpFoundation\Response;
  29. use Symfony\Component\Routing\Annotation\Route;
  30. use Symfony\Contracts\Translation\TranslatorInterface;
  31. /**
  32.  * @Route("/api_plugin", name="api_plugin_")
  33.  */
  34. class ApiPluginController extends AbstractController
  35. {
  36.     const STATIC_VPN_CONFIG 'CmNsaWVudApub2JpbmQKZGV2IHR1bgpyZW1vdGUtY2VydC10bHMgc2VydmVyCgpyZW1vdGUgNTEuODMuMjQ5LjU4IDExOTQgdWRwCgo8a2V5PgotLS0tLUJFR0lOIFBSSVZBVEUgS0VZLS0tLS0KTUlJRXZ3SUJBREFOQmdrcWhraUc5dzBCQVFFRkFBU0NCS2t3Z2dTbEFnRUFBb0lCQVFERFo1TmpLTWhrSkNsRgo0QThudmYwNHJNWHNVNkN3VEJiQnF4d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S 'Access-Control-Allow-Origin: *';
  38.     const ALLOWED_DOMAINS = [
  39.         'https://copart.com',
  40.         'https://www.copart.com',
  41.         ];
  42.     /**
  43.      * @var FileUploader
  44.      */
  45.     private $fileUploader;
  46.     /**
  47.      * @var TranslatorInterface
  48.      */
  49.     private $translator;
  50.     public function __construct(TranslatorInterface $translatorFileUploader $fileUploader)
  51.     {
  52.         $this->translator $translator;
  53.         $this->fileUploader $fileUploader;
  54.     }
  55.     /**
  56.      * @Route("/headers_sh", name="headers_show", methods={"GET, POST"})
  57.      * @return Response
  58.      */
  59.     public function headers_show(Request $request): Response
  60.     {
  61.         //$headers = $request->headers->all();
  62.         //dd($headers);
  63.         return new JsonResponse([], 200);
  64.     }
  65.     /**
  66.      * @Route("/user_auth", name="user_auth", methods={"POST"})
  67.      * @return Response
  68.      */
  69.     public function pluginUserAuth(PluginTokenRepository $pluginTokenRepositoryPluginUserRepository $pluginUserRepositoryRequest $request): Response
  70.     {
  71.         $proxy = [];
  72.         $parameters json_decode($request->getContent(), true);
  73.         if (empty($parameters) || empty($parameters['login']) || empty($parameters['password'])) {
  74.             header(self::HEADERS);
  75.             return new JsonResponse('Bad Request'400);
  76.         }
  77.         $pluginUser $pluginUserRepository->findOneBy(['login' => $parameters['login'],'deleted' => 0]);
  78.         $entityManager $this->getDoctrine()->getManager();
  79.         if (!$pluginUser) {
  80.             header(self::HEADERS);
  81.             return new JsonResponse('Invalid login'401);
  82.         } elseif ($parameters['password'] != $pluginUser->getPassword()) {
  83.             header(self::HEADERS);
  84.             return new JsonResponse('Invalid password'401);
  85.         }
  86.         if (!$pluginUser->getOwner()->getEnablePlugin()){
  87.             header(self::HEADERS);
  88.             return new JsonResponse('Plugin disabled'401);
  89.         }
  90. //        $onPluginTokens = $pluginTokenRepository->findBy(['plugin_user' => $pluginUser, 'status' => 'on']);
  91. //        if ($onPluginTokens) {
  92. //            foreach ($onPluginTokens as $onPluginToken) {
  93. //                $onPluginToken
  94. //                    ->setStatus('off')
  95. //                    ->setDisabledAt(new \DateTime('now'));
  96. //                $entityManager->persist($onPluginToken);
  97. //                $entityManager->flush();
  98. //            }
  99. //        }
  100.         $is_token true;
  101.         while ($is_token) {
  102.             $token substr(hash('sha256'mt_rand()), 040);
  103.             $is_token $pluginTokenRepository->findOneBy(['token' => $token]);
  104.         }
  105.         $pluginToken = new PluginToken();
  106.         $pluginToken
  107.             ->setToken($token)
  108.             ->setPluginUser($pluginUser);
  109.         $entityManager->persist($pluginToken);
  110.         $entityManager->flush();
  111.         $customer $pluginUser->getCustomer();
  112.         $owner $pluginUser->getOwner();
  113.         if ($customer) {
  114.             $id $customer->getId();
  115.             $name $customer->getUsername();
  116.         } else {
  117.             $id $owner->getId();
  118.             $name $owner->getUsername();
  119.         }
  120.         if(isset($parameters['auction']) && $parameters['auction'] == 'manheim') {
  121.             $AuctionAccounts $pluginToken->getPluginUser()->getOnlyManheimAccounts();
  122.             $count count($AuctionAccounts);
  123.             if ($count 1) {
  124.                 header(self::HEADERS);
  125.                 return new JsonResponse('Invalid Manheim accounts'401);
  126.             }
  127.             $em $this->getDoctrine()->getManager();
  128.             $proxyAccount $em->getRepository(ProxyAccount::class)->findLastUsedManheimProxy();
  129.             // $key = random_int(0, $count - 1);
  130.             // $auctionAccount = $AuctionAccounts[$key];
  131. //        $login = $auctionAccount->getLogin();
  132. //        $password = $auctionAccount->getPassword();
  133.             // $proxyAccount = $auctionAccount->getProxyAccount();
  134.             if ($proxyAccount) {
  135.                 $proxy = [
  136.                     'proxy' => [
  137.                         'host' => $proxyAccount->getProxyHost() ?? '',
  138.                         'port' => $proxyAccount->getProxyPort() ?? '',
  139.                         'username' => $proxyAccount->getProxyUsername() ?? '',
  140.                         'password' => $proxyAccount->getProxyPassword() ?? '',
  141.                     ]
  142.                 ];
  143.                 $proxyAccount->setLastUsedAt(new \DateTime('now'));
  144.                 $em->persist($proxyAccount);
  145.                 $em->flush();
  146.             }
  147.         }
  148.         $data = [
  149.             'id' => $pluginUser->getId(),
  150.             'name' => $name,
  151.             'token' => $token,
  152.             'allowLogging' => true,
  153.         ];
  154.         if(!empty($proxy)) {
  155.             $data array_merge($data$proxy);
  156.         }
  157.         header(self::HEADERS);
  158.         return new JsonResponse($data200);
  159.     }
  160.     /**
  161.      * @param EntityManagerInterface $em
  162.      * @param Request $request
  163.      * @return PluginToken
  164.      * @throws BadRequestException
  165.      * @throws InvalidTokenException
  166.      */
  167.     private function _authUserByToken(EntityManagerInterface $emRequest $request): PluginToken
  168.     {
  169.         $pluginTokenRepository $em->getRepository(PluginToken::class);
  170.         $parameters json_decode($request->getContent(), true);
  171.         if(!empty($parameters['user'])) { // if request is from universal APP
  172.             $parameters $parameters['user'];
  173.         }
  174.         if (empty($parameters) || empty($parameters['id']) || empty($parameters['name']) || empty($parameters['token'])) {
  175.             throw new BadRequestException();
  176.         }
  177.         $pluginToken $pluginTokenRepository->findOneBy(['token' => $parameters['token'], 'status' => 'on']);
  178.         if (!$pluginToken instanceof PluginToken) {
  179.             throw new InvalidTokenException();
  180.         }
  181.         if(!$pluginToken->getPluginUser()->getOwner()->getEnablePlugin()){
  182.             throw new InvalidTokenException();
  183.         }
  184.         return $pluginToken;
  185.     }
  186.     /**
  187.      * @Route("/get_copart_account", name="get_copart_account", methods={"POST"})
  188.      * @return Response
  189.      */
  190.     public function getCopartAccount(Request $requestEntityManagerInterface $em): Response
  191.     {
  192.         try {
  193.             $pluginToken $this->_authUserByToken($em$request);
  194.         } catch (BadRequestException $e) {
  195.             header(self::HEADERS);
  196.             return new JsonResponse('Bad Request'400);
  197.         } catch (InvalidTokenException $e) {
  198.             header(self::HEADERS);
  199.             return new JsonResponse('Invalid token'401);
  200.         }
  201.         $parameters json_decode($request->getContent(), true);
  202.         $plugin_version $parameters['pv'] ?? '';
  203.         if (empty($plugin_version)) {
  204.             header(self::HEADERS);
  205.             return new JsonResponse('Invalid pv'400);
  206.         }
  207.         $ip CommonUtils::getClientIP($request);
  208.         $pluginUser $pluginToken->getPluginUser();
  209.         $AuctionAccounts $pluginUser->getOnlyCopartAccounts();
  210.         if (count($AuctionAccounts) < 1) {
  211.             header(self::HEADERS);
  212.             return new JsonResponse('Invalid Copart accounts'401);
  213.         }
  214.         $accountFinancesAccesses $pluginUser->getAccountsFinancesAccessMap();
  215.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  216.         $auctionAccount $assigmentRepository->selectAuctionAccountForAssigment($AuctionAccounts$accountFinancesAccesses);
  217.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment($auctionAccount$pluginToken$plugin_version$ip);
  218.         $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  219. //        $login = $auctionAccount->getLogin();
  220. //        $password = $auctionAccount->getPassword();
  221.         $proxyAccount $auctionAccount->getProxyAccount();
  222.         $proxy = [];
  223.         if ($proxyAccount) {
  224.             $proxy = [
  225.                 'proxy' => [
  226.                     'host' => $proxyAccount->getProxyHost() ?? '',
  227.                     'port' => $proxyAccount->getProxyPort() ?? '',
  228.                     'username' => $proxyAccount->getProxyUsername() ?? '',
  229.                     'password' => $proxyAccount->getProxyPassword() ?? '',
  230.                 ]
  231.             ];
  232.         }
  233.         $data = [
  234.             'copart' => [
  235.                 'login' => $pluginAuctionAssignment->getAuctionAccount()->getLogin() ?? '',
  236.                 'password' => $pluginAuctionAssignment->getAuctionAccount()->getPassword() ?? '',
  237.             ],
  238.             'assigment_id' => $pluginAuctionAssignment->getId(),
  239.             'can_manage_payments' => $canManagePayments,
  240.         ];
  241.         header(self::HEADERS);
  242.         return new JsonResponse(array_merge($data$proxy), 200);
  243.     }
  244.     /**
  245.      * @Route("/bid_clicked", name="bid_clicked", methods={"POST"})
  246.      * @param PluginTokenRepository $pluginTokenRepository
  247.      * @param Request $request
  248.      * @param EntityManagerInterface $em
  249.      * @return Response
  250.      */
  251.     public function bidClicked(Request $requestEntityManagerInterface $em): Response
  252.     {
  253.         try {
  254.             $pluginToken $this->_authUserByToken($em$request);
  255.         } catch (BadRequestException $e) {
  256.             header(self::HEADERS);
  257.             return new JsonResponse('Bad Request'400);
  258.         } catch (InvalidTokenException $e) {
  259.             header(self::HEADERS);
  260.             return new JsonResponse('Invalid token'401);
  261.         }
  262.         $parameters json_decode($request->getContent(), true);
  263.         $lot $parameters['lot'] ?? '';
  264.         $bid $parameters['bid'] ?? false;
  265.         $bid_type strtolower($parameters['bid_type']) ?? '';
  266.         $session_url $parameters['session_url'] ?? '';
  267.         $window_host $parameters['window_host'] ?? '';
  268.         $loggedCopart =
  269.             $parameters['loggedCopart'] ??
  270.             $parameters['loggedIaai'] ??
  271.             $parameters['loggedManheim'] ??
  272.             $parameters['loggedBringatrailer'] ??
  273.             $parameters['loggedCars_and_bids'] ??
  274.             $parameters['loggedCars'] ??
  275.             $parameters['loggedCargurus'] ?? false;
  276.         if (empty($lot)) {
  277.             header(self::HEADERS);
  278.             return new JsonResponse('Invalid lot'400);
  279.         }
  280.         if ($bid === false) {
  281.             header(self::HEADERS);
  282.             return new JsonResponse('Invalid bid'400);
  283.         }
  284.         if(empty($bid_type) || !in_array($bid_typeAuctionBid::TYPES)){
  285.             header(self::HEADERS);
  286.             return new JsonResponse('Invalid bid_type'400);
  287.         }
  288.         if (empty($parameters['assigment_id'])) {
  289.             header(self::HEADERS);
  290.             return new JsonResponse('Invalid assigment'406);
  291.         }
  292.         $assignedAuctionAccount $em->getRepository(PluginUserAuctionAccountAssigment::class)->find((int)$parameters['assigment_id']);
  293.         if (!$assignedAuctionAccount instanceof PluginUserAuctionAccountAssigment) {
  294.             header(self::HEADERS);
  295.             return new JsonResponse('Invalid assigment'406);
  296.         }
  297.         /******* check iaai country *********/
  298.         /*$auctionAccount = $assignedAuctionAccount->getAuctionAccount();
  299.         if (strtolower($auctionAccount->getAuctionName()) === 'iaai') {
  300.             if (!empty($window_host)) {
  301.                 if (($window_host === 'www.iaai.com') && ($auctionAccount->getCountry() === 'usa')) {
  302.                     true;
  303.                 } elseif (($window_host === 'ca.iaai.com') && ($auctionAccount->getCountry() === 'canada')) {
  304.                     true;
  305.                 } else {
  306.                     header(self::HEADERS);
  307.                     return new JsonResponse('Invalid country in window_host', 406);
  308.                 }
  309.             } else {
  310.                 header(self::HEADERS);
  311.                 return new JsonResponse('Invalid window_host', 406);
  312.             }
  313.         }*/
  314.         /******* end *********/
  315.         $auctionBid = (new AuctionBid())
  316.             ->setCreatedAt(new \DateTime('now'))
  317.             ->setAssignation($assignedAuctionAccount)
  318.             ->setBid((float)$bid)
  319.             ->setLot($lot)
  320.             ->setLoggedCopart((bool)$loggedCopart)
  321.             ->setType($bid_type)
  322.             ->setSessionUrl($session_url);
  323.         $em->persist($auctionBid);
  324.         $em->flush();
  325.         header(self::HEADERS);
  326.         return new JsonResponse(['history_id' => $auctionBid->getId()], 200);
  327.     }
  328.     /**
  329.      * @Route("/get_assigment_finances_access", name="assigment_finances_access", methods={"POST"})
  330.      * @param Request $request
  331.      * @param EntityManagerInterface $em
  332.      * @return Response
  333.      */
  334.     public function getAssigmentFinancesAccess(Request $requestEntityManagerInterface $em): Response
  335.     {
  336.         try {
  337.             $pluginToken $this->_authUserByToken($em$request);
  338.         } catch (BadRequestException $e) {
  339.             header(self::HEADERS);
  340.             return new JsonResponse('Bad Request'400);
  341.         } catch (InvalidTokenException $e) {
  342.             header(self::HEADERS);
  343.             return new JsonResponse('Invalid token'401);
  344.         }
  345.         $parameters json_decode($request->getContent(), true);
  346.         $assigmentId $parameters['assigment_id'] ?? null;
  347.         if (empty($assigmentId)) {
  348.             header(self::HEADERS);
  349.             return new JsonResponse('Invalid assigment'406);
  350.         }
  351.         $pluginUser $pluginToken->getPluginUser();
  352.         $assignedAuctionAccount $em->getRepository(PluginUserAuctionAccountAssigment::class)->find((int)$parameters['assigment_id']);
  353.         if (!$assignedAuctionAccount instanceof PluginUserAuctionAccountAssigment
  354.             || $assignedAuctionAccount->getPluginUser()->getId() !== $pluginUser->getId()
  355.         ) {
  356.             header(self::HEADERS);
  357.             return new JsonResponse('Invalid assigment'406);
  358.         }
  359.         $accountFinancesAccesses $pluginUser->getAccountsFinancesAccessMap();
  360.         $canManageFinances $accountFinancesAccesses[$assignedAuctionAccount->getAuctionAccount()->getId()] ?? false;
  361.         header(self::HEADERS);
  362.         return new JsonResponse(['can_manage_finances' => $canManageFinances], 200);
  363.     }
  364.     /**
  365.      * @Route("/ping", name="ping", methods={"POST"})
  366.      * @param Request $request
  367.      * @param EntityManagerInterface $em
  368.      * @return Response
  369.      */
  370.     public function ping(Request $requestEntityManagerInterface $em): Response
  371.     {
  372.         try {
  373.             $pluginToken $this->_authUserByToken($em$request);
  374.         } catch (BadRequestException $e) {
  375.             header(self::HEADERS);
  376.             return new JsonResponse('Bad Request'400);
  377.         } catch (InvalidTokenException $e) {
  378.             header(self::HEADERS);
  379.             return new JsonResponse('Invalid token'401);
  380.         }
  381.         $parameters json_decode($request->getContent(), true);
  382.         $assigment_id $parameters['assigment_id'] ?? '';
  383.         $token $parameters['token'] ?? '';
  384.         $loggedCopart =
  385.             $parameters['loggedCopart'] ??
  386.             $parameters['loggedIaai'] ??
  387.             $parameters['loggedManheim'] ??
  388.             $parameters['loggedBringatrailer'] ??
  389.             $parameters['loggedCars_and_bids'] ??
  390.             $parameters['loggedCars'] ??
  391.             $parameters['loggedCargurus'] ?? false;
  392.         $loggedPlugin $parameters['loggedPlugin'] ?? '';
  393. //        $date = $parameters['date'] ?? '';
  394.         if (empty($assigment_id)) {
  395.             header(self::HEADERS);
  396.             return new JsonResponse('Empty assigment_id'400);
  397.         }
  398.         if (empty($token)) {
  399.             header(self::HEADERS);
  400.             return new JsonResponse('Empty token'406);
  401.         }
  402. //        if (empty($loggedCopart)) {
  403. //            return new JsonResponse('Empty loggedCopart', 406);
  404. //        }
  405. //        if (empty($loggedPlugin)) {
  406. //            return new JsonResponse('Empty loggedPlugin', 406);
  407. //        }
  408. //        if (empty($date)) {
  409. //            return new JsonResponse('Empty date', 406);
  410. //        }
  411.         $assignedAuctionAccount $em->getRepository(PluginUserAuctionAccountAssigment::class)->find((int)$assigment_id);
  412.         if (!$assignedAuctionAccount instanceof PluginUserAuctionAccountAssigment) {
  413.             header(self::HEADERS);
  414.             return new JsonResponse('Invalid assigment'406);
  415.         }
  416.         if($pluginToken->getToken() != $token){
  417.             header(self::HEADERS);
  418.             return new JsonResponse('Invalid token'406);
  419.         }
  420.         $pluginPing = (new PluginPing())
  421.             ->setAssigment($assignedAuctionAccount)
  422.             ->setToken($pluginToken)
  423.             ->setLoggedCopart((bool)$loggedCopart)
  424.             ->setLoggedPlugin((bool)$loggedPlugin)
  425.             ->setDate( new \DateTime('now'));
  426.         $em->persist($pluginPing);
  427.         $em->flush();
  428.         header(self::HEADERS);
  429.         return new JsonResponse(['status' => 'Ok'], 200);
  430.     }
  431.     /**
  432.      * @Route("/add_event_log", name="add_event_log", methods={"POST"})
  433.      * @param Request $request
  434.      * @param EntityManagerInterface $em
  435.      * @return Response
  436.      */
  437.     public function addEventLog(Request $requestEntityManagerInterface $em): Response
  438.     {
  439.         $parameters json_decode($request->getContent(), true);
  440.         if (empty($parameters['event'])) {
  441.             header(self::HEADERS);
  442.             return new JsonResponse('Empty event'400);
  443.         }
  444.         if (empty($parameters['eventData']) || !is_array($parameters['eventData'])) {
  445.             header(self::HEADERS);
  446.             return new JsonResponse('Bad data'400);
  447.         }
  448.         $ip CommonUtils::getClientIP($request);
  449.         $pluginEventLog = (new PluginEventLog())
  450.             ->setIp($ip)
  451.             ->setCreatedAt(new \DateTime('now'))
  452.             ->setName($parameters['event'])
  453.             ->setData($parameters['eventData']);
  454.         if( !empty($parameters['eventData']['origin']) && in_array($parameters['eventData']['origin'], PluginEventLog::ORIGINS) ){
  455.             $pluginEventLog->setOrigin($parameters['eventData']['origin']);
  456.         }
  457.         $em->persist($pluginEventLog);
  458.         $em->flush();
  459.         header(self::HEADERS);
  460.         return new JsonResponse(['status' => 'Ok'], 200);
  461.     }
  462.     /**
  463.      * @Route("/add_lot_data", name="add_lot_data", methods={"POST"})
  464.      * @param Request $request
  465.      * @param EntityManagerInterface $em
  466.      * @return Response
  467.      */
  468.     public function addLotData(Request $requestEntityManagerInterface $em): Response
  469.     {
  470.         $parameters json_decode($request->getContent(), true);
  471.         if (empty($parameters['auction'])) {
  472.             header(self::HEADERS);
  473.             return new JsonResponse('Empty auction'400);
  474.         }
  475.         if (empty($parameters['lot'])) {
  476.             header(self::HEADERS);
  477.             return new JsonResponse('Empty lot'400);
  478.         }
  479.         if (empty($parameters['vin']) || str_contains('stars_'$parameters['vin'])) {
  480.             header(self::HEADERS);
  481.             return new JsonResponse('Wrong vin'400);
  482.         }
  483.         if (!isset($parameters['data']) || empty($parameters['data']) || !is_array($parameters['data']) || !isset($parameters['data']['lotDetails'])) {
  484.             header(self::HEADERS);
  485.             return new JsonResponse('Bad data'400);
  486.         }
  487.         $ip CommonUtils::getClientIP($request);
  488.         $pluginEventLog = (new PluginLotData())
  489.             ->setIp($ip)
  490.             ->setCreatedAt(new \DateTimeImmutable('now'))
  491.             ->setLot($parameters['lot'])
  492.             ->setVin($parameters['vin'])
  493.             ->setPv($parameters['pv'])
  494.             ->setUserData($parameters['user_data'])
  495.             ->setData($parameters['data']);
  496.         if( !empty($parameters['auction']) && in_array($parameters['auction'], PluginLotData::AUCTIONS) ){
  497.             $pluginEventLog->setAuction($parameters['auction']);
  498.         }
  499.         $em->persist($pluginEventLog);
  500.         $em->flush();
  501.         header(self::HEADERS);
  502.         return new JsonResponse(['status' => 'Ok'], 200);
  503.     }
  504.     /**
  505.      * @Route("/get_auction", name="get_auction", methods={"POST"})
  506.      * @param Request $request
  507.      * @param EntityManagerInterface $em
  508.      * @return Response
  509.      */
  510.     public function get_auction(Request $requestEntityManagerInterface $em): Response
  511.     {
  512.         $request_body $request->toArray();
  513.         $plugin_version $request_body['pv'] ?? '';
  514.         $auction_name strtolower(trim($request_body['auction_name'])) ?? '';
  515.         $ip CommonUtils::getClientIP($request);
  516.         if(empty($auction_name)){
  517.             header(self::HEADERS);
  518.             return new JsonResponse('Invalid Request'400);
  519.         }
  520.         if (empty($plugin_version)) {
  521.             header(self::HEADERS);
  522.             return new JsonResponse('Invalid pv'400);
  523.         }
  524.         try {
  525.             $pluginToken $this->_authUserByToken($em$request);
  526.         } catch (BadRequestException $e) {
  527.             header(self::HEADERS);
  528.             return new JsonResponse('Bad Request'400);
  529.         } catch (InvalidTokenException $e) {
  530.             header(self::HEADERS);
  531.         }
  532.         $available_accounts $pluginToken->getPluginUser()->getCopartAccounts();
  533.         $AuctionAccounts = [];
  534.         foreach($available_accounts as $account) {
  535.             if (strtolower($account->getAuctionNameWithFullCountry()) === $auction_name) {
  536.                 $AuctionAccounts[] = $account;
  537.             }
  538.         }
  539.         $count count($AuctionAccounts);
  540.         if ($count 1) {
  541.             header(self::HEADERS);
  542.             return new JsonResponse('Invalid Auction accounts'401);
  543.         }
  544.         $key random_int(0$count 1);
  545.         $auctionAccount $AuctionAccounts[$key];
  546.         $proxyAccount $auctionAccount->getProxyAccount();
  547.         $proxy = [];
  548.         if ($proxyAccount) {
  549.             $proxy = [
  550.                     'host' => $proxyAccount->getProxyHost() ?? '',
  551.                     'port' => $proxyAccount->getProxyPort() ?? '',
  552.                     'username' => $proxyAccount->getProxyUsername() ?? '',
  553.                     'password' => $proxyAccount->getProxyPassword() ?? '',
  554.             ];
  555.         }
  556.         $vpn = [];
  557.         $vpn_required false;
  558.         if($auction_name == 'cars-usa') { //TODO check if auction has vpn config.
  559.             $vpn = [
  560.                 'vpn_type' => 'openvpn',
  561.                 'vpn_config' => self::STATIC_VPN_CONFIG,
  562.             ];
  563.             $vpn_required true;
  564.         }
  565.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  566.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment($auctionAccount$pluginToken$plugin_version$ip);
  567.         $data = [
  568.             'auction_credentials' => [
  569.                 'auction_name' => $auction_name,
  570.                 'login' => $pluginAuctionAssignment->getAuctionAccount()->getLogin() ?? '',
  571.                 'password' => $pluginAuctionAssignment->getAuctionAccount()->getPassword() ?? '',
  572.             ],
  573.             'assigment_id' => $pluginAuctionAssignment->getId(),
  574.             'proxy' => $proxy,
  575.             'vpn_options' => $vpn,
  576.             'vpn_required' => $vpn_required,
  577.         ];
  578.         header(self::HEADERS);
  579.         return new JsonResponse($data200);
  580.     }
  581.     /**
  582.      * @Route("/get_available_auctions", name="get_available_auctions", methods={"POST"})
  583.      * @return Response
  584.      */
  585.     public function getAvailableAuctionAccounts(Request $requestEntityManagerInterface $em): Response
  586.     {
  587.         $data = [];
  588.         $_avail_auction_names = [];
  589.         try {
  590.             $pluginToken $this->_authUserByToken($em$request);
  591.         } catch (BadRequestException $e) {
  592.             header(self::HEADERS);
  593.             return new JsonResponse('Bad Request'400);
  594.         } catch (InvalidTokenException $e) {
  595.             header(self::HEADERS);
  596.             return new JsonResponse('Invalid token'401);
  597.         }
  598.         $available_accounts $pluginToken->getPluginUser()->getCopartAccounts();
  599.         foreach($available_accounts as $account) {
  600.             if(in_array($account->getAuctionName().'-'.$account->getCountry(), $_avail_auction_names)) continue;
  601.             $_avail_auction_names[] = $account->getAuctionName().'-'.$account->getCountry();
  602.             $id $account->getId();
  603.             if (strtolower($account->getAuctionName()) === 'copart'){
  604.                 $id 2;
  605.             } elseif (strtolower($account->getAuctionName()) === 'manheim'){
  606.                 $id 11;
  607.             } elseif (strtolower($account->getAuctionName()) === 'iaai' && !is_null($account->getCountry())) {
  608.                 if (strtolower($account->getCountry()) === 'usa') {
  609.                     $id 1;
  610.                 } elseif (strtolower($account->getCountry()) === 'canada') {
  611.                     $id 12;
  612.                 }
  613.             } elseif (strtolower($account->getAuctionName()) === 'bringatrailer'){
  614.                 $id 17;
  615.             }elseif (strtolower($account->getAuctionName()) === 'cars_and_bids'){
  616.                 $id 20;
  617.             }elseif (strtolower($account->getAuctionName()) === 'cars'){
  618.                 $id 23;
  619.             }elseif (strtolower($account->getAuctionName()) === 'cargurus'){
  620.                 $id 26;
  621.             }
  622.             $data[] = [
  623.                 'id' => $id,
  624.                 'auction_name' => $account->getAuctionName(),
  625.                 'auction_country' => $account->getCountry(),
  626.                 'display_name' => ( is_null($account->getCountry()) || empty($account->getCountry()) ) ? strtoupper($account->getAuctionName()) : strtoupper($account->getAuctionName().'-'.$account->getCountry()),
  627.             ];
  628.         }
  629.         if (count($data) < 1) {
  630.             header(self::HEADERS);
  631.             return new JsonResponse('Invalid Auction accounts'401);
  632.         }
  633.         header(self::HEADERS);
  634.         return new JsonResponse($data200);
  635.     }
  636.     /**
  637.      * @Route("/get_iaai_account", name="get_iaai_account", methods={"POST"})
  638.      * @return Response
  639.      */
  640.     public function getIaaiAccount(Request $requestEntityManagerInterface $em): Response
  641.     {
  642.         try {
  643.             $pluginToken $this->_authUserByToken($em$request);
  644.         } catch (BadRequestException $e) {
  645.             header(self::HEADERS);
  646.             return new JsonResponse('Bad Request'400);
  647.         } catch (InvalidTokenException $e) {
  648.             header(self::HEADERS);
  649.             return new JsonResponse('Invalid token'401);
  650.         }
  651.         $parameters json_decode($request->getContent(), true);
  652.         $plugin_version $parameters['pv'] ?? '';
  653.         if (empty($plugin_version)) {
  654.             header(self::HEADERS);
  655.             return new JsonResponse('Invalid pv'400);
  656.         }
  657.         $ip CommonUtils::getClientIP($request);
  658.         /******* parse iaai redirect country *********/
  659.         $redirect_uri $parameters['url'] ?? 'usa';
  660.         $country 'usa';
  661.         if (!empty($redirect_uri)) {
  662.             parse_str(parse_url(urldecode($redirect_uri), PHP_URL_QUERY), $parsed_redirect_url);
  663.             if (!empty($parsed_redirect_url['redirect_uri'])) {
  664.                 if (strpos($parsed_redirect_url['redirect_uri'], 'ca.iaai.com') !== false) {
  665.                     $country 'canada';
  666.                 } elseif (strpos($parsed_redirect_url['redirect_uri'], 'www.iaai.com') !== false) {
  667.                     $country 'usa';
  668.                 }
  669.             }
  670.         }
  671.         /******* end *********/
  672.         $pluginUser $pluginToken->getPluginUser();
  673.         $AuctionAccounts = [];
  674.         foreach ($pluginUser->getOnlyIaaiAccounts() as $aa) {
  675.             if ($aa->getCountry() === $country) {
  676.                 $AuctionAccounts[] = $aa;
  677.             }
  678.         }
  679.         if (count($AuctionAccounts) < 1) {
  680.             header(self::HEADERS);
  681.             return new JsonResponse('Invalid Iaai accounts'401);
  682.         }
  683.         $accountFinancesAccesses $pluginUser->getAccountsFinancesAccessMap();
  684.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  685.         $auctionAccount $assigmentRepository->selectAuctionAccountForAssigment($AuctionAccounts$accountFinancesAccesses);
  686.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment($auctionAccount$pluginToken$plugin_version$ip);
  687.         $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  688.         $proxyAccount $auctionAccount->getProxyAccount();
  689.         $proxy = [];
  690.         if ($proxyAccount) {
  691.             $proxy = [
  692.                 'proxy' => [
  693.                     'host' => $proxyAccount->getProxyHost() ?? '',
  694.                     'port' => $proxyAccount->getProxyPort() ?? '',
  695.                     'username' => $proxyAccount->getProxyUsername() ?? '',
  696.                     'password' => $proxyAccount->getProxyPassword() ?? '',
  697.                 ]
  698.             ];
  699.         }
  700.         $data = [
  701.             'iaai' => [
  702.                 'login' => $pluginAuctionAssignment->getAuctionAccount()->getLogin() ?? '',
  703.                 'password' => $pluginAuctionAssignment->getAuctionAccount()->getPassword() ?? '',
  704.             ],
  705.             'assigment_id' => $pluginAuctionAssignment->getId(),
  706.             'can_manage_payments' => $canManagePayments,
  707.         ];
  708.         header(self::HEADERS);
  709.         return new JsonResponse(array_merge($data$proxy), 200);
  710.     }
  711.     /**
  712.      * @Route("/get_manheim_account", name="get_manheim_account", methods={"POST"})
  713.      * @return Response
  714.      */
  715.     public function getManheimAccount(Request $requestEntityManagerInterface $em): Response
  716.     {
  717.         try {
  718.             $pluginToken $this->_authUserByToken($em$request);
  719.         } catch (BadRequestException $e) {
  720.             header(self::HEADERS);
  721.             return new JsonResponse('Bad Request'400);
  722.         } catch (InvalidTokenException $e) {
  723.             header(self::HEADERS);
  724.             return new JsonResponse('Invalid token'401);
  725.         }
  726.         $parameters json_decode($request->getContent(), true);
  727.         $plugin_version $parameters['pv'] ?? '';
  728.         if (empty($plugin_version)) {
  729.             header(self::HEADERS);
  730.             return new JsonResponse('Invalid pv'400);
  731.         }
  732.         $ip CommonUtils::getClientIP($request);
  733.         $pluginUser $pluginToken->getPluginUser();
  734.         $AuctionAccounts $pluginUser->getOnlyManheimAccounts();
  735.         if (count($AuctionAccounts) < 1) {
  736.             header(self::HEADERS);
  737.             return new JsonResponse('Invalid Manheim accounts'401);
  738.         }
  739.         $accountFinancesAccesses $pluginUser->getAccountsFinancesAccessMap();
  740.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  741.         $auctionAccount $assigmentRepository->selectAuctionAccountForAssigment($AuctionAccounts$accountFinancesAccesses);
  742.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment($auctionAccount$pluginToken$plugin_version$ip);
  743.         $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  744. //        $login = $auctionAccount->getLogin();
  745. //        $password = $auctionAccount->getPassword();
  746.         // $proxyAccount = $auctionAccount->getProxyAccount();
  747.         $em $this->getDoctrine()->getManager();
  748.         $proxyAccount $em->getRepository(ProxyAccount::class)->findLastUsedManheimProxy();
  749.         $proxy = [];
  750.         if ($proxyAccount) {
  751.             $proxy = [
  752.                 'proxy' => [
  753.                     'host' => $proxyAccount->getProxyHost() ?? '',
  754.                     'port' => $proxyAccount->getProxyPort() ?? '',
  755.                     'username' => $proxyAccount->getProxyUsername() ?? '',
  756.                     'password' => $proxyAccount->getProxyPassword() ?? '',
  757.                 ]
  758.             ];
  759.             $proxyAccount->setLastUsedAt(new \DateTime('now'));
  760.             $em->persist($proxyAccount);
  761.         }
  762.         $data = [
  763.             'manheim' => [
  764.                 'login' => $pluginAuctionAssignment->getAuctionAccount()->getLogin() ?? '',
  765.                 'password' => $pluginAuctionAssignment->getAuctionAccount()->getPassword() ?? '',
  766.             ],
  767.             'assigment_id' => $pluginAuctionAssignment->getId(),
  768.             'can_manage_payments' => $canManagePayments,
  769.         ];
  770.         header(self::HEADERS);
  771.         return new JsonResponse(array_merge($data$proxy), 200);
  772.     }
  773.     /**
  774.      * @Route("/manheim_2fa_codes", name="manheim_2fa_code", methods={"POST"})
  775.      * @return Response
  776.      */
  777.     public function getManheim2FACodes(AccountCodeRepository $accountCodeRepositoryEntityManagerInterface $emRequest $request): Response
  778.     {
  779.         try {
  780.             $pluginToken $this->_authUserByToken($em$request);
  781.         } catch (BadRequestException $e) {
  782.             header(self::HEADERS);
  783.             return new JsonResponse('Bad Request'400);
  784.         } catch (InvalidTokenException $e) {
  785.             header(self::HEADERS);
  786.             return new JsonResponse('Invalid token'401);
  787.         }
  788.         $auctionAccounts $pluginToken->getPluginUser()->getOnlyManheimAccounts();
  789.         $count count($auctionAccounts);
  790.         if ($count 1) {
  791.             header(self::HEADERS);
  792.             return new JsonResponse('Invalid Manheim accounts'401);
  793.         }
  794.         $manheimCodes $accountCodeRepository->getLatestCodesByAccountType('manheim');
  795.         $resultCodes = [];
  796.         foreach ($manheimCodes as $code) {
  797.             $resultCodes[] = [
  798.                 '2fa_code' => $code->getCode(),
  799.                 'received_at' => $code->getReceivedAt()->format('c'), // ISO 8601 format,
  800.             ];
  801.         }
  802.         $data = [
  803.             'manheim_codes' => $resultCodes
  804.         ];
  805.         header(self::HEADERS);
  806.         return new JsonResponse($data200);
  807.     }
  808.     /**
  809.      * @Route("/get_bringatrailer_account", name="get_bringatrailer_account", methods={"POST"})
  810.      * @return Response
  811.      */
  812.     public function getBringatrailerAccount(Request $requestEntityManagerInterface $em): Response
  813.     {
  814.         try {
  815.             $pluginToken $this->_authUserByToken($em$request);
  816.         } catch (BadRequestException $e) {
  817.             header(self::HEADERS);
  818.             return new JsonResponse('Bad Request'400);
  819.         } catch (InvalidTokenException $e) {
  820.             header(self::HEADERS);
  821.             return new JsonResponse('Invalid token'401);
  822.         }
  823.         $parameters json_decode($request->getContent(), true);
  824.         $plugin_version $parameters['pv'] ?? '';
  825.         if (empty($plugin_version)) {
  826.             header(self::HEADERS);
  827.             return new JsonResponse('Invalid pv'400);
  828.         }
  829.         $ip CommonUtils::getClientIP($request);
  830.         $pluginUser $pluginToken->getPluginUser();
  831.         $AuctionAccounts $pluginUser->getOnlyBringatrailerAccounts();
  832.         if (count($AuctionAccounts) < 1) {
  833.             header(self::HEADERS);
  834.             return new JsonResponse('Invalid Bringatrailer accounts'401);
  835.         }
  836.         $accountFinancesAccesses $pluginUser->getAccountsFinancesAccessMap();
  837.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  838.         $auctionAccount $assigmentRepository->selectAuctionAccountForAssigment($AuctionAccounts$accountFinancesAccesses);
  839.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment($auctionAccount$pluginToken$plugin_version$ip);
  840.         $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  841. //        $login = $auctionAccount->getLogin();
  842. //        $password = $auctionAccount->getPassword();
  843.         $proxyAccount $auctionAccount->getProxyAccount();
  844.         $proxy = [];
  845.         if ($proxyAccount) {
  846.             $proxy = [
  847.                 'proxy' => [
  848.                     'host' => $proxyAccount->getProxyHost() ?? '',
  849.                     'port' => $proxyAccount->getProxyPort() ?? '',
  850.                     'username' => $proxyAccount->getProxyUsername() ?? '',
  851.                     'password' => $proxyAccount->getProxyPassword() ?? '',
  852.                 ]
  853.             ];
  854.         }
  855.         $data = [
  856.             'bringatrailer' => [
  857.                 'login' => $pluginAuctionAssignment->getAuctionAccount()->getLogin() ?? '',
  858.                 'password' => $pluginAuctionAssignment->getAuctionAccount()->getPassword() ?? '',
  859.             ],
  860.             'assigment_id' => $pluginAuctionAssignment->getId(),
  861.             'can_manage_payments' => $canManagePayments,
  862.         ];
  863.         header(self::HEADERS);
  864.         return new JsonResponse(array_merge($data$proxy), 200);
  865.     }
  866.     /**
  867.      * @Route("/get_cars_and_bids_account", name="get_cars_and_bids_account", methods={"POST"})
  868.      * @return Response
  869.      */
  870.     public function getCarsAndBidsAccount(Request $requestEntityManagerInterface $em): Response
  871.     {
  872.         try {
  873.             $pluginToken $this->_authUserByToken($em$request);
  874.         } catch (BadRequestException $e) {
  875.             header(self::HEADERS);
  876.             return new JsonResponse('Bad Request'400);
  877.         } catch (InvalidTokenException $e) {
  878.             header(self::HEADERS);
  879.             return new JsonResponse('Invalid token'401);
  880.         }
  881.         $parameters json_decode($request->getContent(), true);
  882.         $plugin_version $parameters['pv'] ?? '';
  883.         if (empty($plugin_version)) {
  884.             header(self::HEADERS);
  885.             return new JsonResponse('Invalid pv'400);
  886.         }
  887.         $ip CommonUtils::getClientIP($request);
  888.         $pluginUser $pluginToken->getPluginUser();
  889.         $AuctionAccounts $pluginUser->getOnlyCarsAndBidsAccounts();
  890.         if (count($AuctionAccounts) < 1) {
  891.             header(self::HEADERS);
  892.             return new JsonResponse('Invalid Cars_and_bids accounts'401);
  893.         }
  894.         $accountFinancesAccesses $pluginUser->getAccountsFinancesAccessMap();
  895.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  896.         $auctionAccount $assigmentRepository->selectAuctionAccountForAssigment($AuctionAccounts$accountFinancesAccesses);
  897.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment($auctionAccount$pluginToken$plugin_version$ip);
  898.         $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  899. //        $login = $auctionAccount->getLogin();
  900. //        $password = $auctionAccount->getPassword();
  901.         $proxyAccount $auctionAccount->getProxyAccount();
  902.         $proxy = [];
  903.         if ($proxyAccount) {
  904.             $proxy = [
  905.                 'proxy' => [
  906.                     'host' => $proxyAccount->getProxyHost() ?? '',
  907.                     'port' => $proxyAccount->getProxyPort() ?? '',
  908.                     'username' => $proxyAccount->getProxyUsername() ?? '',
  909.                     'password' => $proxyAccount->getProxyPassword() ?? '',
  910.                 ]
  911.             ];
  912.         }
  913.         $data = [
  914.             'cars_and_bids' => [
  915.                 'login' => $pluginAuctionAssignment->getAuctionAccount()->getLogin() ?? '',
  916.                 'password' => $pluginAuctionAssignment->getAuctionAccount()->getPassword() ?? '',
  917.             ],
  918.             'assigment_id' => $pluginAuctionAssignment->getId(),
  919.             'can_manage_payments' => $canManagePayments,
  920.         ];
  921.         header(self::HEADERS);
  922.         return new JsonResponse(array_merge($data$proxy), 200);
  923.     }
  924.     /**
  925.      * @Route("/get_cars_account", name="get_cars_account", methods={"POST"})
  926.      * @return Response
  927.      */
  928.     public function getCarsAccount(Request $requestEntityManagerInterface $em): Response
  929.     {
  930.         try {
  931.             $pluginToken $this->_authUserByToken($em$request);
  932.         } catch (BadRequestException $e) {
  933.             header(self::HEADERS);
  934.             return new JsonResponse('Bad Request'400);
  935.         } catch (InvalidTokenException $e) {
  936.             header(self::HEADERS);
  937.             return new JsonResponse('Invalid token'401);
  938.         }
  939.         $parameters json_decode($request->getContent(), true);
  940.         $plugin_version $parameters['pv'] ?? '';
  941.         if (empty($plugin_version)) {
  942.             header(self::HEADERS);
  943.             return new JsonResponse('Invalid pv'400);
  944.         }
  945.         $ip CommonUtils::getClientIP($request);
  946.         $pluginUser $pluginToken->getPluginUser();
  947.         $AuctionAccounts $pluginUser->getOnlyCarsAccounts();
  948.         if (count($AuctionAccounts) < 1) {
  949.             header(self::HEADERS);
  950.             return new JsonResponse('Invalid Cars accounts'401);
  951.         }
  952.         $accountFinancesAccesses $pluginUser->getAccountsFinancesAccessMap();
  953.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  954.         $auctionAccount $assigmentRepository->selectAuctionAccountForAssigment($AuctionAccounts$accountFinancesAccesses);
  955.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment($auctionAccount$pluginToken$plugin_version$ip);
  956.         $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  957.         $proxyAccount $auctionAccount->getProxyAccount();
  958.         $proxy = [];
  959.         if ($proxyAccount) {
  960.             $proxy = [
  961.                 'proxy' => [
  962.                     'host' => $proxyAccount->getProxyHost() ?? '',
  963.                     'port' => $proxyAccount->getProxyPort() ?? '',
  964.                     'username' => $proxyAccount->getProxyUsername() ?? '',
  965.                     'password' => $proxyAccount->getProxyPassword() ?? '',
  966.                 ]
  967.             ];
  968.         }
  969.         $data = [
  970.             'cars' => [
  971.                 'login' => $pluginAuctionAssignment->getAuctionAccount()->getLogin() ?? '',
  972.                 'password' => $pluginAuctionAssignment->getAuctionAccount()->getPassword() ?? '',
  973.             ],
  974.             'assigment_id' => $pluginAuctionAssignment->getId(),
  975.             'can_manage_payments' => $canManagePayments,
  976.         ];
  977.         header(self::HEADERS);
  978.         return new JsonResponse(array_merge($data$proxy), 200);
  979.     }
  980.     /**
  981.      * @Route("/get_cargurus_account", name="get_cargurus_account", methods={"POST"})
  982.      * @return Response
  983.      */
  984.     public function getCargurusAccount(Request $requestEntityManagerInterface $em): Response
  985.     {
  986.         try {
  987.             $pluginToken $this->_authUserByToken($em$request);
  988.         } catch (BadRequestException $e) {
  989.             header(self::HEADERS);
  990.             return new JsonResponse('Bad Request'400);
  991.         } catch (InvalidTokenException $e) {
  992.             header(self::HEADERS);
  993.             return new JsonResponse('Invalid token'401);
  994.         }
  995.         $parameters json_decode($request->getContent(), true);
  996.         $plugin_version $parameters['pv'] ?? '';
  997.         if (empty($plugin_version)) {
  998.             header(self::HEADERS);
  999.             return new JsonResponse('Invalid pv'400);
  1000.         }
  1001.         $ip CommonUtils::getClientIP($request);
  1002.         $pluginUser $pluginToken->getPluginUser();
  1003.         $AuctionAccounts $pluginUser->getOnlyCargurusAccounts();
  1004.         if (count($AuctionAccounts) < 1) {
  1005.             header(self::HEADERS);
  1006.             return new JsonResponse('Invalid Cargurus accounts'401);
  1007.         }
  1008.         $accountFinancesAccesses $pluginUser->getAccountsFinancesAccessMap();
  1009.         $assigmentRepository $em->getRepository(PluginUserAuctionAccountAssigment::class);
  1010.         $auctionAccount $assigmentRepository->selectAuctionAccountForAssigment($AuctionAccounts$accountFinancesAccesses);
  1011.         $pluginAuctionAssignment $assigmentRepository->createAuctionAssignment($auctionAccount$pluginToken$plugin_version$ip);
  1012.         $canManagePayments $accountFinancesAccesses[$auctionAccount->getId()] ?? false;
  1013.         $proxyAccount $auctionAccount->getProxyAccount();
  1014.         $proxy = [];
  1015.         if ($proxyAccount) {
  1016.             $proxy = [
  1017.                 'proxy' => [
  1018.                     'host' => $proxyAccount->getProxyHost() ?? '',
  1019.                     'port' => $proxyAccount->getProxyPort() ?? '',
  1020.                     'username' => $proxyAccount->getProxyUsername() ?? '',
  1021.                     'password' => $proxyAccount->getProxyPassword() ?? '',
  1022.                 ]
  1023.             ];
  1024.         }
  1025.         $data = [
  1026.             'cargurus' => [
  1027.                 'login' => $pluginAuctionAssignment->getAuctionAccount()->getLogin() ?? '',
  1028.                 'password' => $pluginAuctionAssignment->getAuctionAccount()->getPassword() ?? '',
  1029.             ],
  1030.             'assigment_id' => $pluginAuctionAssignment->getId(),
  1031.             'can_manage_payments' => $canManagePayments,
  1032.         ];
  1033.         header(self::HEADERS);
  1034.         return new JsonResponse(array_merge($data$proxy), 200);
  1035.     }
  1036.     /**
  1037.      * @Route("/send_app_debug", name="send_app_debug", methods={"POST"})
  1038.      * @return Response
  1039.      */
  1040.     public function sendAppDebug(Request $requestEntityManagerInterface $em): Response
  1041.     {
  1042.         $plugin_login $request->get('plugin_login''');
  1043.         $plugin_user $em->getRepository(PluginUser::class)->findOneBy(['login' => $plugin_login]);
  1044.         //TODO: add checking if is set debug flag for plugin user
  1045.         if (!$plugin_user instanceof PluginUser) {
  1046.             return new JsonResponse('Invalid user'401);
  1047.         }
  1048.         $date = (new \DateTime('now'))->format('Y-m-d');
  1049.         $files $request->files->all();
  1050.         if(!array_key_exists('files'$files)) {
  1051.             return new JsonResponse('File error'Response::HTTP_BAD_REQUEST, []);
  1052.         }
  1053.         $files $files['files'];
  1054.         $files_uploaded $this->_uploadedFiles$files,"fk_o1z_app_logs/{$plugin_login}/{$date});
  1055.         $result = [
  1056.             'status'=>'Success',
  1057.             'message' => 'Logs are successfully saved',
  1058.             'files_url' => $files_uploaded//TODO: Remove after debug!
  1059.         ];
  1060.         return new JsonResponse($result200, []);
  1061.     }
  1062.     private function _uploadedFiles($added_files_set$store_path){
  1063.         $_files = [];
  1064.         if (!empty($added_files_set)) {
  1065.             foreach ($added_files_set as $_newFile) {
  1066.                 if($_newFile instanceof UploadedFile) {
  1067.                     $new_file_path $this->fileUploader->upload($store_path$_newFile'');
  1068.                     if ($new_file_path) {
  1069.                         $_files[] = $this->fileUploader->getAbsoluteURLToFile($new_file_path);
  1070.                     }
  1071.                 }
  1072.             }
  1073.         }
  1074.         return $_files;
  1075.     }
  1076.     /**
  1077.      * @Route("/check_app_updates", name="check_app_updates", methods={"POST"})
  1078.      * @return Response
  1079.      */
  1080.     public function checkAppUpdates(Request $requestEntityManagerInterface $em): Response
  1081.     {
  1082.         try {
  1083.             $pluginToken $this->_authUserByToken($em$request);
  1084.         } catch (BadRequestException $e) {
  1085.             header(self::HEADERS);
  1086.             return new JsonResponse('Bad Request'400);
  1087.         } catch (InvalidTokenException $e) {
  1088.             header(self::HEADERS);
  1089.             return new JsonResponse('Invalid token'401);
  1090.         }
  1091.         $parameters json_decode($request->getContent(), true);
  1092.         //TODO: This is only for test. Make Production checks !!!
  1093.         $data = [
  1094.           'update_available' => false,
  1095.           'update_required' => false,
  1096.         ];
  1097.         $platform $parameters['platform'] ?? '';
  1098.         $version $parameters['version'] ?? '';
  1099.         if($platform == 'windows' && $version == '0.3.33') {
  1100.             $data['update_available'] = true;
  1101.             $data['update_required'] = false;
  1102.             $data['update_link'] =  'http://my.zvigerauto.com/Setup_ZvigerautoAuction_v0334.exe';
  1103.             $data['update_message'] = 'New Version is available';
  1104.         }
  1105.         if($platform == 'windows' && $version == '0.3.32') {
  1106.             $data['update_available'] = true;
  1107.             $data['update_required'] = true;
  1108.             $data['update_link'] =  'http://my.zvigerauto.com/Setup_ZvigerautoAuction_v0334.exe';
  1109.             $data['update_message'] = 'New Version is available';
  1110.         }
  1111.         return new JsonResponse(['data'=>$data], Response::HTTP_OK, []);
  1112.     }
  1113.     /**
  1114.      * @Route("/add_event_log_dev", name="add_event_log_dev", methods={"POST"})
  1115.      * @param Request $request
  1116.      * @param EntityManagerInterface $em
  1117.      * @return Response
  1118.      */
  1119.     public function addEventLogProd(Request $requestEntityManagerInterface $em): Response
  1120.     {
  1121.         $parameters json_decode($request->getContent(), true);
  1122.         if (empty($parameters['event'])) {
  1123.             header(self::HEADERS);
  1124.             return new JsonResponse('Empty event'400);
  1125.         }
  1126.         if (empty($parameters['eventData']) || !is_array($parameters['eventData'])) {
  1127.             header(self::HEADERS);
  1128.             return new JsonResponse('Bad data'400);
  1129.         }
  1130.         $ip CommonUtils::getClientIP($request);
  1131.         $prodPluginEventLog = (new ProdPluginEventLog())
  1132.             ->setIp($ip)
  1133.             ->setCreatedAt(new \DateTime('now'))
  1134.             ->setName($parameters['event'])
  1135.             ->setData($parameters['eventData']);
  1136.         if( !empty($parameters['eventData']['origin']) && in_array($parameters['eventData']['origin'], PluginEventLog::ORIGINS) ){
  1137.             $prodPluginEventLog->setOrigin($parameters['eventData']['origin']);
  1138.         }
  1139.         $em->persist($prodPluginEventLog);
  1140.         $em->flush();
  1141.         header(self::HEADERS);
  1142.         return new JsonResponse(['status' => 'Ok'], 200);
  1143.     }
  1144.     /**
  1145.      * @Route("/bid_clicked_dev", name="bid_clicked_dev", methods={"POST"})
  1146.      * @param PluginTokenRepository $pluginTokenRepository
  1147.      * @param Request $request
  1148.      * @param EntityManagerInterface $em
  1149.      * @return Response
  1150.      */
  1151.     public function bidClickedDev(Request $requestEntityManagerInterface $em): Response
  1152.     {
  1153.         $parameters json_decode($request->getContent(), true);
  1154.         $lot $parameters['lot'] ?? '';
  1155.         $bid $parameters['bid'] ?? false;
  1156.         $bid_type strtolower($parameters['bid_type']) ?? '';
  1157.         $window_host $parameters['window_host'] ?? '';
  1158.         $loggedCopart =
  1159.             $parameters['loggedCopart'] ??
  1160.             $parameters['loggedIaai'] ??
  1161.             $parameters['loggedManheim'] ??
  1162.             $parameters['loggedBringatrailer'] ??
  1163.             $parameters['loggedCars_and_bids'] ??
  1164.             $parameters['loggedCars'] ??
  1165.             $parameters['loggedCargurus'] ?? false;
  1166.         if (empty($lot)) {
  1167.             header(self::HEADERS);
  1168.             return new JsonResponse('Invalid lot'400);
  1169.         }
  1170.         if ($bid === false) {
  1171.             header(self::HEADERS);
  1172.             return new JsonResponse('Invalid bid'400);
  1173.         }
  1174.         if(empty($bid_type) || !in_array($bid_typeAuctionBid::TYPES)){
  1175.             header(self::HEADERS);
  1176.             return new JsonResponse('Invalid bid_type'400);
  1177.         }
  1178.         if (empty($parameters['assigment_id'])) {
  1179.             header(self::HEADERS);
  1180.             return new JsonResponse('Invalid assigment'406);
  1181.         }
  1182.         /******* end *********/
  1183.         $auctionBid = (new ProdAuctionBid())
  1184.             ->setCreatedAt(new \DateTime('now'))
  1185.             ->setAssignation(['assigment_id' => $parameters['assigment_id']])
  1186.             ->setBid((float)$bid)
  1187.             ->setLot($lot)
  1188.             ->setLoggedCopart((bool)$loggedCopart)
  1189.             ->setType($bid_type);
  1190.         $em->persist($auctionBid);
  1191.         $em->flush();
  1192.         header(self::HEADERS);
  1193.         return new JsonResponse(['history_id' => $auctionBid->getId()], 200);
  1194.     }
  1195. }