Generate UUID as shifts transaction id and fixed translation

This commit is contained in:
Igor Scheller 2022-06-12 15:01:34 +02:00
parent 7c448e6064
commit bb49b308fb
7 changed files with 68 additions and 23 deletions

View File

@ -35,6 +35,7 @@ return [
\Engelsystem\Mail\MailerServiceProvider::class, \Engelsystem\Mail\MailerServiceProvider::class,
\Engelsystem\Http\HttpClientServiceProvider::class, \Engelsystem\Http\HttpClientServiceProvider::class,
\Engelsystem\Helpers\DumpServerServiceProvider::class, \Engelsystem\Helpers\DumpServerServiceProvider::class,
\Engelsystem\Helpers\UuidServiceProvider::class,
], ],
// Application middleware // Application middleware

View File

@ -19,7 +19,7 @@ class ShiftsAddTransactionId extends Migration
} }
$this->schema->table('Shifts', function (Blueprint $table) { $this->schema->table('Shifts', function (Blueprint $table) {
$table->unsignedInteger('transaction_id')->nullable()->default(null); $table->uuid('transaction_id')->index()->nullable()->default(null);
}); });
} }

View File

@ -556,19 +556,6 @@ function Shift_update($shift)
); );
} }
/**
* Get the next free shifts transaction id
*/
function Shift_get_next_transaction_id($lock = true): int
{
$query = Db::connection()->table('Shifts')->select('transaction_id');
if ($lock) {
$query->lockForUpdate();
}
return $query->max('transaction_id') + 1;
}
/** /**
* Create a new shift. * Create a new shift.
* *

View File

@ -5,6 +5,7 @@ use Engelsystem\Helpers\Carbon;
use Engelsystem\Http\Exceptions\HttpForbidden; use Engelsystem\Http\Exceptions\HttpForbidden;
use Engelsystem\Models\Room; use Engelsystem\Models\Room;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Illuminate\Support\Str;
/** /**
* @return string * @return string
@ -365,17 +366,10 @@ function admin_shifts()
throw_redirect(page_link_to('admin_shifts')); throw_redirect(page_link_to('admin_shifts'));
} }
$transactionRunning = true; $transactionId = Str::uuid();
Db::connection()->beginTransaction();
$transactionId = Shift_get_next_transaction_id();
foreach ($session->get('admin_shifts_shifts', []) as $shift) { foreach ($session->get('admin_shifts_shifts', []) as $shift) {
$shift['URL'] = null; $shift['URL'] = null;
$shift_id = Shift_create($shift, $transactionId); $shift_id = Shift_create($shift, $transactionId);
if ($transactionRunning) {
$transactionRunning = false;
Db::connection()->commit();
}
engelsystem_log( engelsystem_log(
'Shift created: ' . $shifttypes[$shift['shifttype_id']] 'Shift created: ' . $shifttypes[$shift['shifttype_id']]
@ -383,6 +377,7 @@ function admin_shifts()
. ' with description ' . $shift['description'] . ' with description ' . $shift['description']
. ' from ' . date('Y-m-d H:i', $shift['start']) . ' from ' . date('Y-m-d H:i', $shift['start'])
. ' to ' . date('Y-m-d H:i', $shift['end']) . ' to ' . date('Y-m-d H:i', $shift['end'])
. ', transaction: ' . $transactionId
); );
$needed_angel_types_info = []; $needed_angel_types_info = [];

View File

@ -532,7 +532,7 @@ msgid "Shift deleted."
msgstr "Schicht gelöscht." msgstr "Schicht gelöscht."
msgid "Shifts history" msgid "Shifts history"
msgstr "Schichten historie" msgstr "Schichten Historie"
msgid "%s shifts deleted." msgid "%s shifts deleted."
msgstr "%s Schichten gelöscht." msgstr "%s Schichten gelöscht."

View File

@ -0,0 +1,34 @@
<?php
namespace Engelsystem\Helpers;
use Engelsystem\Container\ServiceProvider;
use Illuminate\Support\Str;
class UuidServiceProvider extends ServiceProvider
{
/**
* Register the UUID generator to the Str class
*/
public function register()
{
Str::createUuidsUsing([$this, 'uuid']);
}
/**
* Generate a v4 UUID
*/
public function uuid(): string
{
return sprintf(
'%08x-%04x-%04x-%04x-%012x',
mt_rand(0, 0xffffffff),
mt_rand(0, 0xffff),
// first bit is the uuid version, here 4
mt_rand(0, 0x0fff) | 0x4000,
// variant
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffffffffffff)
);
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Engelsystem\Test\Unit\Helpers;
use Engelsystem\Application;
use Engelsystem\Helpers\UuidServiceProvider;
use Engelsystem\Test\Unit\ServiceProviderTest;
use Illuminate\Support\Str;
class UuidServiceProviderTest extends ServiceProviderTest
{
/**
* @covers \Engelsystem\Helpers\UuidServiceProvider::register
* @covers \Engelsystem\Helpers\UuidServiceProvider::uuid
*/
public function testRegister()
{
$app = new Application();
$serviceProvider = new UuidServiceProvider($app);
$serviceProvider->register();
$this->assertStringMatchesFormat(
'%x%x%x%x%x%x%x%x-%x%x%x%x-4%x%x%x-%x%x%x%x-%x%x%x%x%x%x%x%x%x%x%x%x',
Str::uuid()
);
}
}