<commit_before><?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\DuesTransaction;

class DuesTransactionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $transact = DuesTransaction::all();
        return response()->json(['status' => 'success', 'dues_transactions' => $transact]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'received_polo' => 'boolean',
            'received_shirt' => 'boolean',
            'dues_package_id' => 'required|exists:dues_packages,id',
            'payment_id' => 'exists:payments,id',
            'user_id' => 'required|exists:users,id'
        ]);

        try {
            $transact = DuesTransaction::create($request->all());
        } catch (QueryException $e) {
            $errorMessage = $e->errorInfo[2];
            return response()->json(['status' => 'error', 'message' => $errorMessage], 500);
        }<commit_msg>#167 Creation of DuesTransaction allowed only for same user unless admin; Prevent creation of a duplicate DuesTransaction for a user<commit_after><?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\DuesTransaction;

class DuesTransactionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $transact = DuesTransaction::all();
        return response()->json(['status' => 'success', 'dues_transactions' => $transact]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'received_polo' => 'boolean',
            'received_shirt' => 'boolean',
            'dues_package_id' => 'required|exists:dues_packages,id',
            'payment_id' => 'exists:payments,id',
            'user_id' => 'exists:users,id'
        ]);
        
        $user = $request->user();
        $user_id = $request->input('user_id');
        
        //Make sure that the user is actually allowed to create this transaction
        if ($request->has('user_id') && $user_id != $user->id && (!$user->is_admin)) {
            return response()->json(['status' => 'error',
                'message' => 'You may not create a DuesTransaction for another user.'], 403);
        } elseif (!$request->has('user_id')) {
            $request->merge(['user_id' => $user->id]);
        }
        
        //Check to make sure there isn't already an existing package for the target user
        $existingTransaction = DuesTransaction::where('dues_package_id', $request->input('dues_package_id'))
            ->where('user_id', $request->input('user_id'))->first();
        if ($existingTransaction) {
            return response()->json(['status' => 'error',
                'message' => 'There is already a pending Dues Transaction for this user'], 400);
        }

        try {
            $transact = DuesTransaction::create($request->all());
        } catch (QueryException $e) {
            $errorMessage = $e->errorInfo[2];
            return response()->json(['status' => 'error', 'message' => $errorMessage], 500);
        }
{'fraction_non_alphanumeric': 0.14741810105496947, 'fraction_numerical': 0.004719600222098834, 'mean_word_length': 2.4845261121856868, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1556357', 'n_tokens_mistral': 1007, 'n_tokens_neox': 999, 'n_words': 234}