Initial commit - construprogress app
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\PendingSync;
|
||||
use App\Models\Phase;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class OfflineSyncController extends Controller
|
||||
{
|
||||
public function storePending(Request $request)
|
||||
{
|
||||
$payload = $request->validate([
|
||||
'action' => 'required|in:progress_update,task_complete',
|
||||
'payload' => 'required|array',
|
||||
]);
|
||||
$pending = PendingSync::create([
|
||||
'user_id' => Auth::id() ?? 1,
|
||||
'action' => $payload['action'],
|
||||
'payload' => $payload['payload'],
|
||||
]);
|
||||
return response()->json(['queued' => true]);
|
||||
}
|
||||
|
||||
public function sync(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
$pendings = PendingSync::where('user_id', $user->id)->whereNull('synced_at')->get();
|
||||
foreach ($pendings as $pending) {
|
||||
if ($pending->action === 'progress_update') {
|
||||
$phase = Phase::find($pending->payload['phase_id']);
|
||||
if ($phase) {
|
||||
$phase->progress_percent = $pending->payload['progress'];
|
||||
$phase->save();
|
||||
$phase->progressUpdates()->create([
|
||||
'user_id' => $user->id,
|
||||
'progress_percent' => $pending->payload['progress'],
|
||||
'comment' => $pending->payload['comment'] ?? '',
|
||||
'location' => $pending->payload['location'] ?? null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
$pending->synced_at = now();
|
||||
$pending->save();
|
||||
}
|
||||
return response()->json(['synced' => count($pendings)]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user