Make system reset perform reset for all users.

This commit is contained in:
arabcoders
2025-04-03 00:42:28 +03:00
parent abd60715c2
commit 6c7cde4d2b
2 changed files with 21 additions and 33 deletions

View File

@@ -33,15 +33,16 @@
<Message message_class="is-background-warning-80 has-text-dark" title="Important information"
icon="fas fa-exclamation-triangle">
<p>
Are you sure you want to reset <span class="has-text-danger is-bold is-underlined">{{ api_user }}</span> user local state?
Are you sure you want to reset <span class="has-text-danger is-bold is-underlined">all users</span> local
state?
</p>
<h5 class="has-text-dark">This operation will do the following:</h5>
<ul>
<li>Remove all data from user local database.</li>
<li>Attempt to flush the cache related to user.</li>
<li>Reset the user backends last sync date.</li>
<li>Remove all data from local databases.</li>
<li>Flush to cached data.</li>
<li>Reset all users backends last sync date.</li>
</ul>
<p>There is no undo operation. This action is irreversible.</p>
@@ -49,8 +50,7 @@
</div>
<div class="column is-12">
<Confirm @confirmed="resetSystem()"
:title="`Perform local state reset for: ${api_user}`"
<Confirm @confirmed="resetSystem()" :title="`Perform local state reset for all users`"
title-icon="fa-redo"
warning="Depending on your hardware speed, the reset operation might take long time. do not interrupt the process, or close the browser tab. You will be redirected to the index page automatically once the process is complete. Otherwise, you might end up with a corrupted database and/or state."
/>

View File

@@ -6,9 +6,7 @@ namespace App\API\System;
use App\Libs\Attributes\Route\Delete;
use App\Libs\Enums\Http\Status;
use App\Libs\Exceptions\RuntimeException;
use App\Libs\Mappers\ImportInterface as iImport;
use App\Libs\Traits\APITraits;
use Psr\Http\Message\ResponseInterface as iResponse;
use Psr\Http\Message\ServerRequestInterface as iRequest;
use Psr\Log\LoggerInterface as iLogger;
@@ -17,41 +15,31 @@ use RedisException;
final class Reset
{
use APITraits;
public const string URL = '%{api.prefix}/system/reset';
#[Delete(self::URL . '[/]', name: 'system.reset')]
public function __invoke(iRequest $request, Redis $redis, iImport $mapper, iLogger $logger): iResponse
{
try {
$userContext = $this->getUserContext($request, $mapper, $logger);
$user = $userContext->name;
} catch (RuntimeException $e) {
return api_error($e->getMessage(), Status::NOT_FOUND);
foreach (getUsersContext($mapper, $logger) as $userContext) {
// -- reset database.
$userContext->db->reset();
// -- reset last import/export date.
foreach (array_keys($userContext->config->getAll()) as $name) {
$userContext->config->set("{$name}.import.lastSync", null);
$userContext->config->set("{$name}.export.lastSync", null);
}
// -- persist changes.
$userContext->config->persist(true);
}
// -- reset cache data.
try {
$ns = getAppVersion();
$ns .= isValidName($user) ? '.' . $user : '.' . md5($user);
$keys = $redis->keys("{$ns}*");
if ($keys && is_array($keys)) {
$redis->del($keys);
}
$redis->flushDB();
} catch (RedisException) {
}
$userContext->db->reset();
foreach (array_keys($userContext->config->getAll()) as $name) {
$userContext->config->set("{$name}.import.lastSync", null);
$userContext->config->set("{$name}.export.lastSync", null);
}
$userContext->config->persist();
return api_response(Status::OK, ['message' => 'System reset.']);
return api_response(Status::OK, ['message' => 'System reset is complete.']);
}
}