Connect CalDAV todos to DB-Items, finish todo component and add it to pipeline items
This commit is contained in:
@@ -7,13 +7,14 @@
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use App\Services\CaldavService;
|
||||
use App\Models\Todo;
|
||||
use App\Models\PipelineItem;
|
||||
|
||||
class CaldavSyncCommand extends Command
|
||||
{
|
||||
protected $signature = 'caldav:sync';
|
||||
protected $description = 'Sync CalDAV VTODOs into local todos table';
|
||||
|
||||
public function handle(CaldavService $service)
|
||||
public function handle(CaldavService $service)
|
||||
{
|
||||
// only run every 5 minutes although the task is called every minute
|
||||
$cacheKey = 'caldav_sync_last_run';
|
||||
@@ -30,9 +31,53 @@ public function handle(CaldavService $service)
|
||||
|
||||
$count = 0;
|
||||
foreach ($todos as $todo) {
|
||||
Todo::upsert($todo->attributesToArray(), 'id');
|
||||
// Only update the fields that are present in CalDAV
|
||||
$data = $todo->attributesToArray();
|
||||
$data = array_intersect_key($data, array_flip([
|
||||
'id',
|
||||
'etag',
|
||||
'title',
|
||||
'description',
|
||||
'type_id',
|
||||
'url',
|
||||
'due_date',
|
||||
'recurring',
|
||||
'priority',
|
||||
'status',
|
||||
'created_at',
|
||||
'last_modified',
|
||||
'parent',
|
||||
'object',
|
||||
]));
|
||||
|
||||
// Parse the title to extract the todoable title and todo title
|
||||
if (isset($data['title'])) {
|
||||
$titleParts = explode('] ', $data['title'], 2);
|
||||
if (count($titleParts) === 2) {
|
||||
$todoableTitle = trim($titleParts[0], '[]');
|
||||
$todoTitle = $titleParts[1];
|
||||
|
||||
// Find the todoable model by title
|
||||
$models = [
|
||||
'PipelineItem' => PipelineItem::class,
|
||||
// Add other models here as needed
|
||||
];
|
||||
|
||||
foreach ($models as $modelName => $modelClass) {
|
||||
$todoable = $modelClass::where('title', $todoableTitle)->first();
|
||||
if ($todoable) {
|
||||
$data['todoable_type'] = 'App\\Models\\' . $modelName;
|
||||
$data['todoable_id'] = $todoable->id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Todo::upsert($data, 'id');
|
||||
$count++;
|
||||
}
|
||||
|
||||
// Collect hrefs/URLs returned by the CalDAV server so we can remove local
|
||||
// todos that belong to this calendar but were deleted on the server.
|
||||
$hrefs = array_values(array_filter(array_map(function ($t) {
|
||||
|
||||
@@ -14,7 +14,7 @@ public function index($invoiceId)
|
||||
$items = LineItem::with('unit')
|
||||
->select('line_items.*')
|
||||
->where('invoice_id', $invoiceId)
|
||||
->orderBy('position', 'desc')
|
||||
->orderBy('position', 'asc')
|
||||
->get();
|
||||
|
||||
return $items->map(function ($item) {
|
||||
|
||||
@@ -9,6 +9,15 @@
|
||||
|
||||
class NoteController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$notes = Note::with('user')->orderBy('created_at', 'desc')->get();
|
||||
return ApiDataTransformer::snakeToCamel($notes->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
* @param Request $request
|
||||
@@ -16,7 +25,7 @@ class NoteController extends Controller
|
||||
* @param string $modelId The ID of the model
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function index(Request $request, string $modelType, int $modelId)
|
||||
public function notesForModel(Request $request, string $modelType, int $modelId)
|
||||
{
|
||||
$model = app("App\\Models\\" . $modelType)::findOrFail($modelId);
|
||||
|
||||
|
||||
@@ -16,15 +16,38 @@ class PipelineController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$lanes = PipelineLane::with(['items' => function ($q) {
|
||||
$q->withCount(['notes' => function ($query) {
|
||||
$query->where('notable_type', 'App\Models\PipelineItem');
|
||||
}])->orderBy('position');
|
||||
}])->orderBy('position')->get();
|
||||
$lanes = PipelineLane::with(['items' => function ($query) {
|
||||
$query
|
||||
->withCount(['notes' => function ($query) {
|
||||
$query->where('notable_type', 'App\Models\PipelineItem');
|
||||
}])
|
||||
->withCount(['todos' => function ($query) {
|
||||
$query->where('todoable_type', 'App\Models\PipelineItem')
|
||||
->whereNotIn('status', ['completed', 'COMPLETED']);
|
||||
}])
|
||||
->with(['todos' => function ($query) {
|
||||
$query->where('todoable_type', 'App\Models\PipelineItem')->orderBy('due_date', 'asc');
|
||||
}])
|
||||
->orderBy('position')
|
||||
;
|
||||
}])
|
||||
->orderBy('position')
|
||||
->get();
|
||||
|
||||
return $lanes->map(function ($lane) {
|
||||
return ApiDataTransformer::snakeToCamel($lane->toArray());
|
||||
$lanesArray = $lanes->map(function ($lane) {
|
||||
$laneArray = $lane->toArray();
|
||||
|
||||
// Process items to add latest_todo_due_date and remove todos array
|
||||
$laneArray['items'] = collect($laneArray['items'])->map(function ($item) {
|
||||
$item['next_todo_due_date'] = $item['todos'][0]['due_date'] ?? null;
|
||||
unset($item['todos']); // Remove the todos array
|
||||
return $item;
|
||||
})->toArray();
|
||||
|
||||
return $laneArray;
|
||||
});
|
||||
|
||||
return ApiDataTransformer::snakeToCamel($lanesArray->toArray());
|
||||
}
|
||||
|
||||
public function show()
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,7 +15,29 @@ public function index()
|
||||
return ApiDataTransformer::snakeToCamel($todos->toArray());
|
||||
}
|
||||
|
||||
public function show(string $id)
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
* @param Request $request
|
||||
* @param string $modelType The type of the model (e.g., 'Customer', 'Invoice')
|
||||
* @param string $modelId The ID of the model
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function todosForModel(Request $request, string $modelType, int $modelId)
|
||||
{
|
||||
$model = app("App\\Models\\" . $modelType)::findOrFail($modelId);
|
||||
|
||||
// Load all todos of the model with the user relationship
|
||||
$todos = $model->todos()->with('type')->orderBy('created_at', 'desc')->get();
|
||||
|
||||
// Transformiere die Daten in camelCase
|
||||
$notesArray = $todos->map(function ($todo) {
|
||||
return ApiDataTransformer::snakeToCamel($todo->toArray());
|
||||
});
|
||||
|
||||
return response()->json($notesArray);
|
||||
}
|
||||
|
||||
public function single(string $id)
|
||||
{
|
||||
return Todo::with('type')->findOrFail($id);
|
||||
}
|
||||
@@ -35,6 +57,8 @@ public function store(Request $request)
|
||||
'status' => 'nullable|string',
|
||||
'parent' => 'nullable|string',
|
||||
'object' => 'nullable|string',
|
||||
'todoable_id' => 'nullable|integer',
|
||||
'todoable_type' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$data['id'] = $data['id'] ?? (string) Str::uuid();
|
||||
@@ -42,6 +66,19 @@ public function store(Request $request)
|
||||
$data['created_at'] = $data['created_at'] ?? $now;
|
||||
$data['last_modified'] = $now;
|
||||
|
||||
// Set the title with the todoable title if todoable_id and todoable_type are set
|
||||
if (isset($data['todoable_id']) && isset($data['todoable_type'])) {
|
||||
$modelName = str_replace('App\\Models\\', '', $data['todoable_type']);
|
||||
$modelClass = 'App\\Models\\' . $modelName;
|
||||
|
||||
if (class_exists($modelClass)) {
|
||||
$todoable = $modelClass::find($data['todoable_id']);
|
||||
if ($todoable) {
|
||||
$data['title'] = '[' . $todoable->title . '] ' . $data['title'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$todo = Todo::create($data);
|
||||
|
||||
return response()->json($todo, 201);
|
||||
@@ -63,10 +100,25 @@ public function update(Request $request, string $id)
|
||||
'status' => 'nullable|string',
|
||||
'parent' => 'nullable|string',
|
||||
'object' => 'nullable|string',
|
||||
'todoable_id' => 'nullable|integer',
|
||||
'todoable_type' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$data['last_modified'] = now();
|
||||
|
||||
// Set the title with the todoable title if todoable_id and todoable_type are set
|
||||
if (isset($data['todoable_id']) && isset($data['todoable_type'])) {
|
||||
$modelName = str_replace('App\\Models\\', '', $data['todoable_type']);
|
||||
$modelClass = 'App\\Models\\' . $modelName;
|
||||
|
||||
if (class_exists($modelClass)) {
|
||||
$todoable = $modelClass::find($data['todoable_id']);
|
||||
if ($todoable) {
|
||||
$data['title'] = '[' . $todoable->title . '] ' . $data['title'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$todo->update($data);
|
||||
|
||||
return response()->json($todo);
|
||||
|
||||
@@ -39,4 +39,12 @@ public function notes()
|
||||
{
|
||||
return $this->morphMany(Note::class, 'notable');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the todos
|
||||
*/
|
||||
public function todos()
|
||||
{
|
||||
return $this->morphMany(Todo::class, 'todoable');
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -31,6 +31,8 @@ class Todo extends Model
|
||||
'last_modified',
|
||||
'parent',
|
||||
'object',
|
||||
'todoable_id',
|
||||
'todoable_type'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
@@ -53,4 +55,4 @@ public function children()
|
||||
{
|
||||
return $this->hasMany(self::class, 'parent');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user