-
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.
[TM-857] Audit Log and Comment Feature for Project/Site/Polygons (#254)
* add status table * add attachment table * add audit-status + site-polygon endpoints * add swagger definitions for audit status and polygon endpoints * fix: problem with parameter in open api definition * fix openapi definitions and test execution * implement polymorphic association in Audit Status * fix merge errors and remove duplicated definition * add AuditStatus attachments with MediaModel * remove unused methods and files * remove GetPolygonByProjectController * replace auditable_id and auditable_type with entity_name * add AuditStatusRequest to improve endpoint readability * improve swagger definition * update swagger definition * replace EntityModel by AuditableModel * rename variable * remove unused table * remove unused definition and update swagger * change AuditStatus upload file policy * address corrections on AuditableModel * use ModelInterfaceBindingMiddleware on store audit controller * fix typo and update references in swagger * rename status table to audit-statuese * change table name to use underscore * [TM-843] add endpoint to determine wether a site can be approved (#274) * add endpoint to determine wether a site can be approved * improve code for checking site approval * [TM-944] Enable polygon audit log (#273) * Add endpoint for listing SitePolygons for a given project * fix problem with parameters missing in definition * [TM-941] Audit Log request support (#275) * add endpoint for updating status * change EntityModel -> AuditableModel and add missing policy * move criteria site to helper for reuse * add criteria check for site polygon approval * move endpoint to auditable path * add a policy for updating Site Polygons * fix problem with parameters missing in definitio * improve query for criteria site
- Loading branch information
Showing
35 changed files
with
1,145 additions
and
26 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
25 changes: 25 additions & 0 deletions
25
app/Http/Controllers/V2/AuditStatus/GetAuditStatusController.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,25 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\V2\AuditStatus; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Resources\V2\AuditStatusResource; | ||
use App\Models\V2\AuditableModel; | ||
use Illuminate\Http\Request; | ||
|
||
class GetAuditStatusController extends Controller | ||
{ | ||
public function __invoke(Request $request, AuditableModel $auditable) | ||
{ | ||
$auditStatuses = $auditable->auditStatuses() | ||
->orderBy('updated_at', 'desc') | ||
->orderBy('created_at', 'desc') | ||
->get(); | ||
|
||
foreach ($auditStatuses as $auditStatus) { | ||
$auditStatus->entity_name = $auditable->getAuditableNameAttribute(); | ||
} | ||
|
||
return AuditStatusResource::collection($auditStatuses); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
app/Http/Controllers/V2/AuditStatus/StoreAuditStatusController.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,34 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\V2\AuditStatus; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\V2\AuditStatus\AuditStatusCreateRequest; | ||
use App\Http\Resources\V2\AuditStatusResource; | ||
use App\Models\Traits\SaveAuditStatusTrait; | ||
use App\Models\V2\AuditableModel; | ||
use App\Models\V2\AuditStatus\AuditStatus; | ||
|
||
class StoreAuditStatusController extends Controller | ||
{ | ||
use SaveAuditStatusTrait; | ||
|
||
public function __invoke(AuditStatusCreateRequest $auditStatusCreateRequest, AuditableModel $auditable): AuditStatusResource | ||
{ | ||
$body = $auditStatusCreateRequest->all(); | ||
|
||
if ($body['type'] === 'change-request') { | ||
AuditStatus::where([ | ||
['auditable_id', $auditable->id], | ||
['type', 'change-request'], | ||
['is_active', true], | ||
])->update(['is_active' => false]); | ||
$auditStatus = $this->saveAuditStatus(get_class($auditable), $auditable->id, $body['status'], $body['comment'], $body['type'], $body['is_active'], $body['request_removed']); | ||
} else { | ||
$auditStatus = $this->saveAuditStatus(get_class($auditable), $auditable->id, $body['status'], $body['comment'], $body['type']); | ||
} | ||
$auditStatus->entity_name = $auditable->name; | ||
|
||
return new AuditStatusResource($auditStatus); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
app/Http/Controllers/V2/Auditable/UpdateAuditableStatusController.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,99 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\V2\Auditable; | ||
|
||
use App\Helpers\GeometryHelper; | ||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\V2\AuditStatus\AuditStatusUpdateRequest; | ||
use App\Models\Traits\SaveAuditStatusTrait; | ||
use App\Models\V2\AuditableModel; | ||
use App\Models\V2\AuditStatus\AuditStatus; | ||
use App\Models\V2\Sites\SitePolygon; | ||
use App\Services\PolygonService; | ||
|
||
class UpdateAuditableStatusController extends Controller | ||
{ | ||
use SaveAuditStatusTrait; | ||
|
||
public function __invoke(AuditStatusUpdateRequest $request, AuditableModel $auditable) | ||
{ | ||
$this->authorize('update', $auditable); | ||
|
||
if (! $this->canChangeStatus($auditable, $request->status)) { | ||
return response()->json(['message' => 'Cannot change status'], 400); | ||
} | ||
|
||
$body = $request->all(); | ||
$status = $body['status']; | ||
|
||
$auditable->status = $status; | ||
$auditable->save(); | ||
|
||
if (isset($body['status'])) { | ||
$this->saveAuditStatus(get_class($auditable), $auditable->id, $status, $body['comment'], $body['type']); | ||
} elseif (isset($body['is_active'])) { | ||
AuditStatus::where('auditable_id', $auditable->id) | ||
->where('type', $body['type']) | ||
->where('is_active', true) | ||
->update(['is_active' => false]); | ||
$this->saveAuditStatus(get_class($auditable), $auditable->id, $status, $body['comment'], $body['type'], $body['is_active'], $body['request_removed']); | ||
} | ||
|
||
return $auditable; | ||
} | ||
|
||
private function canChangeStatus($auditable, $status): bool | ||
{ | ||
switch(get_class($auditable)) { | ||
case 'App\Models\V2\Sites\Site': | ||
return $this->canChangeSiteStatusTo($auditable, $status); | ||
case 'App\Models\V2\Sites\SitePolygon': | ||
return $this->canChangeSitePolygonStatusTo($auditable, $status); | ||
default: | ||
return true; | ||
} | ||
} | ||
|
||
private function canChangeSiteStatusTo($auditable, $status) | ||
{ | ||
if ($status === 'approved') { | ||
return ! SitePolygon::where('site_id', $auditable->id)->where('status', 'approved')->exists(); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function canChangeSitePolygonStatusTo($sitePolygon, $status) | ||
{ | ||
if ($status === 'approved') { | ||
$geometry = $sitePolygon->polygonGeometry()->first(); | ||
|
||
if ($geometry === null) { | ||
return false; | ||
} | ||
|
||
$criteriaList = GeometryHelper::getCriteriaDataForPolygonGeometry($geometry); | ||
|
||
if (empty($criteriaList)) { | ||
return false; | ||
} | ||
|
||
$criteriaList = array_filter($criteriaList, function ($criteria) { | ||
return $criteria['criteria_id'] !== PolygonService::ESTIMATED_AREA_CRITERIA_ID; | ||
}); | ||
|
||
$canApprove = true; | ||
foreach ($criteriaList as $criteria) { | ||
if (! $criteria['valid']) { | ||
$canApprove = false; | ||
|
||
break; | ||
} | ||
} | ||
|
||
return $canApprove; | ||
} | ||
|
||
return true; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
app/Http/Controllers/V2/Polygons/ViewAllSitesPolygonsForProjectController.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,17 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\V2\Polygons; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Resources\V2\SitePolygon\SitePolygonResource; | ||
use App\Models\V2\Projects\Project; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
|
||
class ViewAllSitesPolygonsForProjectController extends Controller | ||
{ | ||
public function __invoke(Request $request, Project $project): ResourceCollection | ||
{ | ||
return SitePolygonResource::collection($project->sitePolygons()->get()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/Http/Controllers/V2/Sites/SiteCheckApproveController.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,18 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\V2\Sites; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\V2\Sites\Site; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class SiteCheckApproveController extends Controller | ||
{ | ||
public function __invoke(Request $request, Site $site): JsonResource | ||
{ | ||
$hasNonApproved = $site->sitePolygons()->where('status', '!=', 'approved')->exists(); | ||
|
||
return new JsonResource(['can_approve' => $hasNonApproved]); | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
app/Http/Requests/V2/AuditStatus/AuditStatusCreateRequest.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,24 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\V2\AuditStatus; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class AuditStatusCreateRequest extends FormRequest | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'type' => 'sometimes|nullable|string', | ||
'comment' => 'sometimes|nullable|string', | ||
'status' => 'sometimes|nullable|string', | ||
'is_active' => 'sometimes|nullable|boolean', | ||
'request_removed' => 'sometimes|nullable|boolean', | ||
]; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
app/Http/Requests/V2/AuditStatus/AuditStatusUpdateRequest.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,24 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\V2\AuditStatus; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class AuditStatusUpdateRequest extends FormRequest | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'type' => 'sometimes|nullable|string', | ||
'comment' => 'sometimes|nullable|string', | ||
'status' => 'sometimes|nullable|string', | ||
'is_active' => 'sometimes|nullable|boolean', | ||
'request_removed' => 'sometimes|nullable|boolean', | ||
]; | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources\V2; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class AuditStatusResource extends JsonResource | ||
{ | ||
/** | ||
* @param Request $request | ||
* @return array | ||
*/ | ||
public function toArray($request) | ||
{ | ||
$data = [ | ||
'id' => $this->id, | ||
'uuid' => $this->uuid, | ||
'entity_name' => $this->entity_name, | ||
'status' => $this->status, | ||
'comment' => $this->comment, | ||
'first_name' => $this->first_name, | ||
'last_name' => $this->last_name, | ||
'type' => $this->type, | ||
'is_submitted' => $this->is_submitted, | ||
'is_active' => $this->is_active, | ||
'request_removed' => $this->request_removed, | ||
'date_created' => $this->date_created, | ||
'created_by' => $this->created_by, | ||
]; | ||
|
||
return $this->appendFilesToResource($data); | ||
} | ||
} |
Oops, something went wrong.