Connect CalDAV todos to DB-Items, finish todo component and add it to pipeline items
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
use App\Models\PipelineItem;
|
||||
use App\Support\ApiDataTransformer;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class PipelineItemController extends Controller
|
||||
{
|
||||
@@ -13,11 +14,24 @@ class PipelineItemController extends Controller
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$pipelineItems = PipelineItem::withCount(['notes' => function ($query) {
|
||||
$query->where('notable_type', 'App\Models\PipelineItem');
|
||||
}])->orderBy('position')->get();
|
||||
$pipelineItems = PipelineItem
|
||||
::withCount(['notes' => function ($query) {
|
||||
$query->where('notable_type', 'App\Models\PipelineItem');
|
||||
}])
|
||||
->withCount(['todos' => function ($query) {
|
||||
$query->where('todoable_type', 'App\Models\PipelineItem');
|
||||
}])
|
||||
->orderBy('position')
|
||||
->get();
|
||||
|
||||
return ApiDataTransformer::snakeToCamel($pipelineItems->toArray());
|
||||
$pipelineItemsArray = $pipelineItems->map(function ($item) {
|
||||
$itemArray = $item->toArray();
|
||||
$itemArray['next_todo_due_date'] = $item->todos->first()?->due_date?->toISOString() ?? null;
|
||||
unset($itemArray['todos']); // Remove the todos array
|
||||
return $itemArray;
|
||||
});
|
||||
|
||||
return ApiDataTransformer::snakeToCamel($pipelineItemsArray->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -25,9 +39,12 @@ public function index()
|
||||
*/
|
||||
public function single(int $id)
|
||||
{
|
||||
$pipelineItem = PipelineItem::withCount(['notes' => function ($query) {
|
||||
$query->where('notable_type', 'App\Models\PipelineItem');
|
||||
}])->orderBy('position')->findOrFail($id);
|
||||
$pipelineItem = PipelineItem
|
||||
::with(['notes' => function ($query) {
|
||||
$query->where('notable_type', 'App\Models\PipelineItem')->orderBy('created_at', 'desc');
|
||||
}])->with(['todos' => function ($query) {
|
||||
$query->where('todoable_type', 'App\Models\PipelineItem')->orderBy('due_date', 'asc');
|
||||
}])->orderBy('position')->findOrFail($id);
|
||||
|
||||
return ApiDataTransformer::snakeToCamel($pipelineItem->toArray());
|
||||
}
|
||||
@@ -38,11 +55,11 @@ public function single(int $id)
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validatedData = $request->validate([
|
||||
'pipeline_lane_id' => 'required|integer|exists:pipeline_lanes,id',
|
||||
'pipelineLaneId' => 'required|integer|exists:pipeline_lanes,id',
|
||||
'title' => 'required|string',
|
||||
'position' => 'required|integer|min:0',
|
||||
'expected_revenue' => 'nullable|numeric',
|
||||
'due_date' => 'nullable|date',
|
||||
'expectedRevenue' => 'nullable|numeric',
|
||||
'dueDate' => 'nullable|date',
|
||||
'description' => 'nullable|string',
|
||||
]);
|
||||
|
||||
@@ -54,20 +71,31 @@ public function store(Request $request)
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, PipelineItem $pipelineItem)
|
||||
public function update(Request $request, int $id)
|
||||
{
|
||||
$validatedData = $request->validate([
|
||||
'pipeline_lane_id' => 'sometimes|integer|exists:pipeline_lanes,id',
|
||||
'pipelineLaneId' => 'sometimes|integer|exists:pipeline_lanes,id',
|
||||
'title' => 'sometimes|string',
|
||||
'position' => 'sometimes|integer|min:0',
|
||||
'expected_revenue' => 'nullable|numeric',
|
||||
'due_date' => 'nullable|date',
|
||||
'expectedRevenue' => 'nullable|numeric',
|
||||
'dueDate' => 'nullable|date',
|
||||
'description' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$pipelineItem->update($validatedData);
|
||||
$snakeCaseData = ApiDataTransformer::camelToSnake($validatedData);
|
||||
|
||||
return ApiDataTransformer::snakeToCamel($pipelineItem->toArray());
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
$pipelineItem = PipelineItem::findOrFail($id);
|
||||
$snakeCaseData = ApiDataTransformer::camelToSnake($validatedData);
|
||||
$pipelineItem->update($snakeCaseData);
|
||||
DB::commit();
|
||||
return response()->noContent();
|
||||
} catch (\Exception $e) {
|
||||
DB::rollBack();
|
||||
return response()->json(['error' => 'Failed to update pipeline item', 'message' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user