@@ -331,6 +331,33 @@ public function rebuildMaterializedPath(int $barId): void
331331 $ this ->log ->info ('[INGREDIENT_SERVICE] Rebuilt materialized path for ingredients for bar ' . $ barId . ' in ' . round ($ endTime - $ startTime , 6 ) . ' seconds. ' );
332332 }
333333
334+ /**
335+ * Resolve complex ingredients that can be made with the given ingredient IDs.
336+ *
337+ * @param array<int> $ingredientIds
338+ * @return array<int>
339+ */
340+ public function resolveComplexIngredients (array $ ingredientIds ): array
341+ {
342+ return $ this ->db ->table ('complex_ingredients AS ci ' )
343+ ->distinct ()
344+ ->select ('ci.main_ingredient_id ' )
345+ ->join ('ingredients AS i_main ' , 'ci.main_ingredient_id ' , '= ' , 'i_main.id ' )
346+ ->whereIn ('ci.id ' , function ($ query ) use ($ ingredientIds ) {
347+ $ query ->select ('ci_inner.id ' )
348+ ->from ('complex_ingredients AS ci_inner ' )
349+ ->whereNotExists (function ($ query ) use ($ ingredientIds ) {
350+ $ query ->select ('i_ingredient.id ' )
351+ ->from ('complex_ingredients AS ci_sub ' )
352+ ->join ('ingredients AS i_ingredient ' , 'ci_sub.ingredient_id ' , '= ' , 'i_ingredient.id ' )
353+ ->whereColumn ('ci_sub.main_ingredient_id ' , 'ci_inner.main_ingredient_id ' )
354+ ->whereNotIn ('i_ingredient.id ' , $ ingredientIds );
355+ });
356+ })
357+ ->pluck ('main_ingredient_id ' )
358+ ->toArray ();
359+ }
360+
334361 /**
335362 * Return array of all ingredients that are part of the user's shelf
336363 * and can be used to create cocktails.
@@ -366,23 +393,7 @@ public function getMemberIngredients(int $userId, int $barId): array
366393 ->pluck ('id ' )
367394 ->toArray ();
368395
369- $ complexIngredients = $ this ->db ->table ('complex_ingredients AS ci ' )
370- ->distinct ()
371- ->select ('ci.main_ingredient_id ' )
372- ->join ('ingredients AS i_main ' , 'ci.main_ingredient_id ' , '= ' , 'i_main.id ' )
373- ->whereIn ('ci.id ' , function ($ query ) use ($ userIngredientIds ) {
374- $ query ->select ('ci_inner.id ' )
375- ->from ('complex_ingredients AS ci_inner ' )
376- ->whereNotExists (function ($ query ) use ($ userIngredientIds ) {
377- $ query ->select ('i_ingredient.id ' )
378- ->from ('complex_ingredients AS ci_sub ' )
379- ->join ('ingredients AS i_ingredient ' , 'ci_sub.ingredient_id ' , '= ' , 'i_ingredient.id ' )
380- ->whereColumn ('ci_sub.main_ingredient_id ' , 'ci_inner.main_ingredient_id ' )
381- ->whereNotIn ('i_ingredient.id ' , $ userIngredientIds );
382- });
383- })
384- ->pluck ('main_ingredient_id ' )
385- ->toArray ();
396+ $ complexIngredients = $ this ->resolveComplexIngredients ($ userIngredientIds );
386397
387398 return array_unique (array_merge (
388399 $ descendantIngredientIds ,
0 commit comments