mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-16 04:02:15 +01:00
Moved import test files; Added timezone argument to importers
This commit is contained in:
committed by
Constantin Graf
parent
7af77c9357
commit
b5a240cb2f
@@ -11,6 +11,7 @@ use App\Service\Import\Importers\ImporterProvider;
|
||||
use App\Service\Import\Importers\ImportException;
|
||||
use App\Service\Import\Importers\ReportDto;
|
||||
use App\Service\Import\ImportService;
|
||||
use App\Service\TimezoneService;
|
||||
use Brick\Money\ISOCurrencyProvider;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Components\Select;
|
||||
@@ -112,11 +113,14 @@ class OrganizationResource extends Resource
|
||||
if ($file === null) {
|
||||
throw new \Exception('File not found');
|
||||
}
|
||||
/** @var string $timezone */
|
||||
$timezone = $data['timezone'];
|
||||
/** @var ReportDto $report */
|
||||
$report = app(ImportService::class)->import(
|
||||
$record,
|
||||
$data['type'],
|
||||
$file
|
||||
$file,
|
||||
$timezone
|
||||
);
|
||||
Notification::make()
|
||||
->title('Import successful')
|
||||
@@ -156,6 +160,11 @@ class OrganizationResource extends Resource
|
||||
|
||||
return $select;
|
||||
}),
|
||||
Forms\Components\Select::make('timezone')
|
||||
->label('Timezone')
|
||||
->options(fn (): array => app(TimezoneService::class)->getSelectOptions())
|
||||
->searchable()
|
||||
->required(),
|
||||
]),
|
||||
])
|
||||
->bulkActions([
|
||||
|
||||
@@ -67,10 +67,12 @@ class ImportController extends Controller
|
||||
], 400);
|
||||
}
|
||||
|
||||
$timezone = $this->user()->timezone;
|
||||
$report = $importService->import(
|
||||
$organization,
|
||||
$request->input('type'),
|
||||
$importData
|
||||
$importData,
|
||||
$timezone
|
||||
);
|
||||
|
||||
return new JsonResponse([
|
||||
|
||||
@@ -19,7 +19,7 @@ class ImportService
|
||||
/**
|
||||
* @throws ImportException
|
||||
*/
|
||||
public function import(Organization $organization, string $importerType, string $data): ReportDto
|
||||
public function import(Organization $organization, string $importerType, string $data, string $timezone): ReportDto
|
||||
{
|
||||
/** @var ImporterContract $importer */
|
||||
$importer = app(ImporterProvider::class)->getImporter($importerType);
|
||||
@@ -27,8 +27,8 @@ class ImportService
|
||||
Storage::disk(config('filesystems.default'))
|
||||
->put('import/'.Carbon::now()->toDateString().'-'.$organization->getKey().'-'.Str::uuid(), $data);
|
||||
|
||||
DB::transaction(function () use (&$importer, &$data) {
|
||||
$importer->importData($data);
|
||||
DB::transaction(function () use (&$importer, &$data, &$timezone) {
|
||||
$importer->importData($data, $timezone);
|
||||
});
|
||||
|
||||
return $importer->getReport();
|
||||
|
||||
@@ -15,7 +15,7 @@ class ClockifyProjectsImporter extends DefaultImporter
|
||||
* @throws ImportException
|
||||
*/
|
||||
#[\Override]
|
||||
public function importData(string $data): void
|
||||
public function importData(string $data, string $timezone): void
|
||||
{
|
||||
try {
|
||||
$reader = Reader::createFromString($data);
|
||||
|
||||
@@ -40,7 +40,7 @@ class ClockifyTimeEntriesImporter extends DefaultImporter
|
||||
* @throws ImportException
|
||||
*/
|
||||
#[\Override]
|
||||
public function importData(string $data): void
|
||||
public function importData(string $data, string $timezone): void
|
||||
{
|
||||
try {
|
||||
$reader = Reader::createFromString($data);
|
||||
@@ -106,9 +106,9 @@ class ClockifyTimeEntriesImporter extends DefaultImporter
|
||||
|
||||
// Start
|
||||
if (preg_match('/^[0-9]{1,2}:[0-9]{1,2} (AM|PM)$/', $record['Start Time']) === 1) {
|
||||
$start = Carbon::createFromFormat('m/d/Y h:i A', $record['Start Date'].' '.$record['Start Time'], 'UTC');
|
||||
$start = Carbon::createFromFormat('m/d/Y h:i A', $record['Start Date'].' '.$record['Start Time'], $timezone);
|
||||
} else {
|
||||
$start = Carbon::createFromFormat('m/d/Y H:i:s A', $record['Start Date'].' '.$record['Start Time'], 'UTC');
|
||||
$start = Carbon::createFromFormat('m/d/Y H:i:s A', $record['Start Date'].' '.$record['Start Time'], $timezone);
|
||||
}
|
||||
if ($start === null) {
|
||||
throw new ImportException('Start date ("'.$record['Start Date'].'") or time ("'.$record['Start Time'].'") are invalid');
|
||||
@@ -117,9 +117,9 @@ class ClockifyTimeEntriesImporter extends DefaultImporter
|
||||
|
||||
// End
|
||||
if (preg_match('/^[0-9]{1,2}:[0-9]{1,2} (AM|PM)$/', $record['End Time']) === 1) {
|
||||
$end = Carbon::createFromFormat('m/d/Y h:i A', $record['End Date'].' '.$record['End Time'], 'UTC');
|
||||
$end = Carbon::createFromFormat('m/d/Y h:i A', $record['End Date'].' '.$record['End Time'], $timezone);
|
||||
} else {
|
||||
$end = Carbon::createFromFormat('m/d/Y H:i:s A', $record['End Date'].' '.$record['End Time'], 'UTC');
|
||||
$end = Carbon::createFromFormat('m/d/Y H:i:s A', $record['End Date'].' '.$record['End Time'], $timezone);
|
||||
}
|
||||
if ($end === null) {
|
||||
throw new ImportException('End date ("'.$record['End Date'].'") or time ("'.$record['End Time'].'") are invalid');
|
||||
|
||||
@@ -10,7 +10,7 @@ interface ImporterContract
|
||||
{
|
||||
public function init(Organization $organization): void;
|
||||
|
||||
public function importData(string $data): void;
|
||||
public function importData(string $data, string $timezone): void;
|
||||
|
||||
public function getReport(): ReportDto;
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ class TogglDataImporter extends DefaultImporter
|
||||
* @throws ImportException
|
||||
*/
|
||||
#[\Override]
|
||||
public function importData(string $data): void
|
||||
public function importData(string $data, string $timezone): void
|
||||
{
|
||||
try {
|
||||
$zip = new ZipArchive();
|
||||
|
||||
@@ -40,7 +40,7 @@ class TogglTimeEntriesImporter extends DefaultImporter
|
||||
* @throws ImportException
|
||||
*/
|
||||
#[\Override]
|
||||
public function importData(string $data): void
|
||||
public function importData(string $data, string $timezone): void
|
||||
{
|
||||
try {
|
||||
$reader = Reader::createFromString($data);
|
||||
@@ -100,12 +100,12 @@ class TogglTimeEntriesImporter extends DefaultImporter
|
||||
}
|
||||
$timeEntry->billable = $record['Billable'] === 'Yes';
|
||||
$timeEntry->tags = $this->getTags($record['Tags']);
|
||||
$start = Carbon::createFromFormat('Y-m-d H:i:s', $record['Start date'].' '.$record['Start time'], 'UTC');
|
||||
$start = Carbon::createFromFormat('Y-m-d H:i:s', $record['Start date'].' '.$record['Start time'], $timezone);
|
||||
if ($start === null) {
|
||||
throw new ImportException('Start date ("'.$record['Start date'].'") or time ("'.$record['Start time'].'") are invalid');
|
||||
}
|
||||
$timeEntry->start = $start;
|
||||
$end = Carbon::createFromFormat('Y-m-d H:i:s', $record['End date'].' '.$record['End time'], 'UTC');
|
||||
$end = Carbon::createFromFormat('Y-m-d H:i:s', $record['End date'].' '.$record['End time'], $timezone);
|
||||
if ($end === null) {
|
||||
throw new ImportException('End date ("'.$record['End date'].'") or time ("'.$record['End time'].'") are invalid');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user