async def get_password_reset_link(background: BackgroundTasks, user: GetPasswordResetLink): current_user = User.find_user_with_email(email=user.email) if current_user: background.add_task( Fortauto.mailUsr, user_email=user.email, email_message= f" Good day {current_user.first_name} {current_user.last_name}, You requested for password reset, " f"please click\n\n " f"on to link below to continue \n\n {user.website_url}/{current_user.id}/password/reset", email_title="Password reset link") return Fortauto.response( { "message": "password reset link has been sent your email address, click it to continue." }, status_code=status.HTTP_200_OK) return Fortauto.response({"message": "Account does not exist"}, status_code=status.HTTP_400_BAD_REQUEST)
async def delete_service(serviceId: str, user: dict = Depends(UserMixin.authenticate_user)): if user["super_admin"]: try: deleted_to_service = Service.get_single_serviceById(serviceId) if deleted_to_service: deleted_to_service.delete() return Fortauto.response( {"message": "service was deleted successfully"}, status_code=status.HTTP_200_OK) return Fortauto.response({"message": "service not found"}, status_code=status.HTTP_400_BAD_REQUEST) except errors.ValidationError: return Fortauto.response({"message": "service does not exist"}, status_code=status.HTTP_400_BAD_REQUEST) except Exception.__base__: return Fortauto.response( {"message": "Error deleting service"}, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) return Fortauto.response({"message": "Error validating admin"}, status_code=status.HTTP_401_UNAUTHORIZED)
async def update_payment( payment_details:UpdatePayment, user: dict = Depends(UserMixin.authenticate_user)): if user["admin"] or user["super_admin"]: try: payment = Payment.get_single_payment(payment_RefNo=payment_details.payment_RefNo) if payment: if payment_details.total_amount: payment.update(payment_status=payment_details.payment_status, total_amount=payment_details.total_amount) payment.update(payment_status=payment_details.payment_status) payment.save(clean=True) payment = Payment.get_single_payment(payment_RefNo=payment_details.payment_RefNo) return Fortauto.response({"payments": payment.to_json(), "message":"payment updated successfully"}, status_code=status.HTTP_200_OK) return Fortauto.response({"message": "payment not found"}, status_code=status.HTTP_400_BAD_REQUEST) except errors.DoesNotExist: return Fortauto.response({"message": "payment does not exist"}, status_code=status.HTTP_400_BAD_REQUEST) except Exception.__base__: return Fortauto.response({"message": "error updating payment"}, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) return Fortauto.response({"message": "error validating admin"}, status_code=status.HTTP_401_UNAUTHORIZED)
async def create__deposit(deposit_details:UserDeposit, user:dict = Depends(UserMixin.authenticate_user)): try: ## MAKE A REQUEST TO VERIFY PAYMENT with payment_ref user_account = Deposit.get_user_account(userId=user["id"]) if user_account: current_balance = (user_account.total_amount + deposit_details.total_amount) user_account.update(total_amount=current_balance) user_account.save(clean=True) return Fortauto.response({"message": f"You account credited with ₦{deposit_details.total_amount}"}, status_code=status.HTTP_201_CREATED) deposit = Deposit(total_amount=deposit_details.total_amount, userId=user["id"]).save(clean=True) if deposit: return Fortauto.response({"message":f"You account credited with ₦{deposit_details.total_amount}"}, status_code=status.HTTP_201_CREATED) return Fortauto.response({"message":"error making deposit"}, status_code=status.HTTP_400_BAD_REQUEST) except errors.ValidationError: return Fortauto.response({"message": "error validating payment details"}, status_code=status.HTTP_400_BAD_REQUEST) except Exception.__base__: return Fortauto.response({"message": "error making depositing"}, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
def loginUserAccount(userIn: UserLoginInput, response: Response) -> Response: user = User.find_user_with_email(email=userIn.email) if user: if user.active: if UserMixin.check_password(userIn.password, user.password): encode_jwt_access, encode_jwt_refresh = UserMixin.JwtEncoder( user=user.to_json()) if encode_jwt_access and encode_jwt_refresh: response.set_cookie( key="refresh_token", value=encode_jwt_refresh, httponly=True, max_age=172800, expires=172800, ) SuccessResponseData = { "user": user.to_json(indent=4), "message": "logged in successfully", "access_token": encode_jwt_access, "access_token_type": "Bearer", "expires": "2 days", } return Fortauto.response(SuccessResponseData, status_code=status.HTTP_200_OK) ErrorResponseData = {"message": "Password does not match"} return Fortauto.response(ErrorResponseData, status_code=status.HTTP_401_UNAUTHORIZED) ErrorResponseData = { "message": "Email was sent to you, please verify your email" } return Fortauto.response(ErrorResponseData, status_code=status.HTTP_401_UNAUTHORIZED) ErrorResponseData = {"message": "Account does not exist"} return Fortauto.response(ErrorResponseData, status_code=status.HTTP_401_UNAUTHORIZED)
async def create_service(newItems: ServiceListInput, user: dict = Depends(UserMixin.authenticate_user)): if user["admin"]: addToList = ServiceList(**newItems.dict()) try: if addToList: addToList.save(clean=True) return Fortauto.response({"service": addToList.to_json()}, status_code=status.HTTP_201_CREATED) except errors.ValidationError: return Fortauto.response( {"message": "error validating new category type"}, status_code=status.HTTP_400_BAD_REQUEST) except errors.NotUniqueError: return Fortauto.response({"message": "Service already exists"}, status_code=status.HTTP_400_BAD_REQUEST) except Exception.__base__: return Fortauto.response( {"message": "error adding new category, please try again"}, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) return Fortauto.response({"message": "Error validating admin credentials"}, status_code=status.HTTP_401_UNAUTHORIZED)
async def create_service(newItems: ServiceInput, user: dict = Depends(UserMixin.authenticate_user)): try: new_service = Service(userId=user["id"], car_type=newItems.car_type, service_type=newItems.service_type, additional_notes=newItems.additional_notes, quantity=newItems.quantity, priority=newItems.priority, date=newItems.date) if new_service: new_service.save(clean=True) return Fortauto.response({"service": new_service.to_json()}, status_code=status.HTTP_201_CREATED) except errors.ValidationError: return Fortauto.response({"message": "error validating new service"}, status_code=status.HTTP_400_BAD_REQUEST) except errors.NotUniqueError: return Fortauto.response({"message": "Service already exists"}, status_code=status.HTTP_400_BAD_REQUEST) except Exception.__base__: return Fortauto.response( {"message": "error adding new service, please try again"}, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
async def update_user_deposit_account(deposit_details:UpdateDeposit, user:dict = Depends(UserMixin.authenticate_user)): if user["super_admin"]: try: user_account = Deposit.get_user_account(userId=deposit_details.userId) if user_account: if deposit_details.method.lower() == "credit" and deposit_details.total_amount > 0 : current_balance = (user_account.total_amount + deposit_details.total_amount) user_account.update(total_amount=current_balance) user_account.save(clean=True) return Fortauto.response({"message": f"Your account was credited with ₦{deposit_details.total_amount}"}, status_code=status.HTTP_200_OK) elif deposit_details.method.lower() == "debit" and deposit_details.total_amount > 0: if user_account.total_amount < deposit_details.total_amount: balance = (deposit_details.total_amount - user_account.total_amount) return Fortauto.response({"message":f"user does not have sufficient amount, required ₦{balance} to complete transaction"}, status_code=status.HTTP_400_BAD_REQUEST) current_balance = (user_account.total_amount - deposit_details.total_amount) user_account.update(total_amount=current_balance) user_account.save(clean=True) return Fortauto.response( {"message": f"Your account was debited with ₦{deposit_details.total_amount}"}, status_code=status.HTTP_200_OK) if deposit_details.method.lower() != "debit" and deposit_details.method != "credit": return Fortauto.response( {"message": "deposit method not recognized"}, status_code=status.HTTP_400_BAD_REQUEST) elif deposit_details.total_amount <= 0: return Fortauto.response({"message": "you can only deposit amount greater than ₦100 "}, status_code=status.HTTP_400_BAD_REQUEST) except errors.ValidationError: return Fortauto.response({"message": "error validating payment details"}, status_code=status.HTTP_400_BAD_REQUEST) except Exception.__base__: return Fortauto.response({"message": "error updating user credit account"}, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) return Fortauto.response( {"message": f"Error validating admin"}, status_code=status.HTTP_401_UNAUTHORIZED)
async def add_user_car_details(car: CarDetail, current_user: dict = Depends( UserMixin.authenticate_user)): try: user = User.find_user_with_Id(current_user["id"]) if user: if not user.car_details: user.car_details.create(**car.dict()) user.save(clean=True) return Fortauto.response( {"message": "car details added successfully"}, status_code=status.HTTP_201_CREATED) user_car = user.car_details.filter(vin_number=car.vin_number) if not user_car: user.car_details.create(**car.dict()) user.save(clean=True) return Fortauto.response( {"message": "car details added successfully"}, status_code=status.HTTP_201_CREATED) return Fortauto.response({"message": "car already exists"}, status_code=status.HTTP_400_BAD_REQUEST) return Fortauto.response({"message": "user does not exist"}, status_code=status.HTTP_401_UNAUTHORIZED) except errors.NotUniqueError: return Fortauto.response( {"message": "car with this vin number already exists"}, status_code=status.HTTP_400_BAD_REQUEST) except errors.DoesNotExist: return Fortauto.response({"message": "car does not exist"}, status_code=status.HTTP_400_BAD_REQUEST) except errors.ValidationError: return Fortauto.response({"message": "Error validating user"}, status_code=status.HTTP_400_BAD_REQUEST) except Exception.__base__: return Fortauto.response( {"message": "Error adding car, try again"}, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)