mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-09-02 03:13:57 +00:00
document
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Documents\ControllersLogic;
|
||||
|
||||
use App\Http\Resources\DocumentResource;
|
||||
|
||||
use App\Classes\General\Abstracts\AbstractControllerLogic;
|
||||
|
||||
use App\Classes\Modules\Documents\Services\FetchesDocument;
|
||||
|
||||
use App\Classes\Modules\Documents\Standards\Rules\CanUpdateDocument;
|
||||
use App\Classes\Modules\Documents\Services\ApprovesDocument;
|
||||
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
|
||||
|
||||
use App\Classes\ValueObjects\Constants\ObjectStatus;
|
||||
|
||||
use ErrorException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ApproveDocumentLogic extends AbstractControllerLogic
|
||||
{
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function notification():array {
|
||||
return [
|
||||
'title' => 'Approve Document',
|
||||
'message' => 'You have successfully approved the Document'
|
||||
];
|
||||
}
|
||||
|
||||
/** @var CanUpdateDocument */
|
||||
private $canUpdateDocument;
|
||||
|
||||
/** @var ApprovesDocument */
|
||||
private $approvesDocument;
|
||||
|
||||
/** @var FetchesDocument */
|
||||
private $fetchesDocument;
|
||||
|
||||
/**
|
||||
* UpdateStandardSegmentConstantLogic constructor.
|
||||
* @param CanUpdateDocument $canUpdateDocument
|
||||
* @param ApprovesDocument $approvesDocument
|
||||
* @param FetchesDocument $fetchesDocument
|
||||
*/
|
||||
public function __construct(
|
||||
canUpdateDocument $canUpdateDocument,
|
||||
ApprovesDocument $approvesDocument,
|
||||
FetchesDocument $fetchesDocument
|
||||
)
|
||||
{
|
||||
$this->canUpdateDocument = $canUpdateDocument;
|
||||
$this->approvesDocument = $approvesDocument;
|
||||
$this->fetchesDocument = $fetchesDocument;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
* @throws ErrorException
|
||||
*/
|
||||
public function logic(Request $request) : JsonResponse
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$document_query = $this->fetchesDocument->execute(['id' => $request->route('id')]);
|
||||
|
||||
$document_object = new DocumentObject(
|
||||
$document_query->owner_id,
|
||||
$document_query->owner_type,
|
||||
$document_query->document_type,
|
||||
$document_query->reference,
|
||||
ObjectStatus::ACTIVE,
|
||||
\Auth::user()->id,
|
||||
$document_query->issued_date,
|
||||
$document_query->expired_date,
|
||||
$document_query->approved_date
|
||||
);
|
||||
$this->canUpdateDocument->passes($document_object);
|
||||
$document_query = $this->approvesDocument->execute($document_query, $document_object);
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $this->resourceResponse(new DocumentResource($document_query));
|
||||
|
||||
} catch (\Exception $exception){
|
||||
dd($exception);
|
||||
throw new ErrorException($exception->getMessage(), $exception->getCode());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Documents\ControllersLogic;
|
||||
|
||||
|
||||
use App\Classes\General\Abstracts\AbstractControllerLogic;
|
||||
use App\Classes\Modules\Documents\Services\ListsDocuments;
|
||||
use App\Classes\Modules\Documents\Standards\Rules\CanListDocuments;
|
||||
use App\Http\Resources\DocumentResource;
|
||||
use ErrorException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ListDocumentLogic extends AbstractControllerLogic
|
||||
{
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function notification():array {
|
||||
return [
|
||||
'title' => 'Retrieved Document',
|
||||
'message' => 'You have successfully retrieved a list of Document'
|
||||
];
|
||||
}
|
||||
|
||||
/** @var CanListDocuments */
|
||||
private $canListDocuments;
|
||||
|
||||
/** @var ListsDocuments */
|
||||
private $listsDocuments;
|
||||
|
||||
/**
|
||||
* ListStandardSegmentLogic constructor.
|
||||
* @param CanListDocuments $canListDocuments
|
||||
* @param ListsDocuments $listsDocuments
|
||||
*/
|
||||
public function __construct(CanListDocuments $canListDocuments, ListsDocuments $listsDocuments)
|
||||
{
|
||||
$this->canListDocuments = $canListDocuments;
|
||||
$this->listsDocuments = $listsDocuments;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
* @throws ErrorException
|
||||
*/
|
||||
public function logic(Request $request) : JsonResponse
|
||||
{
|
||||
try {
|
||||
|
||||
$document_type = $request->input('document_type');
|
||||
$reference = $request->input('reference');
|
||||
$status = $request->input('status');
|
||||
|
||||
$data = [];
|
||||
|
||||
if ($document_type) {
|
||||
$data['document_type'] = $document_type;
|
||||
}
|
||||
if ($reference) {
|
||||
$data['reference'] = $reference;
|
||||
}
|
||||
if ($status) {
|
||||
$data['status'] = $status;
|
||||
}
|
||||
|
||||
$this->canListDocuments->passes();
|
||||
|
||||
$query = $this->listsDocuments->execute($data);
|
||||
|
||||
return $this->collectionResponse(DocumentResource::collection($query));
|
||||
|
||||
} catch (\Exception $exception){
|
||||
throw new ErrorException($exception->getMessage(), $exception->getCode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Documents\Services;
|
||||
|
||||
use App\Classes\General\Eloquent\AbstractUpdateRecord;
|
||||
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
|
||||
use App\Models\Document;
|
||||
|
||||
class ApprovesDocument extends AbstractUpdateRecord
|
||||
{
|
||||
|
||||
/**
|
||||
* @param DocumentObject $object
|
||||
* @return \Illuminate\Database\Eloquent\Model
|
||||
* @throws \App\Classes\Exceptions\MalformedRequestException
|
||||
*/
|
||||
public function execute(Document $model, DocumentObject $object)
|
||||
{
|
||||
$model->status = $object->getStatus();
|
||||
$model->approved_by = $object->getApprovedBy();
|
||||
return $this->handler($model);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Documents\Services;
|
||||
|
||||
|
||||
use App\Classes\General\Eloquent\AbstractFetchRecord;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use App\Models\Document;
|
||||
|
||||
class FetchesDocument extends AbstractFetchRecord
|
||||
{
|
||||
|
||||
/** @var Document */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* FetchesUser constructor.
|
||||
* @param Document $repository
|
||||
*/
|
||||
public function __construct(Document $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Builder
|
||||
*/
|
||||
public function getRepository(): Builder
|
||||
{
|
||||
return $this->repository->newQuery();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Documents\Services;
|
||||
|
||||
|
||||
use App\Classes\General\Eloquent\AbstractListRecord;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use App\Models\Document;
|
||||
|
||||
class ListsDocuments extends AbstractListRecord
|
||||
{
|
||||
|
||||
/** @var Document */
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* ListsDocuments constructor.
|
||||
* @param Document $repository
|
||||
*/
|
||||
public function __construct(Document $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Builder
|
||||
*/
|
||||
function getRepository(): Builder
|
||||
{
|
||||
return $this->repository->newQuery();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Documents\Standards\Rules;
|
||||
|
||||
use App\Classes\General\Abstracts\AbstractRule;
|
||||
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
|
||||
|
||||
class CanListDocuments extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
protected function authorized(): bool
|
||||
{
|
||||
// TODO Set Authorization rules
|
||||
|
||||
if (!\Auth::user()->can('view document')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DocumentObject $object
|
||||
* @return bool
|
||||
*/
|
||||
protected function validators($object): bool
|
||||
{
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param DocumentObject $object
|
||||
* @return bool
|
||||
*/
|
||||
protected function criteria($object): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\Modules\Documents\Standards\Rules;
|
||||
|
||||
|
||||
use App\Classes\General\Abstracts\AbstractRule;
|
||||
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
|
||||
use App\Classes\Modules\Documents\Standards\Validators\DocumentValidation;
|
||||
|
||||
class CanUpdateDocument extends AbstractRule
|
||||
{
|
||||
|
||||
/** @var DocumentValidation */
|
||||
private $documentValidation;
|
||||
|
||||
/**
|
||||
* CanCreateAddress constructor.
|
||||
* @param DocumentValidation $documentValidation
|
||||
*/
|
||||
public function __construct(DocumentValidation $documentValidation)
|
||||
{
|
||||
$this->documentValidation = $documentValidation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
protected function authorized(): bool
|
||||
{
|
||||
// TODO Set Authorization rules
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DocumentObject $object
|
||||
* @return bool
|
||||
* @throws \App\Classes\Exceptions\RequestValidationException
|
||||
*/
|
||||
protected function validators($object): bool
|
||||
{
|
||||
return $this->documentValidation->validate($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DocumentObject $object
|
||||
* @return bool
|
||||
*/
|
||||
protected function criteria($object): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,5 +3,6 @@
|
||||
namespace App\Classes\ValueObjects\Constants;
|
||||
|
||||
final class ObjectStatus {
|
||||
public const ACTIVE = 1;
|
||||
public const PENDING = 9;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Documents;
|
||||
|
||||
use App\Classes\Modules\Documents\ControllersLogic\ApproveDocumentLogic;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ApproveDocumentController
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param ApproveDocumentLogic $logic
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function approve(Request $request, ApproveDocumentLogic $logic): JsonResponse {
|
||||
return $logic->execute($request);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Documents;
|
||||
|
||||
use App\Classes\Modules\Documents\ControllersLogic\ListDocumentLogic;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ListDocumentsController
|
||||
{
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param ListDocumentLogic $logic
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function list(Request $request, ListDocumentLogic $logic): JsonResponse {
|
||||
return $logic->execute($request);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class DocumentResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'document_type' => $this->document_type,
|
||||
'reference' => $this->reference,
|
||||
'status' => (int) $this->status,
|
||||
'approved_by' => (int) $this->approved_by,
|
||||
'files' => $this->files()->get()
|
||||
];
|
||||
}
|
||||
}
|
||||
+16
-1
@@ -5,6 +5,8 @@ namespace App\Models;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\hasMany;
|
||||
|
||||
/**
|
||||
* Class Document
|
||||
* @package App\Models
|
||||
@@ -22,5 +24,18 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
*/
|
||||
class Document extends AbstractModel
|
||||
{
|
||||
//
|
||||
use SoftDeletes;
|
||||
|
||||
protected $table = 'documents';
|
||||
|
||||
protected $dates = ['deleted_at'];
|
||||
|
||||
/**
|
||||
* @return hasMany
|
||||
*/
|
||||
public function files(): hasMany
|
||||
{
|
||||
return $this->hasMany(File::class, 'document_id');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,4 +21,9 @@ class File extends AbstractModel
|
||||
protected $table = 'files';
|
||||
|
||||
protected $dates = ['deleted_at'];
|
||||
|
||||
public function getFileAttribute($value)
|
||||
{
|
||||
return $value ? json_decode($value) : [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,8 +20,8 @@ class CreateDocumentsTable extends Migration
|
||||
$table->string('document_type')->nullable();
|
||||
$table->string('reference')->nullable();
|
||||
$table->integer('status')->nullable();
|
||||
$table->bigInteger('approved_by')->unsigned();
|
||||
$table->foreign('approved_by')->references('id')->on('users')->onDelete('cascade')->nullable();
|
||||
$table->bigInteger('approved_by')->unsigned()->nullable();
|
||||
$table->foreign('approved_by')->references('id')->on('users')->onDelete('cascade');
|
||||
$table->timestamp('issued_date')->nullable();
|
||||
$table->timestamp('expired_date')->nullable();
|
||||
$table->timestamp('approved_date')->nullable();
|
||||
|
||||
@@ -19,6 +19,12 @@ class AdminUserPermissionsTableSeeder extends Seeder
|
||||
app()['cache']->forget('spatie.permission.cache');
|
||||
|
||||
// admin permissions
|
||||
Permission::create(['name' => 'view document', 'guard_name' => 'web']);
|
||||
Permission::create(['name' => 'add document', 'guard_name' => 'web']);
|
||||
Permission::create(['name' => 'edit document', 'guard_name' => 'web']);
|
||||
Permission::create(['name' => 'delete document', 'guard_name' => 'web']);
|
||||
|
||||
|
||||
Permission::create(['name' => 'view standard_segment', 'guard_name' => 'web']);
|
||||
Permission::create(['name' => 'add standard_segment', 'guard_name' => 'web']);
|
||||
Permission::create(['name' => 'edit standard_segment', 'guard_name' => 'web']);
|
||||
|
||||
@@ -17,6 +17,9 @@ use Illuminate\Support\Facades\Route;
|
||||
Route::group(['middleware' => 'api', 'prefix' => 'v1', 'as' => 'api.'], function () {
|
||||
|
||||
require __DIR__ . '/account.php';
|
||||
|
||||
require __DIR__ . '/document.php';
|
||||
|
||||
require __DIR__ . '/segment.php';
|
||||
require __DIR__ . '/segment_constant.php';
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::group(['namespace' => 'Documents'], function () {
|
||||
Route::group(['middleware' => 'valid.token'], function () {
|
||||
Route::group(['prefix' => 'document'], function () {
|
||||
Route::get('/list', 'ListDocumentsController@list')->name('document.list');
|
||||
Route::put('/approve/{id}', 'ApproveDocumentController@approve')->name('document.approve');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user