Skip to content

Commit

Permalink
Merge pull request #273 from deanblackborough/tweaks-and-improvements
Browse files Browse the repository at this point in the history
Tweaks and improvements
  • Loading branch information
deanblackborough authored Aug 29, 2023
2 parents a9b2838 + f7c2e33 commit 2bfcb74
Show file tree
Hide file tree
Showing 17 changed files with 457 additions and 261 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

The complete changelog for the Costs to Expect REST API, our changelog follows the format defined at https://keepachangelog.com/en/1.0.0/

## [v3.16.0] - 2023-08-29
### Added
- Added a command to prune partial users (bots) from the database.
- Added a `registered_via` field to register to optionally allow recording the App the user registered via.
### Change
- Updated the Budget Pro image on the landing page and added a launching soon badge.
- Updated our dependencies.

## [v3.15.2] - 2023-08-21
### Changed
- Updated the content of all the notifications.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ available on Postman at [postman.costs-to-expect.com](https://postman.costs-to-e
The API is used by the following Apps;

- [Budget](https://budget.costs-to-expect.com) Our free and Open Source Budgeting tool
- [Budget Pro](https://budget-pro.costs-to-expect.com) (alpha) The commercial version of Budget
- [Budget Pro](https://budget-pro.costs-to-expect.com) The commercial version of Budget
- [Expense](https://app.costs-to-expect.com) Our free and Open Source expense tracker
- [Yahtzee Game Scorer](https://yahtzee.game-score.com) Our Yahtzee Game Scorer, free for all to use
- [Yatzy Game Scorer](https://yatzu.game-score.com) Our Yatzy Game Scorer, free for all to use
Expand Down Expand Up @@ -289,7 +289,7 @@ our local test suite is as complete as the Postman request test suite.

| Controller | Action | View |
|:-------------------------|:---------|:---------|
| Authentication | 35 Tests | 3 Tests |
| Authentication | 36 Tests | 3 Tests |
| Category | 21 Tests | 27 Tests |
| Currency | Non yet* | Non yet* |
| ItemCategory | Non yet* | Non yet* |
Expand All @@ -309,7 +309,7 @@ our local test suite is as complete as the Postman request test suite.
| Resource | 24 Tests | 27 Tests |
| ResourceType | 23 Tests | 26 Tests |
| Subcategory | 21 Tests | 23 Tests |
| **Total tests** | **173** | **156** |
| **Total tests** | **174** | **156** |

*Non yet does not mean there are no tests, it just means there are no PHPUnit tests. There are over 2000 tests in
a private Postman collection, I'm slowing transferring them locally and expanding the test suite.
45 changes: 45 additions & 0 deletions app/Console/Commands/PrunePartialUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Console\Commands;

use App\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Console\Command\Command as CommandAlias;

class PrunePartialUsers extends Command
{
protected $signature = 'api:prune-partial-users';

protected $description = 'Remove partial users that have not completed registration';

public function handle()
{
$users = (new User())
->join('password_creates', 'users.email', '=', 'password_creates.email')
->leftJoin('permitted_user', 'users.id', '=', 'permitted_user.user_id')
->where('users.created_at', '<', now()->subDays(7))
->whereNull('permitted_user.user_id')
->get(['users.id', 'users.email']);

foreach ($users as $user) {
$this->info("Preparing to delete user with id: {$user->id} and email: {$user->email}");

try {
DB::transaction(function () use ($user) {
DB::delete('DELETE FROM `password_creates` WHERE `password_creates`.`email` = ?', [$user->email]);
DB::delete('DELETE FROM `permitted_user` WHERE `permitted_user`.`user_id` = ?', [$user->id]);
DB::delete('DELETE FROM `users` WHERE `users`.`id` = ?', [$user->id]);
});
} catch (\Throwable $e) {
$this->error("Failed to delete user with id: {$user->id} and email: {$user->email}");
}

$this->info("Deleted user id: {$user->id}");
}

$this->info("All partial users removed or none to remove");

return CommandAlias::SUCCESS;
}
}
7 changes: 4 additions & 3 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Kernel extends ConsoleKernel
* @var array
*/
protected $commands = [
//
Commands\PrunePartialUsers::class,
];

/**
Expand All @@ -24,8 +24,9 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->command('api:prune-partial-users')
->dailyAt('10:00')
->runInBackground();
}

/**
Expand Down
5 changes: 5 additions & 0 deletions app/Http/Controllers/Action/AuthenticationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ public function register(Request $request): Http\JsonResponse
'required',
'email',
Rule::unique(User::class, 'email')
],
'registered_via' => [
'sometimes',
'string',
]
]
);
Expand All @@ -352,6 +356,7 @@ public function register(Request $request): Http\JsonResponse
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = Hash::make(Str::random(20));
$user->registered_via = $request->input('registered_via', 'api');
$user->save();

$create_token = Str::random(20);
Expand Down
2 changes: 2 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

/**
* @mixin QueryBuilder
* @property int $id
* @property string $name
* @property string $email
* @property string $password
* @property string $remember_token
* @property string|null $registered_via
*/
class User extends Authenticatable
{
Expand Down
Loading

0 comments on commit 2bfcb74

Please sign in to comment.