Ejemplo n.º 1
0
 /**
  * @param array $errors
  * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  */
 public function response(array $errors)
 {
     $response = new AjaxResponse();
     $response->setFailMessage('error');
     $response->addExtraFields(['errors' => $errors]);
     return response($response->get(), $response->badRequest())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 2
0
 /**
  * @param array $errors
  * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  */
 public function response(array $errors)
 {
     $response = new AjaxResponse();
     $response->setFailMessage('error');
     $response->addExtraFields(['errors' => $errors]);
     return response($response->get(), $response->badRequest());
 }
Ejemplo n.º 3
0
 /**
  * Get product data.
  *
  * @param int $productId
  * @param string $productCode
  * @param GetProductRequest $request
  * @param AjaxResponse $response
  * @return mixed
  */
 public function get($productId, $productCode, GetProductRequest $request, AjaxResponse $response)
 {
     // Make sure product exists
     if (!ApplicationProduct::where('code', $productCode)->where('id', $productId)->count()) {
         $response->setFailMessage(trans('common.general_error'));
         return response($response->get())->header('Content-Type', 'application/json');
     }
     $response->setSuccessMessage(trans('common.success'));
     $response->addExtraFields(['product' => ProductsManagerHelper::productDetails($productCode, $productId)]);
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 4
0
 /**
  * Handle given targeted group.
  *
  * @param string $targetedGroup
  * @param int $notificationId
  * @return mixed
  */
 public static function handle($targetedGroup, $notificationId)
 {
     $response = new AjaxResponse();
     $response->setSuccessMessage(trans('notifications.targeted_users_set'));
     // Handle case when all users are targeted
     if ($targetedGroup === self::$targetGroups['all']) {
         self::_handleAll($notificationId);
         return response($response->get())->header('Content-Type', 'application/json');
     }
     // Handle case when no user is targeted
     if ($targetedGroup === self::$targetGroups['none']) {
         self::_handleNone($notificationId);
         return response($response->get())->header('Content-Type', 'application/json');
     }
     $response->setFailMessage(trans('common.general_error'));
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 5
0
 /**
  * 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');
 }
Ejemplo n.º 6
0
 /**
  * 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());
 }
Ejemplo n.º 7
0
 /**
  * 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());
 }
Ejemplo n.º 8
0
 /**
  * Allow admin to delete user account.
  *
  * @param int $userId
  * @param DeleteUserAccountRequest $request
  * @return mixed
  */
 public function deleteUserAccount($userId, DeleteUserAccountRequest $request)
 {
     $response = new AjaxResponse();
     // Make sure user id exists
     if (!User::where('id', $userId)->count()) {
         $response->setFailMessage(trans('users_manager.user_not_found'));
         return response($response->get())->header('Content-Type', 'application/json');
     }
     Subscription::where('user_id', $userId)->delete();
     User::where('id', $userId)->delete();
     $response->setSuccessMessage(trans('users_manager.account_deleted'));
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 9
0
 /**
  * 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());
 }
Ejemplo n.º 10
0
 /**
  * @param int $userId
  * @param string $code
  * @param SetNewPasswordRequest $request
  * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  */
 public function setNewPassword($userId, $code, SetNewPasswordRequest $request)
 {
     $response = new AjaxResponse();
     $recover = RecoverCode::where('user_id', $userId)->where('code', $code)->valid()->first();
     if (!$recover) {
         $response->setFailMessage(trans('common.general_error'));
         return response($response->get(), $response->badRequest());
     }
     $user = User::find($userId);
     if (!$user) {
         // User not found
         $response->setFailMessage(trans('common.general_error'));
         return response($response->get(), $response->badRequest());
     }
     User::where('id', $userId)->update(['password' => bcrypt($request->get('new_password'))]);
     $response->setSuccessMessage(trans('recover.password_updated'));
     return response($response->get());
 }
Ejemplo n.º 11
0
 /**
  * Disable offer.
  *
  * @param int $offerId
  * @param DisableOfferRequest $request
  * @return mixed
  */
 public function disableOffer($offerId, DisableOfferRequest $request)
 {
     $offer = Offer::find($offerId);
     $response = new AjaxResponse();
     // Make sure offer exists
     if (!$offer) {
         $response->setFailMessage(trans('offers.offer_not_found'));
         return response($response->get());
     }
     $offer->disabled = true;
     $offer->save();
     $response->setSuccessMessage(trans('offers.offer_disabled'));
     $response->addExtraFields(['offer' => Offer::countAssociatedSubscriptions()->where('offers.id', $offerId)->first()]);
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 12
0
 /**
  * Mark bill as unpaid.
  *
  * @param int $billId
  * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  */
 public static function markAsUnpaid($billId)
 {
     $response = new AjaxResponse();
     // Make sure bill exists
     if (!Bill::where('id', $billId)->where('user_id', Auth::user()->id)->count()) {
         $response->setFailMessage(trans('bill.bill_not_found'));
         return response($response->get(), 404)->header('Content-Type', 'application/json');
     }
     Auth::user()->bills()->where('id', $billId)->update(['paid' => 0]);
     $response->setSuccessMessage(trans('bill.marked_as_unpaid'));
     $response->addExtraFields(['paid' => 0]);
     return response($response->get());
 }
Ejemplo n.º 13
0
 /**
  * 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');
 }
Ejemplo n.º 14
0
 /**
  * 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');
 }