Skip to content

Commit

Permalink
response for transaction FK constraint on account deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
MHamzaBham committed Dec 24, 2024
1 parent cb7e52c commit 89b7f21
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion app/Http/Controllers/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,24 @@ public function update(Request $request, $id)
public function destroy(Account $account)
{
try {
$hasTransactions = \DB::table('transactions')
->where('src_account_id', $account->id)
->orWhere('dest_account_id', $account->id)
->exists();

if ($hasTransactions) {
return response()->json([
'success' => false,
'message' => 'Cannot delete an account associated with a transaction'
]);
}

$account->delete();

return response()->json([
'success' => true,
'message' => 'Account deleted successfully.'
]);

} catch (\Exception $e) {
return response()->json([
'success' => false,
Expand Down
2 changes: 1 addition & 1 deletion public/assets/js/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ function deleteAccount(accountId) {
} else {
swalWithBootstrapButtons.fire({
title: "Failed",
text: "Failed to delete the account. Please try again.",
text: response.message,
icon: "error"
});
}
Expand Down

0 comments on commit 89b7f21

Please sign in to comment.