mirror of
https://bitbucket.org/studiotem/jandl-taskmanager-app.git
synced 2026-10-21 14:29:34 +00:00
no message
This commit is contained in:
1 parent
9c7b176508
commit
a5f6fbd720
3 files changed
+144
-45
No files matched your search
@@ -11,6 +11,8 @@ use App\Models\SubTask;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class SubTaskController extends Controller
|
||||
@@ -412,8 +414,9 @@ class SubTaskController extends Controller
|
||||
$subtask->syncTimesheets($data['data']['timesheets']);
|
||||
}
|
||||
|
||||
// syncnutie timeblocks
|
||||
if (isset($data['data']['timeblocks'])) {
|
||||
// syncnutie timeblocks - PRE DRAFTY s viacerými zamestnancami NIE!
|
||||
// Timeblocks pre drafty s employees sú uložené v draft_data, nie v tabuľke timeblocks
|
||||
if (isset($data['data']['timeblocks']) && ! $isDraftWithMultipleEmployees) {
|
||||
$subtask->syncTimeblocks($data['data']['timeblocks']);
|
||||
}
|
||||
|
||||
@@ -470,48 +473,74 @@ class SubTaskController extends Controller
|
||||
$mastertask = $draft->mastertask;
|
||||
$createdSubtasks = [];
|
||||
|
||||
// Vytvoríme subtask pre každého zamestnanca
|
||||
foreach ($employees as $employee) {
|
||||
$subtaskData = [
|
||||
'user_id' => $employee['id'],
|
||||
'title' => $draft->title,
|
||||
'description' => $draft->description,
|
||||
'deadline' => $draft->deadline,
|
||||
'estimated_time' => $employee['duration'],
|
||||
'created_by' => $draft->created_by,
|
||||
'is_draft' => false,
|
||||
'draft_data' => null,
|
||||
];
|
||||
// Použijeme transakciu pre atomicitu operácie
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
|
||||
$newSubtask = $mastertask->subtasks()->create($subtaskData);
|
||||
// Vytvoríme subtask pre každého zamestnanca
|
||||
foreach ($employees as $employee) {
|
||||
$subtaskData = [
|
||||
'user_id' => $employee['id'],
|
||||
'title' => $draft->title,
|
||||
'description' => $draft->description,
|
||||
'deadline' => $draft->deadline,
|
||||
'estimated_time' => $employee['duration'],
|
||||
'created_by' => $draft->created_by,
|
||||
'is_draft' => false,
|
||||
'draft_data' => null,
|
||||
];
|
||||
|
||||
// Sync timeblocks ak existujú
|
||||
if (isset($employee['timeblocks']) && ! empty($employee['timeblocks'])) {
|
||||
$newSubtask->syncTimeblocks($employee['timeblocks']);
|
||||
$newSubtask = $mastertask->subtasks()->create($subtaskData);
|
||||
|
||||
// Sync timeblocks ak existujú - DÔLEŽITÉ: odstránime ID aby sa vytvorili nové záznamy
|
||||
if (isset($employee['timeblocks']) && ! empty($employee['timeblocks'])) {
|
||||
$cleanTimeblocks = array_map(function ($timeblock) {
|
||||
// Odstránime ID a sub_task_id, aby sa vytvorili nové záznamy
|
||||
unset($timeblock['id']);
|
||||
unset($timeblock['sub_task_id']);
|
||||
return $timeblock;
|
||||
}, $employee['timeblocks']);
|
||||
$newSubtask->createTimeblocks($cleanTimeblocks);
|
||||
}
|
||||
|
||||
// Skopírujeme attachmenty z pôvodného draftu
|
||||
foreach ($draft->attachments as $attachment) {
|
||||
$newSubtask->attachments()->create([
|
||||
'name' => $attachment->name,
|
||||
'path' => $attachment->path,
|
||||
'size' => $attachment->size,
|
||||
'type' => $attachment->type,
|
||||
]);
|
||||
}
|
||||
|
||||
$createdSubtasks[] = $newSubtask;
|
||||
}
|
||||
|
||||
// Skopírujeme attachmenty z pôvodného draftu
|
||||
foreach ($draft->attachments as $attachment) {
|
||||
$newSubtask->attachments()->create([
|
||||
'name' => $attachment->name,
|
||||
'path' => $attachment->path,
|
||||
'size' => $attachment->size,
|
||||
'type' => $attachment->type,
|
||||
]);
|
||||
}
|
||||
// Zmažeme pôvodný draft
|
||||
$draft->timeblocks()->delete();
|
||||
$draft->attachments()->delete(); // attachmenty sú už skopírované
|
||||
$draft->delete();
|
||||
|
||||
$createdSubtasks[] = $newSubtask;
|
||||
DB::commit();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Draft published successfully',
|
||||
'subtasks' => SubTaskResource::collection($createdSubtasks),
|
||||
], 201);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
Log::error('Failed to publish draft: ' . $e->getMessage(), [
|
||||
'draft_id' => $draft->id,
|
||||
'employees' => $employees,
|
||||
'exception' => $e,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Failed to publish draft. Please try again.',
|
||||
'error' => $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
|
||||
// Zmažeme pôvodný draft
|
||||
$draft->timeblocks()->delete();
|
||||
$draft->attachments()->delete(); // attachmenty sú už skopírované
|
||||
$draft->delete();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Draft published successfully',
|
||||
'subtasks' => SubTaskResource::collection($createdSubtasks),
|
||||
], 201);
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
|
||||
Reference in new issue
Block a user