src/Controller/ObjectController.php line 536

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ObjectAvanti;
  4. use App\Repository\AuctionBidRepository;
  5. use App\Repository\FavoriteRepository;
  6. use App\Schemas\MyLastBidSchema;
  7. use App\Schemas\ObjectSchema;
  8. use App\Service\Api\Avanti\CustomerService;
  9. use App\Service\Customer\CustomerService as DbCustomerService;
  10. use App\Service\Api\Avanti\ObjectService;
  11. use DateTime;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\Cache\Adapter\AdapterInterface as Cache;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\Security\Core\User\UserInterface;
  19. /**
  20.  * @Route ("", name="object_")
  21.  */
  22. class ObjectController extends BaseController
  23. {
  24.     /**
  25.      * @Route ("/object/findById", name="getObjectById")
  26.      *
  27.      * @throws \Exception
  28.      */
  29.     public function getObjectById(ObjectService $objectServiceRequest $request): Response
  30.     {
  31.         $dataParams $this->getData($request, ['id'], 'fields');
  32.         $data       $objectService->getObjectById((int) $dataParams['fields']['id'], false);
  33.         if (!$data) {
  34.             return new JsonResponse([]);
  35.         }
  36.         $object = new ObjectAvanti();
  37.         if (!empty($data)) {
  38.             $object->mapping($data);
  39.         }
  40.         if ($actualCurrencyEur $objectService->getExchangeEur()) {
  41.             $object->setCurrentEurPrice($actualCurrencyEur['nok']);
  42.         }
  43.         $result $this->getEncoder($object, [], ObjectAvanti::class, ObjectSchema::class);
  44.         return new JsonResponse(json_decode($result));
  45.     }
  46.     /**
  47.      * @Route ("/object/goingtoauction", name="goingtoauction")
  48.      */
  49.     public function goingtoauction(ObjectService $objectServiceRequest $request): Response
  50.     {
  51.         $data $this->getData($request, [], 'filter');
  52.         $data['size'] ??= 8;
  53.         $data['filter']['whereDate'] = [
  54.             'field'     => 'Slutt',
  55.             'operation' => '>',
  56.             'date'      => date('Y-m-d H:i:s'),
  57.         ];
  58.         $data['filter']['onlyAvailable']   = true;
  59.         $data['filter']['countCategories'] = true;
  60.         $data['filter']['department_id']   = [ObjectAvanti::AVANTI_DEPARTMENT_ONLINEObjectAvanti::AVANTI_DEPARTMENT_CLASSIC];
  61.         $data['sort']                      = 'OnlineClassicStart';
  62.         $data['direction']                 = 'DESC';
  63.         if (date('H') < 9) {
  64.             $startDate = new DateTime('yesterday');
  65.             $endDate = new DateTime();
  66.         } else {
  67.             $startDate = new DateTime();
  68.             $endDate = new DateTime('tomorrow');
  69.         }
  70.         $startDate->setTime(9,0,0);
  71.         $endDate->setTime(8,59,59);
  72.         $data['filter']['betweenDate'] = [
  73.             'field'  => 'Start',
  74.             'start'  => $startDate->format('Y-m-d H:i:s'),
  75.             'finish' => $endDate->format('Y-m-d H:i:s'),
  76.         ];
  77.         $objects $objectService->getObjectSearch($data);
  78.         $result = [];
  79.         if (!is_null($objects) && property_exists($objects'data')) {
  80.             $objectArray = [];
  81.             foreach ($objects->data as $obj) {
  82.                 $object = new ObjectAvanti();
  83.                 array_push($objectArray$object->mapping($obj));
  84.             }
  85.             $result $this->getEncoder($objectArray$this->getMeta($objects->paginate ?? null$data['sort'], $data['direction'], $objects->aggregations ?? null), ObjectAvanti::class, ObjectSchema::class);
  86.             $result json_decode($result);
  87.         }
  88.         return new JsonResponse($result);
  89.     }
  90.     /**
  91.      * @Route ("/object/endingauction", name="endingauction")
  92.      */
  93.     public function endingauction(ObjectService $objectServiceRequest $request): Response
  94.     {
  95.         $data $this->getData($request, [], 'filter');
  96.         $data['size'] ??= 8;
  97.         $data['filter']['whereDate'] = [
  98.             'field'     => 'Slutt',
  99.             'operation' => '>',
  100.             'date'      => date('Y-m-d H:i:s'),
  101.         ];
  102.         $data['filter']['isNettauksjon']   = true;
  103.         $data['filter']['countCategories'] = true;
  104.         $data['filter']['department_id']   = [ObjectAvanti::AVANTI_DEPARTMENT_ONLINE];
  105.         $data['sort']                      = 'Slutt';
  106.         $data['direction']                 = 'ASC';
  107.         $objects                           $objectService->getObjectSearch($data);
  108.         $result = [];
  109.         if (!is_null($objects) && property_exists($objects'data')) {
  110.             $objectsDate = new DateTime($objects->data[0]->Slutt);
  111.             $data['filter']['betweenDate'] = [
  112.                 'field'  => 'Slutt',
  113.                 'start'  => $objectsDate->format('Y-m-d') . ' 00:00:00',
  114.                 'finish' => $objectsDate->format('Y-m-d') . ' 23:59:59',
  115.             ];
  116.             $objects $objectService->getObjectSearch($data);
  117.             $objectArray = [];
  118.             foreach ($objects->data as $obj) {
  119.                 $object = new ObjectAvanti();
  120.                 array_push($objectArray$object->mapping($obj));
  121.             }
  122.             $result $this->getEncoder($objectArray$this->getMeta($objects->paginate ?? null$data['sort'], $data['direction'], $objects->aggregations ?? null), ObjectAvanti::class, ObjectSchema::class);
  123.             $result json_decode($result);
  124.         }
  125.         return new JsonResponse($result);
  126.     }
  127.     /**
  128.      * @Route ("/object/getObjectSold", name="getObjectSold")
  129.      */
  130.     public function getObjectSold(ObjectService $objectServiceRequest $requestFavoriteRepository $favoriteRepositoryUserInterface $customer null): Response
  131.     {
  132.         $data $this->getData($request, [], 'filter');
  133.         $data['size'] ??= 8;
  134.         $data['filter']['countCategories'] = true;
  135.         $data['filter']['onlySold']        = true;
  136.         $data['filter']['onlyAvailable']   = false;
  137.         $data['filter']['showObjectStartFuture'] = true;
  138.         $data['sort']                      = $data['sort'] ?? 'CurrentEndDate';
  139.         $data['direction']                 = $data['direction'] ?? 'DESC';
  140.         $objects                           $objectService->getObjectSearch($data);
  141.         if (!isset($objects->data) or (null == $objects && null == $objects->data)) {
  142.             return new JsonResponse([]);
  143.         }
  144.         $favoriteIds = [];
  145.         if ($customer) {
  146.             $favoriteIds array_flip($favoriteRepository->listIDsByCustomer($customer->getId()));
  147.         }
  148.         $objectArray = [];
  149.         foreach ($objects->data as $obj) {
  150.             $obj->favorite = isset($favoriteIds[$obj->Objektid]);
  151.             $object        = new ObjectAvanti();
  152.             array_push($objectArray$object->mapping($obj));
  153.         }
  154.         $result $this->getEncoder($objectArray$this->getMeta($objects->paginate ?? null$data['sort'], $data['direction'], $objects->aggregations ?? null), ObjectAvanti::class, ObjectSchema::class);
  155.         return new JsonResponse(json_decode($result));
  156.     }
  157.     /**
  158.      * @Route ("/object/getObjectPhotos", name="getObjectPhotos")
  159.      */
  160.     public function getObjectPhotos(ObjectService $objectServiceRequest $request): Response
  161.     {
  162.         $dataParams $this->getData($request, ['objectNo'], 'fields');
  163.         $size       $dataParams['fields']['size'] ?? null;
  164.         $data   $objectService->getFotowarePhotos($dataParams['fields']['objectNo'], $size);
  165.         $result = ['data' => $data];
  166.         return new JsonResponse($result);
  167.     }
  168.     /**
  169.      * @Route ("/object/findobject", name="findObject")
  170.      *
  171.      * @throws \Exception
  172.      */
  173.     public function findObject(ObjectService $objectServiceRequest $requestFavoriteRepository $favoriteRepositoryUserInterface $customer null)
  174.     {
  175.         $data $this->getData($request, [], 'filter');
  176.         if (null == $data['sort']) {
  177.             if (isset($data['filter']['isNettauksjon']) && 'false' === $data['filter']['isNettauksjon']) {
  178.                 $data['sort'] = 'Katalognr';
  179.             } else {
  180.                 $data['sort'] = 'OnlineClassicSlutt';
  181.             }
  182.         }
  183.         if (null == $data['direction']) {
  184.             $data['direction'] = 'ASC';
  185.         }
  186.         if (!isset($data['filter']['auction_id'])) {
  187.             if (isset($data['filter']['recently_sold']) and 'true' == $data['filter']['recently_sold']) {
  188.                 $data['filter']['whereDate'] = [
  189.                     'field'     => 'Slutt',
  190.                     'operation' => '<',
  191.                     'date'      => date('Y-m-d H:i:s'),
  192.                 ];
  193.             } else {
  194.                 $data['filter']['whereDate'] = [
  195.                     'field'     => 'Slutt',
  196.                     'operation' => '>',
  197.                     'date'      => date('Y-m-d H:i:s'),
  198.                 ];
  199.             }
  200.         } else {
  201.             $data['filter']['onlyAvailable'] = false;
  202.         }
  203.         $object $objectService->getObjectSearch($data);
  204.         if (!isset($object->data)) {
  205.             return new JsonResponse(['meta' => [], 'data' => []]);
  206.         }
  207.         $favoriteIds = [];
  208.         if ($customer) {
  209.             $favoriteIds array_flip($favoriteRepository->listIDsByCustomer($customer->getId()));
  210.         }
  211.         $objectArray = [];
  212.         foreach ($object->data as $obj) {
  213.             $objectAvanti  = new ObjectAvanti();
  214.             $obj->favorite = isset($favoriteIds[$obj->Objektid]);
  215.             $objectArray[] = $objectAvanti->mapping($obj);
  216.         }
  217.         $result $this->getEncoder($objectArray$this->getMeta($object->paginate ?? null$data['sort'], $data['direction'], $object->aggregations ?? null), ObjectAvanti::class, ObjectSchema::class);
  218.         return new JsonResponse(json_decode($result));
  219.     }
  220.     /**
  221.      * @Route ("/api/admin/object/updateImage/{object_id}", name="updateImage", methods={"POST"})
  222.      *
  223.      * @throws \Exception
  224.      */
  225.     public function updateImage(ObjectService $objectServiceRequest $request)
  226.     { 
  227.         $objectId $request->attributes->get('object_id');
  228.         $objectService->updateImageObject($objectId);
  229.         return new JsonResponse(['success' => true]);
  230.     }
  231.     /**
  232.      * @Route ("/object/getshippingtype", name="getShippingType")
  233.      */
  234.     public function getShippingType(ObjectService $objectServiceRequest $requestCache $cache)
  235.     {
  236.         $dataParams $this->getData($request, ['objectId'], 'fields');
  237.         $cacheItem $cache->getItem(__FUNCTION__ http_build_query($dataParams));
  238.         $cacheItem->expiresAfter(3600);
  239.         if ($cacheItem->isHit()) {
  240.             return $cacheItem->get();
  241.         }
  242.         $data   $objectService->getShippingType($dataParams['fields']['objectId'], $dataParams['fields']['zipCode'] ?? null);
  243.         $result = ['data' => $data];
  244.         $cacheItem->set(new JsonResponse($result));
  245.         $cache->save($cacheItem);
  246.         return $cacheItem->get();
  247.     }
  248.     /**
  249.      * @Route ("/object/searchobject", name="searchObject")
  250.      *
  251.      * @throws \GuzzleHttp\Exception\GuzzleException
  252.      */
  253.     public function searchObject(ObjectService $objectServiceRequest $request)
  254.     {
  255.         $data $this->getData($request, [], 'filter');
  256.         if (null == $data['sort']) {
  257.             $data['sort'] = 'Objektid';
  258.         }
  259.         if (null == $data['direction']) {
  260.             $data['direction'] = 'ASC';
  261.         }
  262.         $object $objectService->getObjectSearchPreview($data);
  263.         return new JsonResponse(['data' => ['categories' => $object]]);
  264.     }
  265.     /**
  266.      * @Route ("/object/searchcomplete", name="searchComplete")
  267.      *
  268.      * @throws \GuzzleHttp\Exception\GuzzleException
  269.      */
  270.     public function searchComplete(ObjectService $objectServiceRequest $request)
  271.     {
  272.         $data $this->getData($request, [], 'filter');
  273.         $data['filter']['onlyAvailable'] = false;
  274.         $search $objectService->getObjectSearch($data);
  275.         $objects = [];
  276.         foreach ($search->data as $object) {
  277.             $objects[] = $object->title;
  278.         }
  279.         return new JsonResponse(['data' => array_values(array_unique($objectsSORT_REGULAR))]);
  280.     }
  281.     /**
  282.      * @Route ("/object/getPopularCategories", name="getPopularCategories")
  283.      */
  284.     public function getPopularCategories(ObjectService $objectServiceRequest $request): Response
  285.     {
  286.         $data   $objectService->getPopularCategories();
  287.         $result = ['data' => $data];
  288.         return new JsonResponse($result);
  289.     }
  290.     /**
  291.      * @Route ("/api/object/registerAdvanceBid", name="registerAdvanceBid", methods={"POST"})
  292.      */
  293.     public function registerAdvanceBid(ObjectService $objectServiceRequest $requestCustomerService $customerService): Response
  294.     {
  295.         $data $this->getData($request, ['object_id''value']);
  296.         $objectService->registerAdvanceBid($this->getUser(), $data['attributes']['object_id'], $data['attributes']['value'], $customerService);
  297.         return new JsonResponse([]);
  298.     }
  299.     /**
  300.      * @Route ("/api/object/registerPhoneBid", name="registerPhoneBid", methods={"POST"})
  301.      */
  302.     public function registerPhoneBid(ObjectService $objectServiceRequest $requestCustomerService $customerService): Response
  303.     {
  304.         $data $this->getData($request, ['object_id''message']);
  305.         $objectService->registerPhoneBid($this->getUser(), $data['attributes']['object_id'], $data['attributes']['message'], $customerService);
  306.         return new JsonResponse([]);
  307.     }
  308.     /**
  309.      * @Route ("/object/getStorageFee", name="getStorageFee")
  310.      */
  311.     public function getStorageFee(ObjectService $objectServiceRequest $request)
  312.     {
  313.         $dataParams $this->getData($request, ['objectNo'], 'fields');
  314.         $data   $objectService->getStorageFee($dataParams['fields']['objectNo']);
  315.         $result = ['data' => $data];
  316.         return new JsonResponse($result);
  317.     }
  318.     /**
  319.      * @Route ("/api/object/mylastbids", name="object_mylastbids")'
  320.      *
  321.      * @throws \Exception
  322.      */
  323.     public function mylastbids(ObjectService $objectServiceRequest $requestAuctionBidRepository $auctionBidRepositoryUserInterface $customer): Response
  324.     {
  325.         $requestData       $this->getData($request, [], 'filter');
  326.         $data['page']      = $requestData['page'];
  327.         $data['size']      = $requestData['size'];
  328.         $data['sort']      = $requestData['sort']      ?? 'Slutt';
  329.         $data['direction'] = $requestData['direction'] ?? 'DESC';
  330.         $data['filter']    = $requestData['filter']    ?? [];
  331.         $objects $auctionBidRepository->getMyBidsObjectsIds($customer);
  332.         if (!$objects) {
  333.             return new JsonResponse([]);
  334.         }
  335.         $data['filter']['ObjectId'] = implode(','array_column($objects'object_id'));
  336.         if (isset($requestData['filter']['active_auction'])) {
  337.             if ($requestData['filter']['active_auction']) {
  338.                 $data['filter']['whereDate'] = [
  339.                     'field'     => 'Slutt',
  340.                     'operation' => '>',
  341.                     'date'      => date('Y-m-d H:i:s'),
  342.                 ];
  343.                 $data['sort']      = $requestData['sort']      ?? 'Slutt';
  344.                 $data['direction'] = $requestData['direction'] ?? 'ASC';
  345.             } else {
  346.                 $data['filter']['whereDate'] = [
  347.                     'field'     => 'Slutt',
  348.                     'operation' => '<',
  349.                     'date'      => date('Y-m-d H:i:s'),
  350.                 ];
  351.             }
  352.         }
  353.         $object $objectService->getObjectSearch($data);
  354.         if (!isset($object->data[0])) {
  355.             return new JsonResponse([]);
  356.         }
  357.         $objectArray = [];
  358.         foreach ($object->data as $objectData) {
  359.             $objectAvanti = new ObjectAvanti();
  360.             $objectAvanti->mapping($objectData);
  361.             $auction array_values(array_filter($objects, function ($object) use ($objectAvanti) {
  362.                 return $object['object_id'] == $objectAvanti->getObjectId();
  363.             }));
  364.             if (!isset($auction[0]['auction_id'])) {
  365.                 continue;
  366.             }
  367.             $lastBid   $auctionBidRepository->getLastBid($auction[0]['auction_id'], $objectAvanti->getObjectId());
  368.             $mylastBid $auctionBidRepository->getMyLastBid($customer$auction[0]['auction_id'], $objectAvanti->getObjectId());
  369.             if ($lastBid) {
  370.                 $objectAvanti->setWinningAuction($customer->getId() == $lastBid->getObjectCustomer()->getCustomer()->getId());
  371.                 $objectAvanti->setMyBid($mylastBid->getValue() ?? null);
  372.                 $objectAvanti->setObjectCustomerId($mylastBid->getObjectCustomer()->getId());
  373.             } else {
  374.                 $objectAvanti->setWinningAuction(false);
  375.             }
  376.             $objectArray[] = $objectAvanti;
  377.         }
  378.         $result $this->getEncoder($objectArray$this->getMeta($object->paginate ?? null$data['sort'], $data['direction'], $object->aggregations ?? null), ObjectAvanti::class, MyLastBidSchema::class);
  379.         return new JsonResponse(json_decode($result));
  380.     }
  381.     /**
  382.      * @Route ("/api/object/cutprice/{id}", name="cutPrice")
  383.      *
  384.      * @throws \Exception
  385.      */
  386.     public function cutPrice(ObjectService $objectServiceRequest $request): Response
  387.     {
  388.         $dataParams $this->getData($request, ['value']);
  389.         $object_id $request->attributes->get('id');
  390.         $value     $dataParams['attributes']['value'];
  391.         $data      $objectService->cutPrice($object_id$valuetrue);
  392.         $object = new ObjectAvanti();
  393.         if (!empty($data)) {
  394.             $object->mapping($data);
  395.         }
  396.         $result $this->getEncoder($object, [], ObjectAvanti::class, ObjectSchema::class);
  397.         return new JsonResponse(json_decode($result));
  398.     }
  399.     /**
  400.      * @Route ("/object/objectpreview", name="objectpreview")
  401.      *
  402.      * @throws \Exception
  403.      */
  404.     public function objectPreview(ObjectService $objectServiceRequest $request): Response
  405.     {
  406.         $dataParams                              $this->getData($request, ['objectNo'], 'fields');
  407.         $data['filter']['q']                     = $dataParams['fields']['objectNo'];
  408.         $data['filter']['onlyAvailable']         = false;
  409.         $data['filter']['shouldSkipNettauksjon'] = true;
  410.         $objectResult $objectService->getObjectSearch($data);
  411.         if (!isset($objectResult->data[0])) {
  412.             return new JsonResponse([]);
  413.         }
  414.         $object = new ObjectAvanti();
  415.         $object->mapping($objectResult->data[0]);
  416.         if ($actualCurrencyEur $objectService->getExchangeEur()) {
  417.             $object->setCurrentEurPrice($actualCurrencyEur['nok']);
  418.         }
  419.         $result $this->getEncoder($object, [], ObjectAvanti::class, ObjectSchema::class);
  420.         return new JsonResponse(json_decode($result));
  421.     }
  422.     /**
  423.      * @Route ("/api/admin/object/update-enddate", name="NettobjektUpdate", methods={"PUT"})
  424.      *
  425.      * @throws \Exception
  426.      */
  427.     public function updateObjectEndDate(ObjectService $objectServiceRequest $request)
  428.     { 
  429.         try {
  430.             $idsUpdated = [];
  431.             $content json_decode($request->getContent(), true);
  432.             if(isset($content['objects'])){
  433.                 $objectSlutts $content['objects'];
  434.                 $idsUpdated $objectService->updateEndDate($objectSlutts);
  435.             }
  436.             return new JsonResponse(['success' => true'ids'=> $idsUpdated]);
  437.         } catch (\Throwable $th) {
  438.             return new JsonResponse(['success' => false'ids'=> null'error' => $th->getMessage()], 400);
  439.         }
  440.     }
  441.     /**
  442.      * @Route ("/api/admin/object/clear-cache", name="ClearObjectCaches", methods={"POST"})
  443.      *
  444.      * @throws \Exception
  445.      */
  446.     public function clearObjectCaches(ObjectService $objectServiceRequest $request)
  447.     {
  448.         try {
  449.             $data json_decode($request->getContent(), true);
  450.             foreach ($data['objects'] ?? [] as $object_id) {
  451.                 $objectService->deleteObjectCache($object_id);
  452.             }
  453.             return new JsonResponse(['success' => true]);
  454.         } catch (\Throwable $th) {
  455.             return new JsonResponse(['success' => false'error' => $th->getMessage()], 400);
  456.         }
  457.     }
  458.     /**
  459.      * @Route ("/api/admin/object/send-advance-bid-notification", name="sendAdvanceBidNotification", methods={"POST"})
  460.      */
  461.     public function sendAdvanceBidNotification(ObjectService $objectServiceRequest $requestDbCustomerService $customerService): Response
  462.     {
  463.         $data $this->getData($request, ['customer_number''object_id''value']);
  464.         $objectService->sendAdvanceBidNotification($customerService->getCustomerByCustomerNumberOrCreate($data['attributes']['customer_number']), $data['attributes']['object_id'], $data['attributes']['value']);
  465.         return new JsonResponse([]);
  466.     }
  467.     /**
  468.      * @Route ("/api/admin/object/send-phone-bid-notification", name="sendPhoneBidNotification", methods={"POST"})
  469.      */
  470.     public function sendPhoneBidNotification(ObjectService $objectServiceRequest $requestDbCustomerService $customerService): Response
  471.     {
  472.         $data $this->getData($request, ['customer_number''object_id''message']);
  473.         $objectService->sendPhoneBidNotification($customerService->getCustomerByCustomerNumberOrCreate($data['attributes']['customer_number']), $data['attributes']['object_id'], $data['attributes']['message'], $customerService);
  474.         return new JsonResponse([]);
  475.     }
  476.     /**
  477.      * @Route ("/api/admin/object/send-manual-start-storage-notification", name="sendManualStartStorageNotification", methods={"POST"})
  478.      */
  479.     public function sendManualStartStorageNotification(ObjectService $objectServiceRequest $requestDbCustomerService $customerService): Response
  480.     {
  481.         $data $this->getData($request, ['customer_number''object_id']);
  482.         $objectService->sendManualStartStorageNotification($customerService->getCustomerByCustomerNumberOrCreate($data['attributes']['customer_number']), $data['attributes']['object_id']);
  483.         return new JsonResponse([]);
  484.     }
  485. }