def getProducts(message: Message):
    user = db.getUser(message.chat.id)

    if not user:
        db.createUser(message.chat.id, states.SHOW_PRODUCTS)
        user = db.getUser(message.chat.id)

    page = user[3]

    print(page)

    if page <= 0:
        db.changeUserState(message.chat.id, page=1)

        bot.send_message(message.chat.id,
                         answers.outOfRange,
                         reply_markup=keyboards.removeKeyBoard())

        return

    httpProduct = http.getProducts(user[2], page)

    if not len(httpProduct['data']):
        bot.send_message(message.chat.id, answers.noData)
        return

    for product in httpProduct['data']:
        helpers.sendProduct(message.chat.id, product)
def filterSize(message: Message):
    userFilters: dict = db.getUser(message.chat.id)[2]

    if message.text != '.':
        if message.text in getSizes():
            # change db data
            userFilters['size'] = message.text

        else:
            bot.send_message(message.chat.id, answers.filterSizeInvalid)

            return

    # change db data
    db.changeUserState(message.chat.id,
                       state=states.FILTER_COLOR,
                       filters=userFilters)

    # get next step data
    filters = http.getFilters()
    colors = filters['colors']

    # send next step
    bot.send_message(message.chat.id,
                     answers.filterColor,
                     reply_markup=keyboards.listKeyboard(colors))
def filterCategory(message: Message):
    userFilters: dict = db.getUser(message.chat.id)[2]
    userFilters['categories'] = []

    if message.text != '.':
        # get filters
        filters = http.getFilters()
        categories: list = list(
            map(lambda item: item['name'], filters['categories']))

        # find category
        index = categories.index(message.text)

        if index != -1:
            # add filter to db
            catID = filters['categories'][index]['id']
            userFilters['categories'] = {catID: True}

        else:
            bot.send_message(message.chat.id, answers.filterCategoryInvalid)

            return

    # set new data to db
    db.changeUserState(message.chat.id,
                       state=states.FILTER_SIZE,
                       filters=userFilters)

    # send next step
    bot.send_message(message.chat.id,
                     answers.filterSize,
                     reply_markup=keyboards.listKeyboard(getSizes()))
def filterColor(message: Message):
    userFilters: dict = db.getUser(message.chat.id)[2]

    if message.text != '.':
        filters = http.getFilters()
        colors = filters['colors']

        if message.text in colors:
            # change db data
            userFilters['color'] = message.text

        else:
            bot.send_message(message.chat.id, answers.filterColorInvalid)

            return

    # set data to db
    db.changeUserState(message.chat.id,
                       state=states.FILTER_PRICE,
                       filters=userFilters)

    # send next step
    bot.send_message(message.chat.id,
                     answers.filterPrice,
                     reply_markup=keyboards.removeKeyBoard())
def setFiltersStart(message: Message):
    # start asking of filters
    db.changeUserState(message.chat.id,
                       state=states.FILTER_CATEGORY,
                       filters=dict(),
                       page=1)

    # make keyboard of categories
    filters = http.getFilters()
    reply = keyboards.listKeyboard(
        map(lambda item: item['name'], filters['categories']))

    bot.send_message(message.chat.id,
                     answers.filterCategory,
                     reply_markup=reply)
def willFilter(message: Message):
    if message.text == 'Yes':
        setFiltersStart(message)

    elif message.text == 'No':
        # show all products
        db.changeUserState(message.chat.id,
                           state=states.SHOW_PRODUCTS,
                           filters=dict(),
                           page=1)

        # send products
        getProducts(message)

    else:
        bot.send_message(message.chat.id, answers.invalidValue)
def enterFind(message: Message):
    if not message.text:
        # send error message
        bot.send_message(message.chat.id, answers.findInvalid)
        return

    # change state to show
    db.changeUserState(message.chat.id, state=states.SHOW_PRODUCTS)

    # find products
    products: dict = http.findProducts(message.text)

    if not len(products.get('data')):
        bot.send_message(message.chat.id,
                         answers.notFound,
                         reply_markup=keyboards.removeKeyBoard())

    # show products
    for product in products.get('data'):
        helpers.sendProduct(message.chat.id, product)
def filterPrice(message: Message):
    userFilters: dict = db.getUser(message.chat.id)[2]

    if message.text != '.':
        try:
            # parse range
            priceRange = parseRange(message.text)

            # set data to db
            userFilters['priceRange'] = {
                'from': min(priceRange),
                'to': max(priceRange)
            }

        except Exception:
            bot.send_message(message.chat.id, answers.filterPriceInvalid)
            return

    # get products
    db.changeUserState(message.chat.id,
                       state=states.SHOW_PRODUCTS,
                       filters=userFilters)
    getProducts(message)
def getPrevProducts(message: Message):
    user = db.getUser(message.chat.id)
    page = user[3] - 1

    db.changeUserState(message.chat.id, page=page)
    getProducts(message)
def find(message: Message):
    db.changeUserState(message.chat.id, state=states.FIND)
    bot.send_message(message.chat.id,
                     answers.findMessage,
                     reply_markup=keyboards.removeKeyBoard())