added media, mutation events, agent instructions
This commit is contained in:
@@ -32,15 +32,44 @@ class MutationController extends Controller
|
||||
*/
|
||||
public function store(StoreMutationRequest $request, Dynamic $dynamic, Ledger $ledger)
|
||||
{
|
||||
DB::transaction(function () use ($request, $ledger) {
|
||||
$ledger->mutations()->create([
|
||||
...$request->validated(),
|
||||
$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,
|
||||
]);
|
||||
|
||||
$ledger->increment('score', $request->validated('amount'));
|
||||
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]);
|
||||
}
|
||||
|
||||
@@ -65,7 +94,61 @@ class MutationController extends Controller
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user