mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-08 00:02:15 +01:00
Fixed import lock
This commit is contained in:
committed by
Constantin Graf
parent
29929467f6
commit
62270382dc
@@ -31,10 +31,13 @@ class ImportService
|
||||
$lock = Cache::lock('import:'.$organization->getKey(), config('octane.max_execution_time', 60) + 1);
|
||||
|
||||
if ($lock->get()) {
|
||||
DB::transaction(function () use (&$importer, &$data, &$timezone): void {
|
||||
$importer->importData($data, $timezone);
|
||||
});
|
||||
$lock->release();
|
||||
try {
|
||||
DB::transaction(function () use (&$importer, &$data, &$timezone): void {
|
||||
$importer->importData($data, $timezone);
|
||||
});
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
} else {
|
||||
throw new ImportException('Import is already in progress');
|
||||
}
|
||||
|
||||
@@ -78,6 +78,7 @@ class ImportEndpointTest extends ApiEndpointTestAbstract
|
||||
|
||||
public function test_import_fails_if_data_can_not_be_base64_decoded(): void
|
||||
{
|
||||
// Arrange
|
||||
$user = $this->createUserWithPermission([
|
||||
'import',
|
||||
]);
|
||||
@@ -98,6 +99,7 @@ class ImportEndpointTest extends ApiEndpointTestAbstract
|
||||
|
||||
public function test_import_return_error_message_if_import_fails(): void
|
||||
{
|
||||
// Arrange
|
||||
$user = $this->createUserWithPermission([
|
||||
'import',
|
||||
]);
|
||||
|
||||
@@ -35,6 +35,8 @@ class ImportServiceTest extends TestCase
|
||||
$report = $importService->import($organization, 'toggl_time_entries', $data, $timezone);
|
||||
|
||||
// Assert
|
||||
$lock = Cache::lock('import:'.$organization->getKey());
|
||||
$this->assertTrue($lock->get());
|
||||
$this->assertSame(2, $report->timeEntriesCreated);
|
||||
$this->assertSame(2, $report->tagsCreated);
|
||||
$this->assertSame(1, $report->tasksCreated);
|
||||
@@ -43,6 +45,25 @@ class ImportServiceTest extends TestCase
|
||||
$this->assertSame(1, $report->clientsCreated);
|
||||
}
|
||||
|
||||
public function test_import_releases_lock_if_an_exception_happens_during_the_import(): void
|
||||
{
|
||||
// Arrange
|
||||
Storage::fake(config('filesystems.default'));
|
||||
$organization = Organization::factory()->create();
|
||||
$timezone = 'Europe/Vienna';
|
||||
$data = 'Invalid CSV data';
|
||||
|
||||
// Act
|
||||
$importService = app(ImportService::class);
|
||||
try {
|
||||
$importService->import($organization, 'toggl_time_entries', $data, $timezone);
|
||||
} catch (ImportException) {
|
||||
// Assert
|
||||
$lock = Cache::lock('import:'.$organization->getKey());
|
||||
$this->assertTrue($lock->get());
|
||||
}
|
||||
}
|
||||
|
||||
public function test_import_throws_exception_if_import_is_already_in_progress(): void
|
||||
{
|
||||
// Arrange
|
||||
|
||||
Reference in New Issue
Block a user