Skip to content

Commit

Permalink
feature: include pockets in app
Browse files Browse the repository at this point in the history
  • Loading branch information
mascam97 committed Dec 18, 2023
1 parent dcdec22 commit c5116de
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 6 deletions.
29 changes: 29 additions & 0 deletions src/App/Api/Pockets/Resources/PocketResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Api\Pockets\Resources;

use Domain\Pockets\Models\Pocket;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
* @mixin Pocket;
*/
class PocketResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param Request $request
*/
public function toArray($request): array
{
return [
'id' => $this->id,
'balance' => $this->balance,
'currency' => $this->currency,
'updated_at' => $this->updated_at,
'created_at' => $this->created_at,
];
}
}
2 changes: 2 additions & 0 deletions src/App/Api/Profile/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function show(Request $request): ProfileResource
{
/** @var User $authUser */
$authUser = $request->user();
// TODO: use include=pocket as http query parameter to load pocket
$authUser->load('pocket');

return ProfileResource::make($authUser);
}
Expand Down
2 changes: 2 additions & 0 deletions src/App/Api/Profile/Resources/ProfileResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Api\Profile\Resources;

use App\Api\Pockets\Resources\PocketResource;
use Domain\Users\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
Expand All @@ -24,6 +25,7 @@ public function toArray($request): array
'email' => $this->email,
'locale' => $this->locale,
'sex' => $this->sex,
'pocket' => PocketResource::make($this->whenLoaded('pocket')),
'updated_at' => (string) $this->updated_at,
'created_at' => (string) $this->created_at,
];
Expand Down
2 changes: 2 additions & 0 deletions src/App/Api/Users/Controllers/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function __invoke(UserRequest $request): JsonResponse
$user->email_subscribed_at = now();
$user->save();

// TODO: Create pocket when user is created

Log::channel('daily')->info('New user was created.', ['email' => $user->email]);

(new SendWelcomeEmailAction())->onQueue()->execute($user);
Expand Down
30 changes: 30 additions & 0 deletions src/App/ApiAdmin/Pockets/Resources/PocketResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\ApiAdmin\Pockets\Resources;

use Domain\Pockets\Models\Pocket;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
* @mixin Pocket;
*/
class PocketResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param Request $request
*/
public function toArray($request): array
{
return [
'id' => $this->id,
'balance' => $this->balance,
'currency' => $this->currency,
'updated_at' => $this->updated_at,
'created_at' => $this->created_at,
'deleted_at' => $this->deleted_at,
];
}
}
4 changes: 3 additions & 1 deletion src/App/ApiAdmin/Users/Queries/UserIndexQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function __construct(Request $request)
'id',
'name',
'email',
'pocket_id',
'deleted_at',
'created_at',
'updated_at',
Expand All @@ -23,7 +24,8 @@ public function __construct(Request $request)
parent::__construct($query, $request);

$this->allowedFilters(['id', 'name', AllowedFilter::trashed()])
->allowedIncludes(['permissions', 'roles'])
// TODO: Support error when there is no pocket
->allowedIncludes(['permissions', 'roles', 'pocket'])
->defaultSort('created_at')
->allowedSorts('id', 'name', 'created_at');
}
Expand Down
2 changes: 1 addition & 1 deletion src/App/ApiAdmin/Users/Queries/UserShowQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public function __construct(Request $request)

parent::__construct($query, $request);

$this->allowedIncludes(['permissions', 'roles']);
$this->allowedIncludes(['permissions', 'roles', 'pocket']);
}
}
2 changes: 2 additions & 0 deletions src/App/ApiAdmin/Users/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\ApiAdmin\Users\Resources;

use App\ApiAdmin\Permissions\Resources\PermissionResource;
use App\ApiAdmin\Pockets\Resources\PocketResource;
use App\ApiAdmin\Roles\Resources\RoleResource;
use Domain\Users\Models\User;
use Illuminate\Http\Request;
Expand All @@ -28,6 +29,7 @@ public function toArray($request): array
'permissions_count' => $this->when($this->permissions_count !== null, $this->permissions_count),
'roles' => RoleResource::collection($this->whenLoaded('roles')),
'roles_count' => $this->when($this->roles_count !== null, $this->roles_count),
'pocket' => new PocketResource($this->whenLoaded('pocket')),
'deleted_at' => (string) $this->deleted_at,
'created_at' => (string) $this->created_at,
'updated_at' => (string) $this->updated_at,
Expand Down
3 changes: 3 additions & 0 deletions src/Domain/Users/Actions/DeleteUserAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class DeleteUserAction
{
public function __invoke(User $user): void
{
// TODO: Delete pocket before user is deleted
// TODO: And validate pocket is empty, without balance and without pending transactions

// TODO: Add unit testing to test database transaction
DB::transaction(function () use ($user) {
// TODO: move to a DeleteQuotesAction and delete its ratings
Expand Down
14 changes: 12 additions & 2 deletions tests/Feature/Api/Profile/ShowProfileTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

use Domain\Pockets\Models\Pocket;
use Domain\Users\Factories\UserFactory;
use Domain\Users\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Testing\Fluent\AssertableJson;
use function Pest\Laravel\getJson;

beforeEach(function () {
$this->user = User::factory()->create();
$this->pocket = Pocket::factory()->create();
$this->user = User::factory()->create(['pocket_id' => $this->pocket->getKey()]);

(new UserFactory)->setAmount(4)->create();

Expand All @@ -24,6 +26,11 @@
->has('email')
->has('locale')
->has('sex')
->has('pocket.id')
->has('pocket.balance')
->has('pocket.currency')
->has('pocket.created_at')
->has('pocket.updated_at')
->has('updated_at')
->has('created_at');
})->etc();
Expand All @@ -35,7 +42,10 @@
getJson(route('api.profile.show'))->assertOk();

expect(formatQueries(DB::getQueryLog()))
->toHaveCount(0);
->toHaveCount(1)
->sequence(
fn ($query) => $query->toContain('select * from `pockets` where `pockets`.`id` in'),
);

DB::disableQueryLog();
});
15 changes: 13 additions & 2 deletions tests/Feature/ApiAdmin/Users/IndexUserTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Domain\Pockets\Models\Pocket;
use Domain\Users\Factories\UserFactory;
use Domain\Users\Models\User;
use Illuminate\Support\Facades\DB;
Expand All @@ -11,9 +12,10 @@
use function PHPUnit\Framework\assertLessThan;

beforeEach(function () {
$this->user = User::factory()->create();
$this->pocket = Pocket::factory()->create();
$this->user = User::factory()->create(['pocket_id' => $this->pocket->getKey()]);

(new UserFactory)->setAmount(4)->create();
(new UserFactory)->setAmount(4)->create(['pocket_id' => $this->pocket->getKey()]);

giveRoleWithPermission($this->user, 'view any users');

Expand Down Expand Up @@ -129,6 +131,15 @@
assertEquals('admin', $responseData[0]['roles'][0]['name']);
});

it('can include pocket', function () {
$responseData = getJson(route('admin.users.index', ['include' => 'pocket']))
->assertOk()
->json('data');

assertArrayHasKey('pocket', $responseData[0]);
assertEquals($this->pocket->getKey(), $responseData[0]['pocket']['id']);
});

test('sql queries optimization test', function () {
DB::enableQueryLog();
getJson(route('admin.users.index'))->assertOk();
Expand Down

0 comments on commit c5116de

Please sign in to comment.