162 lines
5.2 KiB
PHP
162 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\StoreMutationRequest;
|
|
use App\Models\Dynamic;
|
|
use App\Models\Ledger;
|
|
use App\Models\Mutation;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class MutationController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(StoreMutationRequest $request, Dynamic $dynamic, Ledger $ledger)
|
|
{
|
|
$isOwner = $dynamic->participants()
|
|
->where('user_id', $request->user()->id)
|
|
->where('role', 'owner')
|
|
->exists();
|
|
|
|
// If the user is an owner, default status to 'approved'. Otherwise default to 'pending'.
|
|
$status = $isOwner ? 'approved' : 'pending';
|
|
|
|
$mutation = DB::transaction(function () use ($request, $ledger, $status) {
|
|
$mutation = $ledger->mutations()->create([
|
|
...$request->except(['media', 'type', 'status']),
|
|
'user_id' => $request->user()->id,
|
|
'type' => $request->input('type', $request->input('amount') >= 0 ? 'addition' : 'subtraction'),
|
|
'status' => $status,
|
|
]);
|
|
|
|
if ($request->hasFile('media')) {
|
|
foreach ($request->file('media') as $file) {
|
|
$path = $file->store('media', 'public');
|
|
$mutation->media()->create([
|
|
'file_path' => $path,
|
|
'file_name' => $file->getClientOriginalName(),
|
|
'mime_type' => $file->getMimeType(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
// Only increment score if the status is approved!
|
|
if ($status === 'approved') {
|
|
$ledger->increment('score', $request->validated('amount'));
|
|
}
|
|
|
|
return $mutation;
|
|
});
|
|
|
|
// Broadcast the real-time creation event!
|
|
broadcast(new \App\Events\MutationCreated($mutation));
|
|
|
|
return redirect()->route('dynamics.ledgers.show', [$dynamic, $ledger]);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(Dynamic $dynamic, Ledger $ledger, Mutation $mutation)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(Dynamic $dynamic, Ledger $ledger, Mutation $mutation)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, Dynamic $dynamic, Ledger $ledger, Mutation $mutation)
|
|
{
|
|
// 1. Authorize - only owners can update mutation status!
|
|
$isOwner = $dynamic->participants()
|
|
->where('user_id', $request->user()->id)
|
|
->where('role', 'owner')
|
|
->exists();
|
|
|
|
if (!$isOwner) {
|
|
abort(403, 'Only dynamic owners can approve or reject mutations.');
|
|
}
|
|
|
|
$request->validate([
|
|
'status' => ['required', 'string', 'in:approved,rejected'],
|
|
]);
|
|
|
|
$oldStatus = $mutation->status;
|
|
$newStatus = $request->input('status');
|
|
|
|
DB::transaction(function () use ($mutation, $ledger, $oldStatus, $newStatus) {
|
|
$mutation->update(['status' => $newStatus]);
|
|
|
|
// Adjust the ledger score if status transitions to approved or from approved!
|
|
if ($oldStatus !== 'approved' && $newStatus === 'approved') {
|
|
$ledger->increment('score', $mutation->amount);
|
|
} elseif ($oldStatus === 'approved' && $newStatus !== 'approved') {
|
|
$ledger->decrement('score', $mutation->amount);
|
|
}
|
|
});
|
|
|
|
// Log to Mutation and Dynamic chats
|
|
$user = $request->user();
|
|
$statusText = strtoupper($newStatus);
|
|
|
|
$mutationMsg = $mutation->chat->messages()->create([
|
|
'user_id' => $user->id,
|
|
'content' => "System: Suggestion was {$statusText} by {$user->name}.",
|
|
]);
|
|
broadcast(new \App\Events\MessageSent($mutationMsg));
|
|
|
|
if ($newStatus === 'approved') {
|
|
$dynamicMsg = $dynamic->chat->messages()->create([
|
|
'user_id' => $user->id,
|
|
'content' => "System: {$user->name} APPROVED the suggestion \"{$mutation->description}\" for " . ($mutation->amount >= 0 ? '+' : '') . "{$mutation->amount} points on \"{$ledger->name}\" ledger.",
|
|
]);
|
|
} else {
|
|
$dynamicMsg = $dynamic->chat->messages()->create([
|
|
'user_id' => $user->id,
|
|
'content' => "System: {$user->name} REJECTED the suggestion \"{$mutation->description}\" on \"{$ledger->name}\" ledger.",
|
|
]);
|
|
}
|
|
broadcast(new \App\Events\MessageSent($dynamicMsg));
|
|
|
|
// Broadcast the real-time update event!
|
|
broadcast(new \App\Events\MutationUpdated($mutation));
|
|
|
|
return redirect()->back();
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Dynamic $dynamic, Ledger $ledger, Mutation $mutation)
|
|
{
|
|
//
|
|
}
|
|
}
|