89 lines
3.2 KiB
PHP
89 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\System\VersionManagement;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Api\System\VersionManagement\CheckForUpdateRequest;
|
|
use App\Models\System\VersionManagement\AppVersion;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
class AppVersionController extends Controller
|
|
{
|
|
/**
|
|
* Check for app updates
|
|
*
|
|
* This api should be triggered when the app is **launched**, and **current version** and **operating system** should be sent in body. It should check if there is any update available for the app.\
|
|
* **Handle Response**:\
|
|
* * **Update Required**: If the update is critical/mandatory, show a **blocking modal**. The user cannot dismiss it and must click a button to go to the App Store or Google Play Store.\
|
|
* * **Update Optional**: If a new version is available but not mandatory, show a **dismissible notification** or modal suggesting the user to update for a better experience.\
|
|
* * **Latest Version**: If the app is up-to-date, proceed to the app home screen silently.\
|
|
* * **Version Not Found**: If the version is not recognized (e.g., development build), do not show any update prompts.\
|
|
*/
|
|
public function checkForUpdate(CheckForUpdateRequest $request): JsonResponse
|
|
{
|
|
$app_version = AppVersion::latest()->where('os', $request->os)->first();
|
|
|
|
if (! $app_version) {
|
|
return $this->versionNotFound();
|
|
}
|
|
|
|
if ($request->version === $app_version->version) {
|
|
return $this->latestVersion($app_version);
|
|
}
|
|
|
|
if ($request->version < $app_version->version && $app_version->important) {
|
|
return $this->requiredToUpdate($app_version);
|
|
}
|
|
|
|
if ((int) $request->version < (int) $app_version->version) {
|
|
return $this->optionalToUpdate($app_version);
|
|
}
|
|
|
|
return $this->versionNotFound();
|
|
}
|
|
|
|
/**
|
|
* Latest version
|
|
*/
|
|
public function latestVersion(AppVersion $app_version): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'update' => 'latest',
|
|
'notes' => sprintf('You are using the latest version of the app (%s).', $app_version->version),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Required to update
|
|
*/
|
|
public function requiredToUpdate(AppVersion $app_version): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'update' => 'required',
|
|
'notes' => sprintf('A critical update is available. Please update to the latest version (%s).', $app_version->version),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update not required, but should be
|
|
*/
|
|
public function optionalToUpdate(AppVersion $app_version): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'update' => 'optional',
|
|
'notes' => sprintf('A new version is available. Please update to the latest version (%s).', $app_version->version),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* App version not found
|
|
*/
|
|
public function versionNotFound(): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'update' => 'version-not-found',
|
|
'notes' => 'The version is not recognized (e.g., development build).',
|
|
]);
|
|
}
|
|
}
|