src/Aqarmap/Bundle/SearchBundle/Controller/Api/V4/ListingSearchController.php line 592

  1. #[Rest\QueryParam(name: 'minRoom', requirements: '\d+', default: null, description: 'Minimum Room')]
  2. #[Rest\QueryParam(name: 'floor', requirements: '\d+', default: null, description: 'Floor Number')]
  3. #[Rest\QueryParam(name: 'room', requirements: '\d+', default: null, description: 'Number of Rooms')]
  4. #[Rest\QueryParam(name: 'baths', requirements: '\d+', default: null, description: 'Number of Baths')]
  5. #[Rest\QueryParam(name: 'finishType', default: null, description: 'finish Type')]
  6. #[Rest\QueryParam(name: 'sellerRole', requirements: '\d+', default: null, description: 'sellerRole')]
  7. #[Rest\QueryParam(name: 'paymentMethod', requirements: '\d+', default: null, description: 'paymentMethod')]
  8. #[Rest\QueryParam(name: 'deliveryYear', requirements: '\d+', default: null, description: 'deliveryYear')]
  9. #[Rest\QueryParam(name: 'bath', default: null, description: 'Number of Baths')]
  10. #[Rest\QueryParam(name: 'photos', requirements: '(1)|(0)', default: '0', description: 'Get only listings with photos', strict: true, nullable: true)]
  11. #[Rest\QueryParam(name: 'isMortgage', requirements: '(1)|(0)', default: '0', description: 'Get only listings that support mortgage', strict: true, nullable: true)]
  12. #[Rest\QueryParam(name: 'eligibleForMortgage', description: 'Get listings that has mortgage Percentage', strict: true, nullable: true)]
  13. #[Rest\QueryParam(name: 'page', requirements: '\d+', default: 1, description: 'Page number, starting from 1.', nullable: true)]
  14. #[Rest\QueryParam(name: 'limit', requirements: '\d+', default: 10, description: 'Number of items per page.', nullable: true)]
  15. #[Rest\QueryParam(name: 'sort', requirements: 'price|area', default: null, description: 'Sort search results by price or area.', nullable: true)]
  16. #[Rest\QueryParam(name: 'direction', requirements: 'asc|desc', default: 'asc', description: 'Ascending (A to Z, 0 to 9), Descending (Z to A, 9 to 0)', nullable: true)]
  17. #[Rest\QueryParam(name: 'keywordSearch', description: "Search by keyword in listing's title, description and address")]
  18. #[Rest\QueryParam(name: 'unitOnly', requirements: '(1)|(0)', description: 'Get compound units only', nullable: true)]
  19. #[Rest\View]
  20. #[Cache(expires: '+2 hours', maxage: '+2 hours', smaxage: '+2 hours', public: true, vary: ['Accept-Language', 'X-Accept-Version', 'Accept'])]
  21. #[OA\Parameter(name: 'propertyType', description: 'Property Type ID', in: 'query', required: false)]
  22. #[OA\Parameter(name: 'location', description: 'Location ID comma spereted', in: 'query', required: false)]
  23. #[OA\Parameter(name: 'section', description: 'Section ID', in: 'query', required: false)]
  24. #[OA\Parameter(name: 'bounds', description: 'Map bounds (example: 24.6275450,46.6363017,24.6977461,46.817232)', in: 'query', required: false)]
  25. #[OA\Parameter(name: 'minPrice', description: 'Minimum Prices', in: 'query', required: false)]
  26. #[OA\Parameter(name: 'maxPrice', description: 'Maximum Prices', in: 'query', required: false)]
  27. #[OA\Parameter(name: 'minArea', description: 'Minimum Area', in: 'query', required: false)]
  28. #[OA\Parameter(name: 'maxArea', description: 'Maximum Area', in: 'query', required: false)]
  29. #[OA\Parameter(name: 'minFloor', description: 'Minimum Floor', in: 'query', required: false)]
  30. #[OA\Parameter(name: 'minRoom', description: 'Minimum Room', in: 'query', required: false)]
  31. #[OA\Parameter(name: 'floor', description: 'Floor Number', in: 'query', required: false)]
  32. #[OA\Parameter(name: 'room', description: 'Number of Rooms', in: 'query', required: false)]
  33. #[OA\Parameter(name: 'baths', description: 'Number of Baths', in: 'query', required: false)]
  34. #[OA\Parameter(name: 'finishType', description: 'finish Type', in: 'query', required: false)]
  35. #[OA\Parameter(name: 'sellerRole', description: 'sellerRole', in: 'query', required: false)]
  36. #[OA\Parameter(name: 'paymentMethod', description: 'paymentMethod', in: 'query', required: false)]
  37. #[OA\Parameter(name: 'deliveryYear', description: 'deliveryYear', in: 'query', required: false)]
  38. #[OA\Parameter(name: 'bath', description: 'Number of Baths', in: 'query', required: false)]
  39. #[OA\Parameter(name: 'photos', description: 'Get only listings with photos', in: 'query', required: false)]
  40. #[OA\Parameter(name: 'isMortgage', description: 'Get only listings that support mortgage', in: 'query', required: false)]
  41. #[OA\Parameter(name: 'eligibleForMortgage', description: 'Get listings that has mortgage Percentage', in: 'query', required: false)]
  42. #[OA\Parameter(name: 'page', description: 'Page number, starting from 1.', in: 'query', required: false)]
  43. #[OA\Parameter(name: 'limit', description: 'Number of items per page.', in: 'query', required: false)]
  44. #[OA\Parameter(name: 'sort', description: 'Sort search results by price or area.', in: 'query', required: false)]
  45. #[OA\Parameter(name: 'direction', description: 'Ascending (A to Z, 0 to 9), Descending (Z to A, 9 to 0)', in: 'query', required: false)]
  46. #[OA\Parameter(name: 'keywordSearch', description: "Search by keyword in listing's title, description and address", in: 'query', required: false)]
  47. #[OA\Parameter(name: 'unitOnly', description: 'Get compound units only', in: 'query', required: false)]
  48. #[OA\Response(response: 500, description: 'Returned when something went wrong, for example if you entered non existing propertyType ID')]
  49. public function getSearchListings(Request $request, CacheInterface $cache): Response
  50. {
  51. $criteria = $this->builderDirector->build($request)->getResult();
  52. $locations = explode(',', $request->query->get('location', ''));
  53. $request->query->set('locations', $locations);
  54. if (!empty($criteria['keywordSearch'])) {
  55. $this->eventDispatcher->dispatch(new SearchTriggerEvent($request));
  56. }
  57. if ($this->getUser() instanceof User) {
  58. $this->messageBus->dispatch(new Search($request->query, $this->getUser()));
  59. }
  60. $criteria['status'] = ListingStatus::LIVE;
  61. $cacheKey = sprintf('api_v4_listings_search_%s_%s', $request->getLocale(), md5(http_build_query($request->query->all())));
  62. $cachedSerialize = $cache->getItem($cacheKey);
  63. if (!$cachedSerialize->isHit() || empty($cachedSerialize->get())) {
  64. $result = $this->mediator->start($criteria)->getResults();
  65. $mapped = new ListingDataMapper();
  66. $mapped->setMapListingAttributes(true);
  67. $mapped->setMapListingMainPhoto(true);
  68. $mapped->setMapListingPhotos(true);
  69. $data = [
  70. 'default' => $this->makeMappedPaginatedBody($result['searchResults'], $mapped),
  71. 'related' => [],
  72. ];
  73. $data = $this->serializer->serialize(
  74. $data,
  75. 'json',
  76. SerializationContext::create()
  77. ->setGroups('SearchV4')
  78. ->enableMaxDepthChecks()
  79. );
  80. $cachedSerialize->set($data);
  81. $cachedSerialize->expiresAfter(3600 * 3);
  82. $cache->save($cachedSerialize);
  83. }
  84. return new Response($cachedSerialize->get());
  85. }
  86. /**
  87. * Related Listings Search V4.
  88. */
  89. #[Rest\Get('/api/v4/listings/search/related', name: 'aqarmap_api_get_related_results_v4', options: ['i18n' => false])]
  90. #[Rest\QueryParam(name: 'propertyType', requirements: '\d+', default: null, description: 'Property Type ID')]
  91. #[Rest\QueryParam(name: 'location', requirements: '\d+', default: null, description: 'Location ID comma spereted')]
  92. #[Rest\QueryParam(name: 'section', requirements: '\d+', default: null, description: 'Section ID')]
  93. #[Rest\QueryParam(name: 'page', requirements: '\d+', default: 1, description: 'Page number, starting from 1.', nullable: true)]
  94. #[Rest\QueryParam(name: 'limit', requirements: '\d+', default: 10, description: 'Number of items per page.', nullable: true)]
  95. #[Rest\QueryParam(name: 'sort', requirements: 'price|area', default: null, description: 'Sort search results by price or area.', nullable: true)]
  96. #[Rest\QueryParam(name: 'direction', requirements: 'asc|desc', default: 'asc', description: 'Ascending (A to Z, 0 to 9), Descending (Z to A, 9 to 0)', nullable: true)]
  97. #[Rest\View(serializerGroups: ['DefaultV4', 'SearchV4'])]
  98. #[OA\Parameter(name: 'propertyType', description: 'Property Type ID', in: 'query', required: false)]
  99. #[OA\Parameter(name: 'location', description: 'Location ID comma seperated', in: 'query', required: false)]
  100. #[OA\Parameter(name: 'section', description: 'Section ID', in: 'query', required: false)]
  101. #[OA\Parameter(name: 'page', description: 'Page number, starting from 1.', in: 'query', required: false)]
  102. #[OA\Parameter(name: 'limit', description: 'Number of items per page.', in: 'query', required: false)]
  103. #[OA\Parameter(name: 'sort', description: 'Sort search results by price or area.', in: 'query', required: false)]
  104. #[OA\Parameter(name: 'direction', description: 'Ascending (A to Z, 0 to 9), Descending (Z to A, 9 to 0)', in: 'query', required: false)]
  105. #[OA\Response(response: 500, description: 'Returned when something went wrong, for example if you entered non existing propertyType ID')]
  106. public function getRelatedSearchListings(Request $request, HttpClientInterface $searchClient, SerializerInterface $jmsSerializer, ListingRepository $listingRepository): JsonResponse
  107. {
  108. $response = $searchClient->request('GET', '/api/listing/nearest', [
  109. 'query' => $request->query->all(),
  110. 'headers' => [
  111. 'Accept-Language' => $request->getPreferredLanguage(),
  112. ],
  113. ]);
  114. if (200 === $response->getStatusCode()) {
  115. $data = $response->toArray();
  116. if (isset($data['data']) && is_array($data['data'])) {
  117. foreach ($data['data'] as &$item) {
  118. if (isset($item['listings']) && is_array($item['listings'])) {
  119. $listings = $listingRepository->getListingsByIds(array_column($item['listings'], 'id'))->getQuery()->getResult();
  120. $context = SerializationContext::create()
  121. ->setGroups(['DefaultV4', 'SearchV4']);
  122. $item['listings'] = json_decode(
  123. $jmsSerializer->serialize($listings, 'json', $context),
  124. true
  125. );
  126. }
  127. }
  128. }
  129. return $this->json($data);
  130. }
  131. return $this->json([
  132. 'error' => 'Failed to fetch nearest search listings',
  133. ], $response->getStatusCode());
  134. }
  135. #[Rest\Get('/api/v4/listings', name: 'aqarmap_api_get_listings', options: ['i18n' => false])]
  136. #[Rest\Get('/api/v4/listings/search', name: 'aqarmap_api_listings_search_v4', options: ['i18n' => false])]
  137. #[OA\Get(summary: 'Search listings', description: 'Returns listing search results for the provided filters and pagination settings.', tags: ['ListingsSearch'])]
  138. #[OA\Parameter(name: 'minPrice', description: 'Minimum listing price.', in: 'query', required: false, schema: new OA\Schema(type: 'integer'))]
  139. #[OA\Parameter(name: 'maxPrice', description: 'Maximum listing price.', in: 'query', required: false, schema: new OA\Schema(type: 'integer'))]
  140. #[OA\Parameter(name: 'minArea', description: 'Minimum listing area.', in: 'query', required: false, schema: new OA\Schema(type: 'integer'))]
  141. #[OA\Parameter(name: 'maxArea', description: 'Maximum listing area.', in: 'query', required: false, schema: new OA\Schema(type: 'integer'))]
  142. #[OA\Parameter(name: 'room', description: 'Exact number of rooms.', in: 'query', required: false, schema: new OA\Schema(type: 'integer'))]
  143. #[OA\Parameter(name: 'minRoom', description: 'Minimum number of rooms.', in: 'query', required: false, schema: new OA\Schema(type: 'integer'))]
  144. #[OA\Parameter(name: 'bounds', description: 'Map bounds in south-west/north-east latitude-longitude format.', in: 'query', required: false, schema: new OA\Schema(type: 'string'))]
  145. #[OA\Parameter(name: 'marketPropertyType', description: 'Market-specific property type filter.', in: 'query', required: false, schema: new OA\Schema(type: 'string'))]
  146. #[OA\Parameter(name: 'excludedCategory', description: 'Category identifier to exclude from results.', in: 'query', required: false, schema: new OA\Schema(type: 'string'))]
  147. #[OA\Parameter(name: 'trackTotalHits', description: 'Whether to request exact total hit tracking from the search backend.', in: 'query', required: false, schema: new OA\Schema(type: 'boolean'))]
  148. #[OA\Parameter(name: 'scoredebug', description: 'Include search scoring debug details when permitted.', in: 'query', required: false, schema: new OA\Schema(type: 'boolean'))]
  149. #[OA\Parameter(name: 'esdebug', description: 'Include Elasticsearch debug details when permitted.', in: 'query', required: false, schema: new OA\Schema(type: 'boolean'))]
  150. #[OA\Parameter(name: 'limit', description: 'Maximum number of listings returned per page.', in: 'query', required: false, schema: new OA\Schema(type: 'integer'))]
  151. #[OA\Parameter(name: 'sort', description: 'Sorting field for the result set.', in: 'query', required: false, schema: new OA\Schema(type: 'string'))]
  152. #[OA\Parameter(name: 'aggregatedField', description: 'Aggregation field requested from the search backend.', in: 'query', required: false, schema: new OA\Schema(type: 'string'))]
  153. #[OA\Parameter(name: 'creditExpiryDate', description: 'Credit expiry date filter.', in: 'query', required: false, schema: new OA\Schema(type: 'string', format: 'date'))]
  154. #[Rest\QueryParam(name: 'personalizedSearch', requirements: '(1)|(0)', default: '0', description: 'Enable personalized search', strict: true, nullable: true)]
  155. #[OA\Parameter(name: 'personalizedSearch', description: 'Enable personalized search (requires an authenticated user with an active subscription).', in: 'query', required: false, schema: new OA\Schema(type: 'string', enum: ['0', '1']))]
  156. #[OA\Parameter(name: 'personalizedForUser', description: 'Internal user identifier used to scope personalized results.', in: 'query', required: false, schema: new OA\Schema(type: 'integer'))]
  157. #[OA\Parameter(name: 'status', description: 'Listing status filter.', in: 'query', required: false, schema: new OA\Schema(type: 'string'))]
  158. #[OA\Parameter(name: 'location', description: 'Comma-separated location identifiers.', in: 'query', required: false, schema: new OA\Schema(type: 'string'))]
  159. #[OA\Parameter(name: 'propertyType', description: 'Property type identifier.', in: 'query', required: false, schema: new OA\Schema(type: 'integer'))]
  160. #[OA\Parameter(name: 'section', description: 'Section identifier.', in: 'query', required: false, schema: new OA\Schema(type: 'integer'))]
  161. #[OA\Parameter(name: 'excludedListingsIds', description: 'Comma-separated listing identifiers to exclude from results.', in: 'query', required: false, schema: new OA\Schema(type: 'string'))]
  162. #[Rest\QueryParam(name: 'amenities', description: 'A list of amenities to filter by.', map: true, nullable: true)]
  163. #[OA\Parameter(name: 'amenities', description: 'Filter by a list of amenities. Can be provided as an array.', in: 'query', required: false, schema: new OA\Schema(type: 'array', items: new OA\Items(type: 'string', enum: Amenities::ALL)))]
  164. #[Rest\QueryParam(name: 'downPayment', requirements: '\d+', default: null, description: 'Filter by maximum down payment amount', nullable: true)]
  165. #[OA\Parameter(name: 'downPayment', description: 'Filter by maximum down payment amount', in: 'query', required: false, schema: new OA\Schema(type: 'integer'))]
  166. #[Rest\QueryParam(name: 'duration', requirements: '\d+', default: null, description: 'Filter by minimum installment duration (in days)', nullable: true)]
  167. #[OA\Parameter(name: 'duration', description: 'Filter by minimum installment duration (in days)', in: 'query', required: false, schema: new OA\Schema(type: 'integer'))]
  168. #[OA\Response(response: 200, description: 'Paginated listing search results.')]
  169. #[OA\Response(response: 400, description: 'Invalid search request, such as a page number above the supported limit.')]
  170. #[OA\Response(response: 401, description: 'Returned when personalized search is requested without an authenticated user.')]
  171. #[OA\Response(response: 403, description: 'Returned when personalized search is requested without an active subscription.')]
  172. public function getListings(Request $request, ListingRepository $listingRepository, ListingManager $listingManager, CacheInterface $cache, MessageBusInterface $messageBus)
  173. {
  174. // This to prevent the "result window is too large" exception in ElasticSearch.
  175. if ($request->get('page', 1) > 500) {
  176. throw new BadRequestHttpException('Page number must be less than or equal to 500');
  177. }
  178. /** @var UserInterface $user */
  179. $user = $this->getUser();
  180. $hasSearchScoringRole = $user && $user->hasRole('ROLE_SEARCH_SCORING');
  181. if (!$hasSearchScoringRole && ($request->get('esdebug') || $request->get('scoredebug'))) {
  182. $request->query->remove('esdebug');
  183. $request->query->remove('scoredebug');
  184. }
  185. if ($request->get('location')) {
  186. $request->query->set('locations', explode(',', $request->query->get('location', '')));
  187. }
  188. $personalizedSearch = $request->query->getBoolean('personalizedSearch', false);
  189. if ($personalizedSearch) {
  190. if (!$user instanceof User) {
  191. throw new UnauthorizedHttpException('Personalized search requires an active session. Please log in.');
  192. }
  193. if (!$user->hasSubscriptionPlan()) {
  194. throw new AccessDeniedHttpException('Active subscription required for personalized search.');
  195. }
  196. $request->query->set('personalizedSearch', true);
  197. $request->query->set('personalizedForUser', $user->getId());
  198. }
  199. if ($personalizedSearch) {
  200. $data = $this->getSerializedListingSearchResults($request, $listingRepository, $listingManager);
  201. } else {
  202. $cacheKey = sprintf('api_listings_search_v4_%s_%s', $request->getLocale(), md5(http_build_query($request->query->all())));
  203. $data = $cache->get(
  204. $cacheKey,
  205. function(ItemInterface $item) use ($request, $listingRepository, $listingManager) {
  206. $item->expiresAfter(3600 * 3);
  207. return $this->getSerializedListingSearchResults($request, $listingRepository, $listingManager);
  208. }
  209. );
  210. }
  211. return new Response($data);
  212. }
  213. private function getSerializedListingSearchResults(Request $request, ListingRepository $listingRepository, ListingManager $listingManager): string
  214. {
  215. $listingsElasticResponse = $listingManager->getListingsElasticResponse($request);
  216. $listingsQueryBuilder = $listingRepository->getListingsByIds(array_column($listingsElasticResponse['items'], 'id'));
  217. if ('aqarmap_api_listings_search_v4' === $request->attributes->get('_route')) {
  218. $listingsElasticResponse['pagination']['totalPages'] = (float) $listingsElasticResponse['pagination']['totalPages'];
  219. }
  220. $listings = $listingsQueryBuilder->getQuery()->getResult();
  221. $data = [
  222. 'default' => $this->mapPaginatedBody($listingsElasticResponse, $listings),
  223. 'related' => [],
  224. ];
  225. return $this->serializer->serialize(
  226. $data,
  227. 'json',
  228. SerializationContext::create()
  229. ->setGroups('SearchV4')
  230. ->enableMaxDepthChecks()
  231. );
  232. }
  233. #[Rest\Get('/api/v4/listings/debug', options: ['i18n' => false], name: 'aqarmap_api_get_listings_debug')]
  234. public function getListingsEsDebug(Request $request, ListingManager $listingManager)
  235. {
  236. /** @var UserInterface $user */
  237. $user = $this->getUser();
  238. if (!($user && $user->hasRole('ROLE_SEARCH_SCORING'))) {
  239. throw new UnauthorizedHttpException();
  240. }
  241. $request->query->set('esdebug', 1);
  242. if ($request->get('location')) {
  243. $request->query->set('locations', explode(',', $request->query->get('location', '')));
  244. }
  245. $listingsDebugElasticResponse = $listingManager->getListingsDebugElasticResponse($request);
  246. if ('aqarmap_api_listings_search_v4' === $request->attributes->get('_route')) {
  247. $listingsDebugElasticResponse['pagination']['totalPages'] = (float) $listingsDebugElasticResponse['pagination']['totalPages'];
  248. }
  249. $data = [
  250. 'default' => $this->mapPaginatedBody($listingsDebugElasticResponse, $listingsDebugElasticResponse['items']),
  251. ];
  252. return new JsonResponse($data);
  253. }
  254. #[Rest\Get('/api/v4/listings/search/ssr-data', name: 'aqarmap_api_get_listings_search_ssr-data', options: ['i18n' => false])]
  255. #[OA\Get(
  256. summary: 'Get listing search SSR data',
  257. description: 'Provides the pre-rendered content required for the listing search SSR page such as location hierarchy, SEO metadata and UI chips.',
  258. tags: ['ListingsSearch']
  259. )]
  260. #[OA\Parameter(name: 'location', in: 'query', required: false, description: 'Slug of the primary location to load contextual data for.', schema: new OA\Schema(type: 'string'))]
  261. #[OA\Parameter(
  262. name: 'locations[]',
  263. in: 'query',
  264. required: false,
  265. description: 'Optional list of location slugs used to resolve additional data.',
  266. schema: new OA\Schema(type: 'array', items: new OA\Items(type: 'string'))
  267. )]
  268. #[OA\Parameter(name: 'section', in: 'query', required: false, description: 'Section slug to scope the returned data.', schema: new OA\Schema(type: 'string'))]
  269. #[OA\Parameter(name: 'propertyType', in: 'query', required: false, description: 'Property type slug to scope the returned data.', schema: new OA\Schema(type: 'string'))]
  270. #[OA\Parameter(name: 'byOwnerOnly', in: 'query', required: false, description: 'Whether to limit content to owner only listings.', schema: new OA\Schema(type: 'boolean'))]
  271. #[OA\Response(
  272. response: 200,
  273. description: 'Successful response containing SSR data required by the listing search page.',
  274. content: new OA\JsonContent(
  275. type: 'object',
  276. properties: [
  277. new OA\Property(
  278. property: 'locationChildren',
  279. description: 'Hierarchy of child locations related to the requested location.',
  280. type: 'array',
  281. items: new OA\Items(type: 'object')
  282. ),
  283. new OA\Property(
  284. property: 'longTail',
  285. description: 'SEO long-tail content blocks generated for the section, property type and location.',
  286. type: 'array',
  287. items: new OA\Items(type: 'object')
  288. ),
  289. new OA\Property(
  290. property: 'customParagraph',
  291. description: 'Custom paragraph content tailored to the current search context.',
  292. type: 'array',
  293. items: new OA\Items(type: 'object')
  294. ),
  295. new OA\Property(
  296. property: 'slugResolver',
  297. description: 'Resolved slugs mapping for the provided filters.',
  298. type: 'array',
  299. items: new OA\Items(type: 'object')
  300. ),
  301. new OA\Property(
  302. property: 'faqData',
  303. description: 'Frequently asked questions related to the current search.',
  304. type: 'array',
  305. items: new OA\Items(type: 'object')
  306. ),
  307. new OA\Property(
  308. property: 'locationParents',
  309. description: 'Ordered list of parent locations for breadcrumb generation.',
  310. type: 'array',
  311. items: new OA\Items(type: 'object')
  312. ),
  313. new OA\Property(
  314. property: 'sections',
  315. description: 'Available sections that can be displayed in the SSR page.',
  316. type: 'array',
  317. items: new OA\Items(type: 'object')
  318. ),
  319. new OA\Property(
  320. property: 'propertyTypeChips',
  321. description: 'Property type chips rendered in the search interface.',
  322. type: 'array',
  323. items: new OA\Items(type: 'object')
  324. ),
  325. ]
  326. )
  327. )]
  328. #[OA\Response(response: 500, description: 'Returned when SSR data could not be generated or cached.')]
  329. public function getListingsSearchSSRData(Request $request, ListingManager $listingManager, CacheInterface $cache, SectionService $sectionService)
  330. {
  331. $cacheKey = sprintf('api_listings_search_ssr_data_%s_%s', $request->getLocale(), md5(http_build_query($request->query->all())));
  332. $cachedResponse = $cache->getItem($cacheKey);
  333. if (!$cachedResponse->isHit() || empty($cachedResponse->get())) {
  334. $customParagraph = $locationChildren = $slugResolver = $faqData = $locationParents = $longTail = [];
  335. if ($request->get('location')) {
  336. $request->query->set('locations', explode(',', $request->query->get('location', '')));
  337. }
  338. $location = $request->query->all('locations') ? $request->query->all('locations')[0] : null;
  339. $location = $this->locationRepository->findOneBy(['slug' => $location]);
  340. $section = $this->sectionRepository->findOneBy(['slug' => $request->query->get('section')]);
  341. $propertyType = $this->propertyTypeRepository->findOneBy(['slug' => $request->query->get('propertyType')]);
  342. if ($location) {
  343. $locationChildren = $listingManager->getSerializedLocationChildren($location);
  344. $locationParents = $listingManager->getLocationParents($location, $request->getLocale());
  345. }
  346. if ($section && $propertyType && $location) {
  347. $longTail = $this->seoListingSearchService->getSerializedLongTailData($section, $propertyType, $location) ?? [];
  348. }
  349. if ($section) {
  350. $slugResolver = $listingManager->getSerializedResolvedSlugs($section, $propertyType, $request->query->all('locations'));
  351. }
  352. if ($section && $propertyType) {
  353. $byOwnerOnly = filter_var($request->get('byOwnerOnly'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
  354. $customParagraph = $listingManager->getSerializedCustomParagraph($section, $propertyType, $location, $byOwnerOnly) ?? [];
  355. $faqData = $listingManager->getFaqs($section, $propertyType, $location, $request->getLocale());
  356. }
  357. $response = [
  358. 'locationChildren' => !empty($locationChildren) ? json_decode((string) $locationChildren, true) : [],
  359. 'longTail' => !empty($longTail) ? json_decode((string) $longTail, true) : [],
  360. 'customParagraph' => !empty($customParagraph) ? json_decode((string) $customParagraph, true) : [],
  361. 'slugResolver' => !empty($slugResolver) ? json_decode((string) $slugResolver, true) : [],
  362. 'faqData' => $faqData,
  363. 'locationParents' => $locationParents,
  364. 'sections' => json_decode((string) $sectionService->getSerializedSections(), true),
  365. 'propertyTypeChips' => $listingManager->getListingPropertyTypesChips($request, $location, $section, $propertyType),
  366. ];
  367. $cachedResponse->set(json_encode($response));
  368. $cachedResponse->expiresAfter(3600 * 3);
  369. $cache->save($cachedResponse);
  370. }
  371. return new JsonResponse(json_decode((string) $cachedResponse->get(), true));
  372. }
  373. #[Rest\Get('/api/v4/listings/trigger-search', options: ['i18n' => false], name: 'aqarmap_api_trigger_search_listings_v4')]
  374. #[OA\Get(summary: 'Trigger listings search tracking', description: 'Dispatches search tracking and background processing events for the current listing search request.', tags: ['ListingsSearch'])]
  375. #[OA\Parameter(name: 'location', in: 'query', required: false, description: 'Comma-separated location identifiers to normalize into the locations filter.', schema: new OA\Schema(type: 'string'))]
  376. #[OA\Parameter(name: 'keywordSearch', in: 'query', required: false, description: 'Keyword query that triggers the search event tracking flow.', schema: new OA\Schema(type: 'string'))]
  377. #[OA\Response(response: 200, description: 'Search trigger event dispatched successfully.')]
  378. public function triggerSearchListings(Request $request, MessageBusInterface $messageBus)
  379. {
  380. $user = $this->getUser();
  381. if ($request->get('location')) {
  382. $request->query->set('locations', explode(',', $request->query->get('location', '')));
  383. }
  384. if ($request->get('keywordSearch')) {
  385. $this->eventDispatcher->dispatch(new SearchTriggerEvent($request));
  386. }
  387. if ($user) {
  388. $messageBus->dispatch(new Search($request->query, $user));
  389. }
  390. return new JsonResponse([
  391. 'statusCode' => Response::HTTP_OK,
  392. 'statusMessage' => 'Search triggered successfully!',
  393. ]);
  394. }
  395. private function mapPaginatedBody(array $result, array $listings): array
  396. {
  397. if (!$result['pagination']) {
  398. return [
  399. 'statusCode' => $this->getStatusCode(),
  400. 'statusMessage' => $this->getStatusMessage(),
  401. 'paginate' => [],
  402. 'data' => [],
  403. 'errors' => $this->getErrors(),
  404. ];
  405. }
  406. return [
  407. 'statusCode' => $this->getStatusCode(),
  408. 'statusMessage' => $this->getStatusMessage(),
  409. 'paginate' => $result['pagination'],
  410. 'data' => $listings,
  411. 'errors' => $this->getErrors(),
  412. ];
  413. }
  414. }