/** * Log user in * * @param LoginRequest $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ public function login(LoginRequest $request) { $response = new AjaxResponse(); // Get inputs $email = $request->get('email'); $password = $request->get('password'); $userId = User::where('email', $email)->value('id'); // todo check for login attempts // Check if credentials are ok if ($this->auth->attempt(['email' => $email, 'password' => $password, 'active' => 1])) { event(new UserLoggedIn($this->auth->user()->id)); $response->setSuccessMessage(trans('common.success')); return response($response->get())->header('Content-Type', 'application/json'); } // If email exists in database log the login attempt if ($userId) { event(new FailedLogIn($userId)); } $response->setFailMessage(trans('login.login_failed')); return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json'); }
/** * Get product details. * * @param string $productCode * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response */ public static function details($productCode) { $response = new AjaxResponse(); $isApplicationProduct = false; // Check if is in products table $product = Product::where('user_id', Auth::user()->id)->where('code', $productCode)->first(); if (!$product) { $product = ApplicationProduct::where('code', $productCode)->first(); $isApplicationProduct = true; } // Check if is in application_products table if (!$product) { $response->setFailMessage('not found'); return response($response->get(), $response->getDefaultErrorResponseCode()); } $response->setSuccessMessage('ok'); if ($isApplicationProduct) { $data = ['id' => $product->id, 'code' => $product->code, 'name' => $product->name, 'created_at' => $product->created_at, 'sold_pieces' => self::productSoldPieces($product->id), 'total_price' => self::productTotalPrice($product->id), 'paid_bills' => self::paidBillsThatContainProduct($product->id), 'not_paid_bills' => self::notPaidBillsThatContainProduct($product->id), 'is_application_product' => $isApplicationProduct]; $response->addExtraFields($data); return response($response->get()); } $response->addExtraFields(['id' => $product->id, 'code' => $product->code, 'name' => $product->name, 'created_at' => $product->created_at, 'sold_pieces' => self::productSoldPieces($product->id, true), 'total_price' => self::productTotalPrice($product->id, true), 'paid_bills' => self::paidBillsThatContainProduct($product->id, true), 'not_paid_bills' => self::notPaidBillsThatContainProduct($product->id, true), 'is_application_product' => $isApplicationProduct]); return response($response->get()); }
/** * Delete client. * * @param DeleteClientRequest $request * @param int $clientId * @return array */ public function delete($clientId, DeleteClientRequest $request) { $response = new AjaxResponse(); $table = 'clients'; // Count rows, delete record and count rows after the operation $initialRows = DB::table($table)->where('user_id', Auth::user()->id)->count(); DB::table($table)->where('id', $clientId)->where('user_id', Auth::user()->id)->delete(); $finalRows = DB::table($table)->where('user_id', Auth::user()->id)->count(); // Check if record was deleted or not and return a success or error response if ($finalRows < $initialRows) { $response->setSuccessMessage(trans('clients.client_deleted')); return response($response->get()); } $response->setFailMessage(trans('common.delete_error')); return response($response->get(), $response->getDefaultErrorResponseCode()); }
public function getUserData($userId) { $response = new AjaxResponse(); if (DB::table('users')->where('id', $userId)->count()) { $response->setSuccessMessage(trans('common.success')); $response->addExtraFields(['user' => DB::table('users')->where('id', $userId)->select('email', 'active')->first()]); return response($response->get())->header('Content-Type', 'application/json'); } $response->setFailMessage(trans('common.error')); return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json'); }
/** * Edit user password. * * @param EditUserPasswordRequest $request * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response */ public function editPassword(EditUserPasswordRequest $request) { $response = new AjaxResponse(); // Check if current password is ok if (!Hash::check($request->get('password'), Auth::user()->password)) { $response->setFailMessage(trans('settings.invalid_password')); return response($response->get(), $response->getDefaultErrorResponseCode()); } User::where('id', Auth::user()->id)->update(['password' => bcrypt($request->get('new_password'))]); $response->setSuccessMessage(trans('settings.password_updated')); return response($response->get()); }
/** * Handle database operations to edit a bill product. * * @param array $data * @option int billId * @option int productId * @option int billProductId * @option string productCode * @option string columnToUpdate * @option string newValue * @return mixed */ public static function handleBillProductEdit($data = []) { $response = new AjaxResponse(); // Query for bill $bill = Auth::user()->bills()->where('id', $data['billId'])->first(); // Now make sure exists in database if (!$bill) { $response->setFailMessage(trans('common.general_error')); return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json'); } // Make sure bill product exists and belongs to current user if (!Product::where('id', $data['productId'])->count() && !ApplicationProduct::where('id', $data['productId'])->count()) { $response->setFailMessage(trans('bill.product_not_found')); return response($response->get(), 404)->header('Content-Type', 'application/json'); } // Make sure bill product belongs to current user if (!BillProduct::where('id', $data['billProductId'])->count() && !BillApplicationProduct::where('id', $data['billProductId'])->count()) { $response->setFailMessage(trans('bill.bill_product_not_found')); return response($response->get(), 404)->header('Content-Type', 'application/json'); } // We will use this variable to check if operation was successful $success = false; // Check if is a custom product if (Products::isCustomProduct($data['productId'], $data['productCode'])) { // Get product details and update with new data $product = BillProduct::where('id', $data['billProductId'])->first(); BillProduct::where('id', $data['billProductId'])->update(Bills::getDataToUpdateOnEdit($data['columnToUpdate'], $data['newValue'], $product)); $success = true; } // Check if is an application product if (Products::isApplicationProduct($data['productId'], $data['productCode'])) { // Get product details and update with new data $product = BillApplicationProduct::where('id', $data['billProductId'])->first(); BillApplicationProduct::where('id', $data['billProductId'])->update(Bills::getDataToUpdateOnEdit($data['columnToUpdate'], $data['newValue'], $product)); $success = true; } // Check if update was successful if ($success) { $response->setSuccessMessage(trans('bill.' . $data['columnToUpdate'] . '_updated')); return response($response->get())->header('Content-Type', 'application/json'); } // If we arrive here something is wrong $response->setFailMessage(trans('common.general_error')); return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json'); }
/** * Edit article title and content. * * @param int $categoryId * @param EditArticleRequest $request * @return mixed */ public function editArticle($categoryId, EditArticleRequest $request) { $response = new AjaxResponse(); $category = HelpCenterCategory::where('id', $categoryId)->first(); if (!$category) { $response->setFailMessage(trans('help_center.category_not_found')); return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json'); } // Edit article $article = HelpCenterArticle::find($request->get('article_id')); $article->title = $request->get('article_title'); $article->content = $request->get('article_content'); $article->save(); // Get updated version of articles $extraFields = []; $articles = HelpCenterManagerHelper::getCategoryArticles($categoryId); if (count($articles)) { $extraFields['articles'] = $articles; } $response->setSuccessMessage(trans('help_center.article_updated')); $response->addExtraFields($extraFields); return response($response->get())->header('Content-Type', 'application/json'); }
/** * Check if a product code is already used by application products or user products * * @param string $code * @return mixed */ public function checkProductCode($code) { $response = new AjaxResponse(); // Validation rules $validator = Validator::make(['code' => $code], ['code' => ['required', 'digits:5']]); // Run validator if ($validator->fails()) { $response->setFailMessage($this->getValidatorFirstErrorMessage($validator->messages())); return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json'); } // Check if product code is available if ($this->isProductCodeAlreadyUsed($code)) { $response->setFailMessage(trans('my_products.product_code_used')); return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json'); } $response->setSuccessMessage(trans('my_products.product_code_available')); return response($response->get())->header('Content-Type', 'application/json'); }