Skip to content

Commit 724f670

Browse files
committed
Version check
1 parent a1556e8 commit 724f670

File tree

4 files changed

+75
-57
lines changed

4 files changed

+75
-57
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
### Fixed
2424
### Removed --->
2525

26+
## 0.1.2 - 2020-03-19
27+
### Added
28+
29+
- Version checking
30+
2631
## 0.1.1 - 2020-03-19
2732
### Removed
2833

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"require": {
3636
"php": ">=7.2",
3737
"cknow/laravel-money": "^4.0",
38+
"guzzlehttp/guzzle": "^7.3",
3839
"illuminate/support": "^6.0|^7.0|^8.0",
3940
"laracasts/flash": "^3.2",
4041
"livewire/livewire": "^1.0",

config/package.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
|
1414
*/
1515

16-
'version' => '0.1.1',
16+
'version' => '0.1.2',
1717

1818
];

src/Http/Middleware/Settings.php

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Carbon\Carbon;
66
use Closure;
7+
use GuzzleHttp\Client;
78
use Illuminate\Support\Facades\Schema;
89
use VentureDrake\LaravelCrm\Models\Setting;
10+
use VentureDrake\LaravelCrm\Models\User;
911

1012
class Settings
1113
{
@@ -18,73 +20,83 @@ class Settings
1820
*/
1921
public function handle($request, Closure $next)
2022
{
21-
// TBC: Check if table exists
2223
if (Schema::hasTable(config('laravel-crm.db_table_prefix').'settings')) {
23-
$settingVersion = Setting::where([
24-
'name' => 'version',
25-
])->first();
24+
Setting::updateOrCreate([
25+
'name' => 'app_name',
26+
], [
27+
'value' => config('app.name'),
28+
]);
2629

27-
if (! $settingVersion) {
28-
$setting = Setting::create([
29-
'name' => 'version',
30-
'value' => config('laravel-crm.version'),
31-
]);
32-
} else {
33-
$settingVersion->update([
34-
'value' => config('laravel-crm.version'),
35-
]);
36-
}
30+
Setting::updateOrCreate([
31+
'name' => 'app_env',
32+
], [
33+
'value' => config('app.env'),
34+
]);
3735

38-
$setting = Setting::where([
39-
'name' => 'app_name',
40-
])->first();
36+
Setting::updateOrCreate([
37+
'name' => 'app_url',
38+
], [
39+
'value' => config('app.url'),
40+
]);
4141

42-
if (! $setting) {
43-
$setting = Setting::create([
44-
'name' => 'app_name',
45-
'value' => config('app.name'),
46-
]);
47-
} else {
48-
$setting->update([
49-
'value' => config('app.name'),
50-
]);
51-
}
42+
$versionSetting = Setting::updateOrCreate([
43+
'name' => 'version',
44+
], [
45+
'value' => config('laravel-crm.version'),
46+
]);
47+
48+
if ($versionSetting && $versionSetting->updated_at < Carbon::now()->subDays(3)) {
49+
try {
50+
$client = new Client();
51+
$url = "https://beta.laravelcrm.com/api/public/version";
5252

53-
$setting = Setting::where([
54-
'name' => 'app_env',
55-
])->first();
53+
$installIdSetting = Setting::where([
54+
'name' => 'install_id',
55+
])->first();
56+
57+
$userCount = User::where('crm_access', 1)->count();
58+
59+
if ($userCount == 0) {
60+
$userCount = 1;
61+
}
5662

57-
if (! $setting) {
58-
$setting = Setting::create([
59-
'name' => 'app_env',
60-
'value' => config('app.env'),
61-
]);
62-
} else {
63-
$setting->update([
64-
'value' => config('app.env'),
65-
]);
66-
}
63+
$response = $client->request('POST', $url, [
64+
'json' => [
65+
'id' => $installIdSetting->value ?? null,
66+
'name' => config('app.name') ?? null,
67+
'url' => config('app.url') ?? null,
68+
'env' => config('app.env') ?? null,
69+
'version' => config('laravel-crm.version') ?? null,
70+
'server_ip' => request()->server('SERVER_ADDR') ?? null,
71+
'user_ip' => request()->ip() ?? null,
72+
'user_count' => $userCount,
73+
],
74+
]);
6775

68-
$setting = Setting::where([
69-
'name' => 'app_url',
70-
])->first();
76+
$responseBody = json_decode($response->getBody());
7177

72-
if (! $setting) {
73-
$setting = Setting::create([
74-
'name' => 'app_url',
75-
'value' => config('app.url'),
76-
]);
77-
} else {
78-
$setting->update([
79-
'value' => config('app.url'),
80-
]);
78+
if (isset($responseBody->id) && ! $installIdSetting) {
79+
$installIdSetting = Setting::create([
80+
'name' => 'install_id',
81+
'value' => $responseBody->id,
82+
]);
83+
}
84+
85+
Setting::updateOrCreate([
86+
'name' => 'version_latest',
87+
], [
88+
'value' => $responseBody->version,
89+
]);
90+
} catch (\Exception $e) {
91+
//
92+
}
93+
94+
if ($versionSetting) {
95+
$versionSetting->touch();
96+
}
8197
}
8298
}
8399

84-
if ((isset($settingVersion) && $settingVersion->updated_at < Carbon::now()->subDay()) || ! isset($settingVersion)) {
85-
// TBC: Check server for updates, check if can connect first
86-
}
87-
88100
return $next($request);
89101
}
90102
}

0 commit comments

Comments
 (0)