public function testGettingSubscribedAccountsForSpecifiedAccount()
 {
     $account = factory(Account::class)->create([]);
     $mock = m::mock('Modules\\Account\\AccountManager');
     $mock->shouldNotReceive('account');
     $base = factory(Account::class)->create([]);
     $repo = m::mock('Modules\\Account\\AccountRepositoryInterface');
     $repo->shouldReceive('baseAccount')->andReturn($base);
     //if we dont provide account, it should use the environment one.
     $manager = new GammaSubscriptionManager($mock, $repo);
     $response = $manager->subscribedAccounts($account);
     $this->assertInstanceOf(Collection::class, $response);
     $this->assertSameSize($response, [$base, $account]);
     $this->assertSame($base->id, $response->get(0)->id);
     $this->assertSame($account->id, $response->get(1)->id);
 }
Exemplo n.º 2
0
 /**
  * @param GammaSubscriptionManager $subscriptions
  * @return string
  */
 protected function indexesToUse(GammaSubscriptionManager $subscriptions)
 {
     $accounts = $subscriptions->subscribedAccounts();
     $aliases = $accounts->lists('alias')->toArray();
     return implode(',', $aliases);
 }
Exemplo n.º 3
0
 /**
  * @param GammaSelection $gamma
  * @param GammaNotification $notification
  * @param GammaSubscriptionManager $subscriptions
  * @param Request $request
  * @return Collection
  */
 public function brands(GammaSelection $gamma, GammaNotification $notification, GammaSubscriptionManager $subscriptions, Request $request)
 {
     $productRequirements = function ($query) use($subscriptions) {
         $query->whereIn('account_id', $subscriptions->subscribedIds());
         //also make sure the products are actually linked to a category
         $query->join('product_categories_pivot', 'product_categories_pivot.product_id', '=', 'products.id');
     };
     $categoryRequirements = function ($query) use($subscriptions) {
         $query->whereIn('account_id', $subscriptions->subscribedIds());
     };
     $brands = Brand::whereHas('products', $productRequirements);
     //if we passed in a brand, we used the suggest to find a brand.
     if ($brand = $request->get('brand')) {
         $brand = Brand::find($brand);
         if ($brand) {
             $brands->where('id', $brand->id);
         }
     }
     if ($brand) {
         $brands = $brands->paginate(5, ['*'], 'page', $page = 1);
     } else {
         $brands = $brands->paginate(5);
     }
     $ids = $brands->lists('id')->toArray();
     if (!count($ids)) {
         return new Collection();
     }
     $brands->load(['translations', 'selection']);
     foreach ($brands as $brand) {
         $brand->load(['categories' => function ($query) use($subscriptions, $brand) {
             $query->join('product_categories_pivot', 'product_categories_pivot.category_id', '=', 'product_categories.id')->join('products', 'products.id', '=', 'product_categories_pivot.product_id')->whereIn('products.account_id', $subscriptions->subscribedIds())->where('products.brand_id', $brand->id)->distinct(['product_categories.*'])->get(['product_categories.*']);
         }, 'categories.translations', 'categories.selection']);
     }
     $selections = $this->selections($gamma, 'brand_id', $ids);
     $reviews = $this->reviews($notification, 'brand_id', $ids);
     //use foreach instead of map, so we can reuse the original paginator.
     foreach ($brands as $key => $brand) {
         $brand->activated = $brand->selection ? true : false;
         $brand->selection = null;
         $cSelections = $selections->get($brand->id);
         $cReviews = $reviews->get($brand->id);
         $brand->categories = $brand->categories->map(function ($category) use($cSelections, $cReviews) {
             $category->activated = $category->selection ? true : false;
             $category->selection = null;
             $inReview = $cReviews && $cReviews->contains('category_id', $category->id);
             $actuallySelected = $cSelections && $cSelections->contains('category_id', $category->id);
             $category->selected = $actuallySelected && !$inReview || !$actuallySelected && $inReview;
             $category->inReview = $inReview;
             return $category;
         });
         $brands[$key] = $brand;
     }
     return $brands;
 }