Skip to content

Commit

Permalink
Merge pull request #13 from EscolaLMS/feature/START-40
Browse files Browse the repository at this point in the history
Feature/start 40
  • Loading branch information
HerbertIV authored Feb 23, 2022
2 parents 40ef2e6 + 76b92db commit 9a05422
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"escolalms/categories": "^0",
"escolalms/jitsi": "^0",
"laravel/framework": ">=8.0",
"escolalms/cart": "^0"
"escolalms/cart": "0.1.9"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
Expand Down
15 changes: 15 additions & 0 deletions src/Http/Controllers/ConsultationAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ public function index(ListConsultationsRequest $listConsultationsRequest): JsonR
);
}

public function forUser(ListConsultationsRequest $listConsultationsRequest): JsonResponse
{
$search = $listConsultationsRequest->except(['limit', 'skip', 'order', 'order_by']);
$consultations = $this->consultationServiceContract
->getConsultationsListForUser($search)
->paginate(
$listConsultationsRequest->get('per_page') ??
config('escolalms_consultations.perPage', ConstantEnum::PER_PAGE)
);

return $this->sendResponseForResource(
ConsultationSimpleResource::collection($consultations), __('Consultations retrieved successfully')
);
}

public function reportTerm(int $orderItemId, ReportTermConsultationRequest $request): JsonResponse
{
$this->consultationServiceContract->reportTerm($orderItemId, $request->input('term'));
Expand Down
2 changes: 0 additions & 2 deletions src/Models/Consultation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace EscolaLms\Consultations\Models;

use EscolaLms\Auth\Models\User;
use EscolaLms\Cart\Contracts\Base\BuyableTrait;
use EscolaLms\Cart\Models\OrderItem;
use EscolaLms\Categories\Models\Category;
use EscolaLms\Consultations\Database\Factories\ConsultationFactory;
Expand Down Expand Up @@ -114,7 +113,6 @@
class Consultation extends Model
{
use HasFactory;
use BuyableTrait;

protected $fillable = [
'base_price',
Expand Down
12 changes: 12 additions & 0 deletions src/Repositories/ConsultationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use EscolaLms\Consultations\Repositories\Contracts\ConsultationRepositoryContract;
use EscolaLms\Core\Repositories\BaseRepository;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\JoinClause;

class ConsultationRepository extends BaseRepository implements ConsultationRepositoryContract
{
Expand All @@ -30,6 +31,17 @@ public function allQueryBuilder(array $search = [], array $criteria = []): Build
return $query;
}

public function forUser(array $search = [], array $criteria = []): Builder
{
$q = $this->allQueryBuilder($search, $criteria);
$q->whereHas('orderItems', function ($query) {
return $query
->leftJoin('orders', 'orders.id', '=', 'order_items.order_id')
->where('orders.user_id', '=', auth()->user()->getKey());
});
return $q;
}

public function updateModel(Consultation $consultation, array $data): Consultation
{
$consultation->fill($data);
Expand Down
12 changes: 12 additions & 0 deletions src/Services/ConsultationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ public function getConsultationsList(array $search = [], bool $onlyActive = fals
);
}

public function getConsultationsListForUser(array $search = []): Builder
{
$now = now()->format('Y-m-d');
$search['active_to'] = $search['active_to'] ?? $now;
$search['active_from'] = $search['active_from'] ?? $now;
$criteria = FilterListDto::prepareFilters($search);
return $this->consultationRepositoryContract->forUser(
$search,
$criteria
);
}

public function store(ConsultationDto $consultationDto): Consultation
{
return DB::transaction(function () use($consultationDto) {
Expand Down
1 change: 1 addition & 0 deletions src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// user endpoints
Route::group(['middleware' => ['auth:api'], 'prefix' => 'api/consultations'], function () {
Route::get('/', [ConsultationAPIController::class, 'index']);
Route::get('/me', [ConsultationAPIController::class, 'forUser']);
Route::get('/{id}', [ConsultationController::class, 'show']);
Route::post('/report-term/{orderItemId}', [ConsultationAPIController::class, 'reportTerm']);
Route::get('/proposed-terms/{orderItemId}', [ConsultationAPIController::class, 'proposedTerms']);
Expand Down
1 change: 1 addition & 0 deletions tests/APIs/ConsultationApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ public function testConsultationsListUnauthorized(): void
$this->response = $this->json('GET','/api/admin/consultations');
$this->response->assertUnauthorized();
}

}
81 changes: 81 additions & 0 deletions tests/APIs/ConsultationListForUserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace EscolaLms\Consultations\Tests\APIs;

use EscolaLms\Cart\Models\Order;
use EscolaLms\Cart\Models\OrderItem;
use EscolaLms\Cart\Models\User;
use EscolaLms\Consultations\Database\Seeders\ConsultationsPermissionSeeder;
use EscolaLms\Consultations\Models\Consultation;
use EscolaLms\Consultations\Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Collection;
use Illuminate\Testing\Fluent\AssertableJson;

class ConsultationListForUserTest extends TestCase
{
use DatabaseTransactions;
private string $apiUrl;
private Collection $consultations;

protected function setUp(): void
{
parent::setUp();
$this->seed(ConsultationsPermissionSeeder::class);

$this->user = User::factory()->create();
$this->user->guard_name = 'api';
$this->user->assignRole('tutor');
$this->apiUrl = 'api/consultations/me';
}

private function initVariable(): void
{
$this->consultations = Consultation::factory(3)->create();
$price = $this->consultations->reduce(fn ($acc, Consultation $consultation) => $acc + $consultation->getBuyablePrice(), 0);
$this->order = Order::factory()->afterCreating(
fn (Order $order) => $order->items()->saveMany(
$this->consultations->map(
function (Consultation $consultation) {
return OrderItem::query()->make([
'quantity' => 1,
'buyable_id' => $consultation->getKey(),
'buyable_type' => Consultation::class,
]);
}
)
)
)->create([
'user_id' => $this->user->getKey(),
'total' => $price,
'subtotal' => $price,
]);
}

public function testConsultationListForUser(): void
{
$this->initVariable();
$this->response = $this->actingAs($this->user, 'api')->json('GET', $this->apiUrl);
$consArray = $this->consultations->pluck('id')->toArray();
$this->response->assertJson(fn (AssertableJson $json) => $json->has(
'data',
fn ($json) => $json->each(fn (AssertableJson $json) =>
$json->where('id', fn ($json) =>
in_array($json, $consArray)
)
->etc()
)
->etc()
)
->etc()
);
$this->response->assertOk();
}

public function testConsultationReportTermUnauthorized(): void
{
$this->initVariable();
$this->response = $this->json('GET', $this->apiUrl);
$this->response->assertUnauthorized();
}
}

0 comments on commit 9a05422

Please sign in to comment.