SyntaxSquad/DADProject#16 fix: i forgot this
This commit is contained in:
@@ -255,7 +255,6 @@ class MatchController extends Controller
|
||||
|
||||
public function open(Request $request)
|
||||
{
|
||||
// FIX: Allow matches where P2 is NULL OR 'Ghost' (ID 1)
|
||||
$query = MatchGame::where('status', 'Pending')
|
||||
->where(function($q) {
|
||||
$q->whereNull('player2_user_id')
|
||||
@@ -272,4 +271,54 @@ class MatchController extends Controller
|
||||
|
||||
return response()->json($matches);
|
||||
}
|
||||
|
||||
// --- FIX: Safely Delete Match by removing dependencies first ---
|
||||
public function destroy(MatchGame $match)
|
||||
{
|
||||
$user = request()->user();
|
||||
|
||||
if ($match->player1_user_id !== $user->id) {
|
||||
return response()->json(['message' => 'Unauthorized'], 403);
|
||||
}
|
||||
|
||||
if ($match->status !== 'Pending') {
|
||||
return response()->json(['message' => 'Cannot cancel a match that has already started'], 400);
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($match, $user) {
|
||||
// 1. Delete associated Games first (The FK constraint cause)
|
||||
DB::table('games')->where('match_id', $match->id)->delete();
|
||||
|
||||
// 2. Unlink existing Coin Transactions (Set match_id to NULL)
|
||||
CoinTransaction::where('match_id', $match->id)->update(['match_id' => null]);
|
||||
|
||||
// 3. Process Refund
|
||||
if ($match->stake > 0) {
|
||||
$user->increment('coins_balance', $match->stake);
|
||||
|
||||
$refundType = CoinTransactionType::firstOrCreate(
|
||||
['name' => 'Refund'],
|
||||
['type' => 'C']
|
||||
);
|
||||
|
||||
// Create Refund Transaction with NO LINK to the deleted match
|
||||
CoinTransaction::create([
|
||||
'user_id' => $user->id,
|
||||
'match_id' => null, // IMPORTANT: Must be null
|
||||
'coin_transaction_type_id' => $refundType->id,
|
||||
'transaction_datetime' => now(),
|
||||
'coins' => $match->stake,
|
||||
'custom' => "Refund for Match #{$match->id}"
|
||||
]);
|
||||
}
|
||||
|
||||
// 4. Finally delete the match
|
||||
$match->delete();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Match canceled and refunded',
|
||||
'balance' => $user->coins_balance
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,17 +4,17 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class CoinTransactionType extends Model
|
||||
{
|
||||
use HasFactory, SoftDeletes;
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'coin_transaction_types';
|
||||
// FIX: Disable auto-timestamps because the table doesn't have updated_at/created_at columns
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = ['name', 'type', 'custom'];
|
||||
|
||||
protected $casts = [
|
||||
'custom' => 'array',
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'type',
|
||||
'description' // Include description if your table has it
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user