def get(cls):
        uid = get_jwt_identity()
        orders = []

        if StoreModel.find_by_id(uid):
            for order in OrderModel.find_all():
                if order.status == 'pending':
                    items = fetching_order(order.order_items)
                    message = order.message
                    orders.append({
                        'id': order.id,
                        'status': order.status,
                        'items': items,
                        'message': message
                    })
            return orders, 200
        elif CustomerModel.find_by_id(uid):
            for order in OrderModel.find_customer_completed_orders(
                    customer_id=uid):
                items = fetching_order(order.order_items)
                message = order.message
                orders.append({
                    'id': order.id,
                    'items': items,
                    'message': message
                })
            return orders, 200
        return 401
Beispiel #2
0
    def put(self, _id):
        data = self.parser.parse_args()
        user = CustomerModel.find_by_id(_id)

        if user:
            user.first_name = data["first_name"]  # if data["first_name"] else user.first_name
            user.middle_name = data["middle_name"]
            user.last_name = data["last_name"]
            user.date_of_birth = date(*data["date_of_birth"])
            user.gender = data["gender"]
            user.email = data["email"]
            user.phone_No_1 = data["phone_No_1"]
            user.phone_No_2 = data["phone_No_2"]
            user.phone_No_3 = data["phone_No_3"]
            user.address = data["address"]
            user.social_status = data["social_status"]
            user.national_id = data["national_id"]
            user.has_smartphone = data["has_smartphone"]
            user.has_computer = data["has_computer"]
            user.has_internet = data["has_internet"]
        else:
            valid_data = {key: val for key, val in data.items() if val is not None}
            user = CustomerModel(**valid_data)

        try:
            user.save_to_db()
        except:
            return {"message": "An error occurred while saving to the database."}, 500

        return user.json()
Beispiel #3
0
    def post(self):
        data = request.form
        customer = CustomerModel.find_by_id(data['customer_id'])
        if customer and safe_str_cmp(customer.password, md5(data['password'].encode("utf-8")).hexdigest()):
            return redirect("/information/" + data['customer_id'])

        return {"message": "Invalid Credentials!"}, 401
Beispiel #4
0
 def get(cls):
     uid = get_jwt_identity()
     if CustomerModel.find_by_id(uid):
         return {"type": "customer"}, 200
     store = StoreModel.find_by_id(uid)
     if store:
         return {"type": "messenger"}, 200
     return {"message": CUSTOMER_NOT_FOUND}, 404
Beispiel #5
0
    def get(cls, confirmation_id: str):
        confirmation = ConfirmationModel.find_by_id(confirmation_id)
        user = CustomerModel.find_by_id(confirmation.customer_id)
        if not user:
            return {"message": CUSTOMER_NOT_FOUND}, 404

        confirmation.confirmed = True
        confirmation.save_to_db()
        headers = {"Context-Type": "text/html"}
        return make_response(render_template("confirmed.html"), 200, headers)
Beispiel #6
0
    def delete(cls, _id: str):
        customer = CustomerModel.find_by_id(_id)
        if not customer:
            return {"message": CUSTOMER_NOT_FOUND}, 404
        try:
            customer.delete_from_db()
        except:
            return {"message": ERROR_DELETING_CUSTOMER}, 500

        return {"message": SUCCESS_DELETING_CUSTOMER}, 200
Beispiel #7
0
    def delete(self, _id):
        user = CustomerModel.find_by_id(_id)
        if user:
            try:
                user.delete_from_db()
            except:
                return {"message": "An error occurred while deleting this user."}, 500

            return {'message': 'User deleted.'}

        return {'message': "The user you're trying to delete doesn't exist."}, 400
Beispiel #8
0
def identity(payload):
    _id = payload['identity']
    # print(payload)
    user = UserModel.find_by_id(_id)
    customer = CustomerModel.find_by_id(_id)
    if user:
        print("as user")
        g.user = user
        return user
    else:
        print("as customer")
        g.customer = customer
        return customer
Beispiel #9
0
    def post(cls, _id: str):
        if CustomerModel.find_by_id(_id):
            return {"message": ERROR_ID_EXISTS}, 400

        data = request.get_json()
        if CustomerModel.find_by_email(data["email"]):
            return {"message": ERROR_EMAIL_EXISTS}, 400

        customer = customer_schema.load(data)
        try:
            customer.save_to_db()
        except:
            return {"message": ERROR_SAVING_USER}, 500

        return {"message": SUCCESS_SAVING_USER}, 200
Beispiel #10
0
def authenticate(customer_id, password):
    user = CustomerModel.find_by_id(customer_id)
    if user and safe_str_cmp(customer_id, password):
        return user
Beispiel #11
0
def identity(payload):
    user_id = payload['identity']
    return CustomerModel.find_by_id(user_id)
Beispiel #12
0
 def get(cls, _id: str):
     customer = CustomerModel.find_by_id(_id)
     if customer:
         return customer_schema.dump(customer), 200
     return {"message": CUSTOMER_NOT_FOUND}, 404
 def validate_customer(*args, **kwargs):
     jwt_data = _decode_jwt_from_request(request_type='access')
     uid = jwt_data[0]['identity']
     if CustomerModel.find_by_id(uid):
         return func(*args, **kwargs)
     abort(401)
Beispiel #14
0
    def post(self):
        data = request.form
        print(data)
        if not CustomerModel.find_by_id(data['customer_id']):
            CustomerModel(data['customer_id'], data['password'], data['full_name'], data['address'],
                                     data['gender'], data['marital_status'], data['customer_type']).save_to_db()
            print("customer saved!")
            msg = "Customer enrolled successfully!"
            print(CustomerModel.query.filter(CustomerModel.customer_id == 10000001).first())
        else:
            msg = "Customer ID has exist!"

        def save_home():
            print("start saving home insurance")
            h_start_date = datetime.strptime(data['h_start_date'], "%Y-%m-%d").date()
            h_end_date = datetime.strptime(data['h_end_date'], "%Y-%m-%d").date()
            h_invoice_date = datetime.now()
            h_pay_due_date = datetime.strptime('2020-12-31', "%Y-%m-%d").date()
            purchase_date = datetime.strptime(data['purchase_date'], "%Y-%m-%d").date()
            print("date changed")
            HomeInsuranceModel(data['customer_id'], h_start_date, h_end_date,
                               data['h_premium_amount'], data['h_policy_status']).save_to_db()
            print("h ins saved")
            HomeInvoiceModel(h_invoice_date, h_pay_due_date,
                             data['h_premium_amount'], data['customer_id']).save_to_db()
            print("h inv saved")
            HomeModel(purchase_date, data['purchase_value'], data['area_sqft'], data['home_type'],
                      data['auto_fire_notif'], data['security_sys'], data['swimming_pool'], data['basement'],
                      data['customer_id']).save_to_db()
            print("home saved")

        def save_auto():
            print("start saving home insurance")
            a_start_date = datetime.strptime(data['a_start_date'], "%Y-%m-%d").date()
            a_end_date = datetime.strptime(data['a_end_date'], "%Y-%m-%d").date()
            a_invoice_date = datetime.now()
            a_pay_due_date = datetime.strptime('2020-12-31', "%Y-%m-%d").date()
            driver_birthday = datetime.strptime(data['driver_birthday'], "%Y-%m-%d").date()
            print("date changed")

            AutoInsuranceModel(data['customer_id'], a_start_date, a_end_date,
                               data['a_premium_amount'], data['a_policy_status']).save_to_db()
            print("a ins saved")
            AutoInvoiceModel(a_invoice_date, a_pay_due_date,
                             data['a_premium_amount'], data['customer_id']).save_to_db()
            print("a inv saved")
            VehicleModel(data['vin'], data['model_year'], data['vehicle_status'], data['customer_id']).save_to_db()
            print("vehicle saved")
            new_vehicle = VehicleModel.find_by_vin(data['vin'])
            DriverModel(data['license_num'], data['driver_name'], driver_birthday,
                        new_vehicle.vehicle_id).save_to_db()
            print("driver saved")

        if data['customer_type'] == "A":
            save_auto()
            print("auto insurance saved!")
        elif data['customer_type'] == "H":
            save_home()
            print("home insurance saved!")
        elif data['customer_type'] == "B":
            save_auto()
            save_home()
            print("both insurance saved!")

        return redirect("/information/"+data['customer_id'])
Beispiel #15
0
 def get(self, _id):
     user = CustomerModel.find_by_id(_id)
     if user:
         return user.json()
     return {"message": "No matching account."}, 404