Skip to content

Commit

Permalink
Merge pull request #99 from fleetbase/dev-v1.4.27
Browse files Browse the repository at this point in the history
v1.4.27
  • Loading branch information
roncodes authored Jul 5, 2024
2 parents 05e9272 + f7aae7c commit 4567b4a
Show file tree
Hide file tree
Showing 7 changed files with 727 additions and 47 deletions.
672 changes: 651 additions & 21 deletions LICENSE.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "fleetbase/core-api",
"version": "1.4.26",
"version": "1.4.27",
"description": "Core Framework and Resources for Fleetbase API",
"keywords": [
"fleetbase",
"logistics platform"
],
"license": "MIT",
"license": "AGPL-3.0-or-later",
"authors": [
{
"name": "Fleetbase Pte Ltd.",
Expand Down
2 changes: 0 additions & 2 deletions src/Http/Controllers/Internal/v1/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public function upload(UploadFileRequest $request)
$type = $request->input('type');
$size = $request->input('file_size', $request->file->getSize());
$path = $request->input('path', 'uploads');
$subjectId = $request->input('subject_uuid');
$subjectType = $request->input('subject_type');

// Generate a filename
$fileName = File::randomFileNameFromRequest($request);
Expand Down
29 changes: 18 additions & 11 deletions src/Jobs/LogApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fleetbase\Jobs;

use Fleetbase\Models\ApiCredential;
use Fleetbase\Models\ApiRequestLog;
use Fleetbase\Support\Utils;
use Illuminate\Bus\Queueable;
Expand Down Expand Up @@ -44,10 +45,8 @@ class LogApiRequest implements ShouldQueue
*/
public function __construct(array $payload, string $session)
{
// set the connection to log request
$this->dbConnection = $session;
// build payload
$this->payload = $payload;
$this->payload = $payload;
}

/**
Expand All @@ -57,9 +56,7 @@ public function __construct(array $payload, string $session)
*/
public function handle()
{
// Log::info('Logging API Request ' . print_r($this->payload, true));
ApiRequestLog::on($this->dbConnection)->create($this->payload);
// Clear response cache
ResponseCache::clear();
}

Expand All @@ -70,24 +67,34 @@ public function handle()
*/
public static function getPayload(Request $request, $response): array
{
// Prepare the payload
$payload = [];

// get response content
$content = json_decode($response->content());

// get request/response relations
// Get request/response relations
$related = [];

// // if response has a `id` property push to related
// If response has a `id` property push to related
if (Utils::get($content, 'id')) {
$related[] = Utils::get($content, 'id');
}

// get request duration
// Validate api credential, if not uuid then it could be internal
if (ApiCredential::where('uuid', session('api_credential'))->exists()) {
// Need to add a `api_credentail_type` field and morph -- in later versions
// As could be `PersonalAccessToken` `ApiCredential` and eventually `NavigatorAppToken`
$payload['api_credential_uuid'] = session('api_credential');
}

// Get request duration
$duration = round(microtime(true) - LARAVEL_START, 13);

$payload = [
// Finalize payload
$payload = array_merge($payload, [
'_key' => session('api_key'),
'company_uuid' => session('company'),
'api_credential_uuid' => session('api_credential'),
'method' => $request->method(),
'path' => $request->path(),
'full_url' => $request->url(),
Expand All @@ -106,7 +113,7 @@ public static function getPayload(Request $request, $response): array
'response_headers' => static::getResponseHeaders($response),
'response_body' => $content,
'response_raw_body' => $response->content(),
];
]);

return $payload;
}
Expand Down
14 changes: 3 additions & 11 deletions src/Listeners/SendResourceLifecycleWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function handle($event)
$apiCredentialId = session()->get('api_credential', $event->apiCredential);
$apiKey = session()->get('api_key', $event->apiKey ?? 'console');
$apiSecret = session()->get('api_secret', $event->apiSecret ?? 'internal');
$apiEnvironment = session()->get('api_environment', $event->apiEnvironment);
$apiEnvironment = session()->get('api_environment', $event->apiEnvironment ?? 'live');
$isSandbox = session()->get('is_sandbox', $event->isSandbox);

try {
Expand All @@ -49,25 +49,17 @@ public function handle($event)
return;
}

// if no api environment set used do not send webhooks
if (!$apiEnvironment) {
return;
}

// get all webhooks for current company
$webhooks = WebhookEndpoint::where([
'company_uuid' => $companyId,
'status' => 'enabled',
'mode' => $apiEnvironment,
])->where(function ($q) use ($apiCredentialId) {
$q->whereNull('api_credential_uuid');
$q->orWhere('api_credential_uuid', $apiCredentialId);
})->get();
])->get();

// Send Webhook for event
foreach ($webhooks as $webhook) {
// Only Send Webhook if webhook requires this event
if (!empty($webhook->events) && is_array($webhook->events) && !in_array($apiEvent->event, $webhook->events)) {
if ($webhook->cannotFireEvent($apiEvent->event)) {
continue;
}

Expand Down
22 changes: 22 additions & 0 deletions src/Models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,26 @@ public static function fromRequest(Request $request, string $param = 'files'): C

return static::whereIn('uuid', $ids)->get();
}

/**
* Retrieves the contents of the file from the specified disk and path.
*
* This method utilizes the Laravel Storage facade to access the filesystem configured
* for the model's specified disk and retrieves the file located at the model's path.
* Error handling is included to manage cases where the disk or path might not be set,
* or if the file does not exist on the disk.
*
* @return string|null the file contents as a string if found, or null if the file does not exist
*
* @throws \InvalidArgumentException if the disk or path property is not set
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException if the file does not exist
*/
public function getContents(): ?string
{
if (!isset($this->disk) || !isset($this->path)) {
throw new \InvalidArgumentException('Disk or path is not specified.');
}

return Storage::disk($this->disk)->get($this->path);
}
}
31 changes: 31 additions & 0 deletions src/Models/WebhookEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,35 @@ public function getApiCredentialNameAttribute()

return static::attributeFromCache($this, 'apiCredential.key');
}

/**
* Determines if an event cannot be fired based on the current events array.
*
* Checks if the 'events' property is an array, it is not empty, and the specified
* event is not present in the array. If these conditions are met, the event
* cannot be fired.
*
* @param string $event the name of the event to check
*
* @return bool returns true if the event cannot be fired, otherwise false
*/
public function cannotFireEvent(string $event): bool
{
return is_array($this->events) && count($this->events) && !in_array($event, $this->events);
}

/**
* Determines if an event can be fired.
*
* Utilizes the `cannotFireEvent` method to check if the event cannot be fired.
* If `cannotFireEvent` returns false, then it implies the event can be fired.
*
* @param string $event the name of the event to check
*
* @return bool returns true if the event can be fired, otherwise false
*/
public function canFireEvent(string $event): bool
{
return !$this->cannotFireEvent($event);
}
}

0 comments on commit 4567b4a

Please sign in to comment.