-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from openfoodfoundation/feature/audit-items
Feature: Audit Items model
- Loading branch information
Showing
34 changed files
with
1,429 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace App\Events\Users; | ||
|
||
use App\Models\User; | ||
use Illuminate\Broadcasting\Channel; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Broadcasting\PrivateChannel; | ||
use Illuminate\Contracts\Auth\Authenticatable; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class UserWasCreated | ||
{ | ||
use Dispatchable; | ||
use InteractsWithSockets; | ||
use SerializesModels; | ||
|
||
/** | ||
* Create a new event instance. | ||
* | ||
* @param User|Authenticatable $user | ||
*/ | ||
public function __construct(public User|Authenticatable $user) {} | ||
|
||
/** | ||
* Get the channels the event should broadcast on. | ||
* | ||
* @return array<int, Channel> | ||
*/ | ||
public function broadcastOn(): array | ||
{ | ||
return [ | ||
new PrivateChannel('channel-name'), | ||
]; | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
app/Http/Controllers/Api/V1/Admin/ApiAdminAuditItemsController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\V1\Admin; | ||
|
||
use App\Enums\ApiResponse; | ||
use App\Exceptions\DisallowedApiFieldException; | ||
use App\Http\Controllers\Api\HandlesAPIRequests; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\AuditItem; | ||
use Illuminate\Http\JsonResponse; | ||
|
||
class ApiAdminAuditItemsController extends Controller | ||
{ | ||
use HandlesAPIRequests; | ||
|
||
/** | ||
* Set the related data the GET request is allowed to ask for | ||
*/ | ||
public array $availableRelations = [ | ||
'team', | ||
]; | ||
|
||
public static array $searchableFields = []; | ||
|
||
/** | ||
* GET / | ||
* | ||
* @return JsonResponse | ||
* | ||
* @throws DisallowedApiFieldException | ||
*/ | ||
public function index(): JsonResponse | ||
{ | ||
$this->query = AuditItem::with($this->associatedData); | ||
$this->query = $this->updateReadQueryBasedOnUrl(); | ||
$this->data = $this->query->paginate($this->limit); | ||
|
||
return $this->respond(); | ||
} | ||
|
||
/** | ||
* POST / | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function store(): JsonResponse | ||
{ | ||
$this->responseCode = 403; | ||
$this->message = ApiResponse::RESPONSE_METHOD_NOT_ALLOWED->value; | ||
|
||
return $this->respond(); | ||
} | ||
|
||
/** | ||
* GET /{id} | ||
* | ||
* @param int $id | ||
* | ||
* @return JsonResponse | ||
* | ||
* @throws DisallowedApiFieldException | ||
*/ | ||
public function show(int $id) | ||
{ | ||
$this->query = AuditItem::with($this->associatedData); | ||
$this->query = $this->updateReadQueryBasedOnUrl(); | ||
$this->data = $this->query->find($id); | ||
|
||
return $this->respond(); | ||
} | ||
|
||
/** | ||
* PUT /{id} | ||
* | ||
* @param string $id | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function update(string $id) | ||
{ | ||
$this->responseCode = 403; | ||
$this->message = ApiResponse::RESPONSE_METHOD_NOT_ALLOWED->value; | ||
|
||
return $this->respond(); | ||
} | ||
|
||
/** | ||
* DELETE / {id} | ||
* | ||
* @param string $id | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function destroy(string $id) | ||
{ | ||
$this->responseCode = 403; | ||
$this->message = ApiResponse::RESPONSE_METHOD_NOT_ALLOWED->value; | ||
|
||
return $this->respond(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
app/Http/Controllers/Api/V1/ApiMyTeamAuditItemsController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\V1; | ||
|
||
use App\Enums\ApiResponse; | ||
use App\Exceptions\DisallowedApiFieldException; | ||
use App\Http\Controllers\Api\HandlesAPIRequests; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\AuditItem; | ||
use Auth; | ||
use Illuminate\Http\JsonResponse; | ||
|
||
class ApiMyTeamAuditItemsController extends Controller | ||
{ | ||
use HandlesAPIRequests; | ||
|
||
/** | ||
* Set the related data the GET request is allowed to ask for | ||
*/ | ||
public array $availableRelations = [ | ||
'team', | ||
]; | ||
|
||
public static array $searchableFields = []; | ||
|
||
/** | ||
* GET / | ||
* | ||
* @return JsonResponse | ||
* | ||
* @throws DisallowedApiFieldException | ||
*/ | ||
public function index(): JsonResponse | ||
{ | ||
$this->query = AuditItem::where('auditable_team_id', Auth::user()->current_team_id)->with($this->associatedData); | ||
$this->query = $this->updateReadQueryBasedOnUrl(); | ||
$this->data = $this->query->paginate($this->limit); | ||
|
||
return $this->respond(); | ||
} | ||
|
||
/** | ||
* POST / | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function store(): JsonResponse | ||
{ | ||
$this->responseCode = 403; | ||
$this->message = ApiResponse::RESPONSE_METHOD_NOT_ALLOWED->value; | ||
|
||
return $this->respond(); | ||
} | ||
|
||
/** | ||
* GET /{id} | ||
* | ||
* @param int $id | ||
* | ||
* @return JsonResponse | ||
* | ||
* @throws DisallowedApiFieldException | ||
*/ | ||
public function show(int $id) | ||
{ | ||
$this->query = AuditItem::where('auditable_team_id', Auth::user()->current_team_id)->with($this->associatedData); | ||
$this->query = $this->updateReadQueryBasedOnUrl(); | ||
$this->data = $this->query->find($id); | ||
|
||
return $this->respond(); | ||
} | ||
|
||
/** | ||
* PUT /{id} | ||
* | ||
* @param string $id | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function update(string $id) | ||
{ | ||
$this->responseCode = 403; | ||
$this->message = ApiResponse::RESPONSE_METHOD_NOT_ALLOWED->value; | ||
|
||
return $this->respond(); | ||
} | ||
|
||
/** | ||
* DELETE / {id} | ||
* | ||
* @param string $id | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function destroy(string $id) | ||
{ | ||
$this->responseCode = 403; | ||
$this->message = ApiResponse::RESPONSE_METHOD_NOT_ALLOWED->value; | ||
|
||
return $this->respond(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Models\User; | ||
use App\Services\AuditItemService; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Queue\Queueable; | ||
|
||
class RecordUserWasCreatedAuditItem implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
/** | ||
* Create a new job instance. | ||
* | ||
* @param User $createdUser | ||
*/ | ||
public function __construct(public User $createdUser) {} | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
AuditItemService::createAuditItemForEvent( | ||
model : $this->createdUser, | ||
eventText: 'User ' . $this->createdUser->name . ' was created.', | ||
teamId : $this->createdUser->current_team_id | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Listeners\Users; | ||
|
||
use App\Events\Users\UserWasCreated; | ||
use App\Jobs\RecordUserWasCreatedAuditItem; | ||
|
||
class HandleUserWasCreatedEvent | ||
{ | ||
/** | ||
* Create the event listener. | ||
*/ | ||
public function __construct() {} | ||
|
||
/** | ||
* Handle the event. | ||
* | ||
* @param UserWasCreated $event | ||
*/ | ||
public function handle(UserWasCreated $event): void | ||
{ | ||
|
||
dispatch( | ||
new RecordUserWasCreatedAuditItem( | ||
createdUser: $event->user | ||
) | ||
); | ||
|
||
} | ||
} |
Oops, something went wrong.