Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_KEY=base64:yul3qSY4NPlQvHdVuOgBu3aS+IK2CjYyW18JBvo0IWM=
APP_DEBUG=true
APP_TIMEZONE=UTC
APP_URL=http://localhost
Expand All @@ -21,12 +21,16 @@ LOG_STACK=single
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mongodb
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=laravel
# DB_USERNAME=root
# DB_PASSWORD=
DB_CONNECTION=sqlite
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_DB=
REDIS_CACHE_DB=
CACHE_STORE=redis
REDIS_CACHE_CONNECTION=cache
REDIS_CACHE_LOCK_CONNECTION=default
CACHE_PREFIX=2

SESSION_DRIVER=database
SESSION_LIFETIME=120
Expand All @@ -38,15 +42,13 @@ BROADCAST_CONNECTION=log
FILESYSTEM_DISK=local
QUEUE_CONNECTION=database

CACHE_STORE=database
CACHE_PREFIX=

MEMCACHED_HOST=127.0.0.1

REDIS_CLIENT=phpredis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
#REDIS_HOST=127.0.0.1
#REDIS_PASSWORD=null
#REDIS_PORT=6379

MAIL_MAILER=log
MAIL_SCHEME=null
Expand All @@ -64,14 +66,14 @@ AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

VITE_APP_NAME="${APP_NAME}"
MONGODB_CONNECTION=mongodb
MONGODB_HOST=mongodb
MONGODB_PORT=27017
MONGODB_DATABASE=kit
MONGODB_URI="mongodb://localhost:27017"
MONGODB_USERNAME=
MONGODB_PASSWORD=
MONGODB_AUTHENTICATION_DATABASE=admin
#MONGODB_CONNECTION=mongodb
#MONGODB_HOST=mongodb
#MONGODB_PORT=27017
#MONGODB_DATABASE=kit
#MONGODB_URI="mongodb://localhost:27017"
#MONGODB_USERNAME=root
#MONGODB_PASSWORD=123456789
#MONGODB_AUTHENTICATION_DATABASE=admin

#https://cabinet-new.tk-kit.com/developers/get-token
TK_KIT_API_URL=https://capi.tk-kit.com
Expand Down
50 changes: 31 additions & 19 deletions app/Jobs/UpdateKitGeographyData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Jobs;

use App\Models\Terminal;
use App\Repositories\TerminalRepository;
use App\Services\KitDeliveryService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -20,40 +20,52 @@ class UpdateKitGeographyData implements ShouldQueue
use Queueable;
use SerializesModels;

public function handle(KitDeliveryService $kitService)
public function handle(KitDeliveryService $kitService, TerminalRepository $terminalRepository)
{
try {
Log::info('Starting synchronization with KIT API...');

$start = microtime(true);

$terminals = $kitService->getTerminals();
$terminalIds = [];
Log::info('Retrieved ' . count($terminals) . ' terminals from API');

if (count($terminals) === 0) {
Log::warning('No terminals returned from KIT API');
return;
}

$processedTerminals = [];

foreach ($terminals as $terminal) {
if (is_object($terminal)) {
Terminal::updateOrCreate(
['id' => $terminal->id],
[
'geography_city_id' => $terminal->geography_city_id,
'lat' => $terminal->lat,
'lon' => $terminal->lon,
'address_code' => $terminal->address_code,
'city_name' => $terminal->city_name,
'phone' => $terminal->phone,
'email' => $terminal->email,
'value' => $terminal->value,
]
);
$terminalIds[] = $terminal->id;
// Convert object to array for caching
$processedTerminals[] = [
'id' => $terminal->id,
'geography_city_id' => $terminal->geography_city_id,
'lat' => $terminal->lat,
'lon' => $terminal->lon,
'address_code' => $terminal->address_code,
'city_name' => $terminal->city_name,
'phone' => $terminal->phone,
'email' => $terminal->email,
'value' => $terminal->value,
];
}
}

Terminal::whereNotIn('id', $terminalIds)->delete();
Log::info('Processed ' . count($processedTerminals) . ' terminals for caching');

// Store all terminals in cache
$terminalRepository->cacheTerminals($processedTerminals);

// Verify data was cached by reading it back
$cachedCount = count($terminalRepository->getAllTerminals());
Log::info("Verification: found {$cachedCount} terminals in cache after storing");

$duration = round(microtime(true) - $start, 2);

Log::info("Synchronization completed in {$duration} seconds");
Log::info("Synchronization completed in {$duration} seconds. Cached " . count($processedTerminals) . " terminals.");
} catch (\Exception $e) {
Log::error("Synchronization failed: {$e->getMessage()}");
Log::error($e->getTraceAsString());
Expand Down
10 changes: 3 additions & 7 deletions app/Models/Terminal.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use MongoDB\Laravel\Eloquent\Model;
use Illuminate\Database\Eloquent\Model;

class Terminal extends Model
{
use HasFactory;

protected $collection = 'terminals';
protected $table = 'terminals';

protected $fillable = [
'id',
Expand All @@ -24,8 +24,4 @@ class Terminal extends Model
'email',
'value',
];
// public function city()
// {
// return $this->belongsTo(City::class, 'city_id', 'city_id');
// }
}
}
89 changes: 70 additions & 19 deletions app/Repositories/TerminalRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,87 @@

namespace App\Repositories;

use App\Models\Terminal;
use Illuminate\Support\Facades\DB;
use MongoDB\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class TerminalRepository
{
private Collection $collection;

public function __construct()
{
$this->collection = Terminal::raw();
}
private const CACHE_TTL = 86400; // Cache for 24 hours
private const CACHE_KEY_PREFIX = 'terminals:';
private const CACHE_ALL_KEY = 'terminals:all';

/**
* Search terminals by query string
*
* @param string|null $query
* @return array
*/
public function search(?string $query = null): array
{
$query = $query ?? '';

$result = DB::connection('mongodb')
->table('terminals')
->where(function($q) use ($query) {
$q->where('city_name', 'like', '%' . $query . '%')
->orWhere('address_code', 'like', '%' . $query . '%');
})
->limit(10)
->get();
if (empty($query)) {
return $this->getAllTerminals();
}

$allTerminals = $this->getAllTerminals();

return array_filter($allTerminals, function($terminal) use ($query) {
$query = strtolower($query);
return stripos(strtolower($terminal['city_name'] ?? ''), $query) !== false ||
stripos(strtolower($terminal['address_code'] ?? ''), $query) !== false;
});
}

/**
* Get all terminals from cache
*
* @return array
*/
public function getAllTerminals(): array
{
return Cache::get(self::CACHE_ALL_KEY, []);
}

/**
* Cache terminals from API
*
* @param array $terminals
* @return void
*/
public function cacheTerminals(array $terminals): void
{
Cache::put(self::CACHE_ALL_KEY, $terminals, self::CACHE_TTL);

return $result->toArray();
foreach ($terminals as $terminal) {
if (isset($terminal['id'])) {
Cache::put(
self::CACHE_KEY_PREFIX . $terminal['id'],
$terminal,
self::CACHE_TTL
);
}
}
}

/**
* Get terminal by ID
*
* @param string $id
* @return array|null
*/
public function find(string $id): ?array
{
return Cache::get(self::CACHE_KEY_PREFIX . $id);
}

/**
* Clear terminal cache
*
* @return void
*/
public function clearCache(): void
{
Cache::forget(self::CACHE_ALL_KEY);
}
}
}
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"kitdelivery/sdk-kit-api": "dev-main",
"laravel/framework": "^11.31",
"symfony/finder": "^7.0.7",
"laravel/tinker": "^2.9",
"mongodb/laravel-mongodb": "^5.1"
"laravel/tinker": "^2.9"
},
"repositories": [
{
Expand Down
Loading
Loading