/** * Mark all timers that belong to the user (payee), * and are with a certain payer, as paid * * WARNING: Be careful, method not Restful! (Should be PUT /timers/{timer}) * * @param Request $request */ public function markAsPaid(Request $request) { $payer = Payer::findOrFail($request->get('payer_id')); $payee = Payee::find(Auth::user()->id); $project_ids = $payee->projects()->where('payer_id', $payer->id)->lists('id'); Timer::whereIn('project_id', $project_ids)->where('paid', 0)->update(['paid' => 1, 'time_of_payment' => Carbon::now()]); $message = Auth::user()->name . ' has marked all timers as paid.'; //Create a notification in the database for the payer, in case they are not currently logged in $notification = new Notification(['message' => $message]); $notification->user()->associate($payer->id); $notification->save(); //Pusher $pusher = new Pusher(env('PUSHER_PUBLIC_KEY'), env('PUSHER_SECRET_KEY'), env('PUSHER_APP_ID')); $data = ['payer_id' => $payer->id, 'payee_id' => $payee->id, 'notification' => $notification]; $pusher->trigger('channel', 'markAsPaid', $data); // @TODO Return collection of timers that have been modified }
/** * Remove a relationship between a payee and a payer, * and all associated projects * @TODO Should be a DELETE method to /users/{user}/payers/{payer} * @param Request $request * @return mixed */ public function removePayer(Request $request) { $payer = Payer::findOrFail($request->get('payer_id')); $payee = Payee::find(Auth::user()->id); //Remove the relationship between the payee and the payer //from the payee_payer table $payee->payers()->detach($payer->id); $payee->save(); //Remove projects the payee had with the payer /** * @VP: * Is there some way I could do this instead in the migrations file, * like with cascade on delete? * @JS: * Nope. */ $payee->projects()->where('payer_id', $payer->id)->delete(); }