Skip to content

Commit

Permalink
feat(configs): add sub must be renewed notification
Browse files Browse the repository at this point in the history
  • Loading branch information
exmanka committed Jan 23, 2024
1 parent 68e1c0a commit d0b07e9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
22 changes: 19 additions & 3 deletions src/database/postgres_dbms.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def is_user_registered(telegram_id: int) -> bool | None:


async def is_subscription_active(telegram_id: int) -> bool | None:
"""Check subcription's expiration date of client with specified telegram_id. Return TRUE if acive, FALSE if inactive."""
"""Check subcription's expiration date of client with specified telegram_id. Return TRUE if acive, None if inactive."""
return await conn.fetchval(
'''
SELECT TRUE
Expand All @@ -54,7 +54,7 @@ async def is_subscription_active(telegram_id: int) -> bool | None:
async def is_subscription_not_started(telegram_id: int) -> bool | None:
"""Check admin hasn't send first client's configuration to activate subscription.
Actually check subsciption's expiration date before 1980 year (peculiarity of implementation of database architecture).
Actually check subscription's expiration date before 1980 year (peculiarity of implementation of database architecture).
"""
return await conn.fetchval(
'''
Expand All @@ -68,6 +68,22 @@ async def is_subscription_not_started(telegram_id: int) -> bool | None:
telegram_id)


async def is_subscription_blank(telegram_id: int) -> bool | None:
"""Check subscription was never paid or renewed.
Actually check subscription's expiration date is 'EPOCH' = 1970-01-01 00:00 (peculiarity of implementation of database architecture)."""
return await conn.fetchval(
'''
SELECT TRUE
FROM clients_subscriptions AS cs
JOIN clients AS c
ON cs.client_id = c.id
WHERE c.telegram_id = $1
AND cs.expiration_date = TIMESTAMP 'EPOCH';
''',
telegram_id)


async def is_subscription_free(telegram_id: int) -> bool | None:
"""Check client has free subscription."""
return await conn.fetchval(
Expand Down Expand Up @@ -961,7 +977,7 @@ async def update_notifications_7d(client_id: int) -> bool | None:
client_id)


async def add_subscription_time(client_id: int, days: int) -> None:
async def add_subscription_period(client_id: int, days: int) -> None:
"""Add interval for subscription expiration date."""
await conn.execute(
'''
Expand Down
3 changes: 3 additions & 0 deletions src/handlers/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

async def command_help(message: Message):
"""Send message with information about provided help."""
# if client needs to renew subscription before receiving his first configuration
await internal_functions.notify_client_if_subscription_must_be_renewed_to_receive_configuration(message.from_user.id)

await message.answer(markdown.hide_link('https://t.me/exmanka') + loc.other.msgs['help'], parse_mode='HTML')


Expand Down
7 changes: 7 additions & 0 deletions src/handlers/user_authorized.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ async def subscription_status(message: Message):
"""Send message with subscription status."""
# if admin hasn't still sent client's first configuration
if await postgres_dbms.is_subscription_not_started(message.from_user.id):

# if client needs to renew subscription before receiving his first configuration
await internal_functions.notify_client_if_subscription_must_be_renewed_to_receive_configuration(message.from_user.id)

await message.answer(loc.auth.msgs['sub_isnt_active'])
return

Expand Down Expand Up @@ -191,6 +195,9 @@ async def account_subscription_info(message: Message):
@user_mw.authorized_only()
async def account_configurations_fsm_start(message: Message, state: FSMContext):
"""Start FSM for account configurations menu and show account configurations menu keyboard."""
# if client needs to renew subscription before receiving his first configuration
await internal_functions.notify_client_if_subscription_must_be_renewed_to_receive_configuration(message.from_user.id)

await state.set_state(user_authorized_fsm.AccountMenu.configs)
await message.answer(loc.auth.msgs['go_config_menu'], parse_mode='HTML', reply_markup=user_authorized_kb.config)

Expand Down
2 changes: 1 addition & 1 deletion src/localizations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"ref_promo_accepted": "Wow! A promo code has been accepted from <b>client {0}</b>, giving <b>{1} days</b> of free subscription!",
"sub_info": "🌟 Connected <b>{0}</b> with <b>cost {1:g}</b>₽ per month.",
"invalid_promo": "There is no such promo code! Try to enter it again",
"need_renew_sub": "❗️ <b>To get the configuration, you need to renew your subscription!</b>"
"need_renew_sub": "❗️ <b>To get the first configuration, you need to renew your subscription</b>"
},

"keyboard_buttons": {
Expand Down
2 changes: 1 addition & 1 deletion src/localizations/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"ref_promo_accepted": "✨ Ух ты! Принят промокод от <b>пользователя {0}</b>, дающий <b>{1} дней</b> бесплатной подписки!",
"sub_info": "🌟 Подключена <b>{0}</b> со <b>стоимостью {1:g}</b>₽ в месяц.",
"invalid_promo": "Такого промокода нет! Попробуйте ввести его еще раз",
"need_renew_sub": "❗️ <b>Чтобы получить конфигурацию, Вам необходимо продлить подписку!</b>"
"need_renew_sub": "❗️ <b>Для получения первой конфигурации Вам необходимо продлить подписку</b>"
},

"keyboard_buttons": {
Expand Down
9 changes: 8 additions & 1 deletion src/services/internal_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,13 @@ async def notify_client_new_referal(client_creator_id: int, referral_client_name
await bot.send_message(client_creator_telegram_id,
loc.internal.msgs['ref_promo_was_entered'].format(referral_client_name, referral_client_username_str, bonus_time_parsed),
parse_mode='HTML')


async def notify_client_if_subscription_must_be_renewed_to_receive_configuration(telegram_id: int):
"""Send message by specified telegram_id IF client needs to renew subscription for receiving his first configuration."""

if await postgres_dbms.is_subscription_blank(telegram_id):
await bot.send_message(telegram_id, loc.unauth.msgs['need_renew_sub'], parse_mode='HTML')


async def create_configuration_description(configuration_date_of_receipt: str,
Expand Down Expand Up @@ -462,7 +469,7 @@ async def check_referral_reward(ref_client_id: int):

# add subscription bonus time (30 days) for old client
_, client_creator_id, *_ = await postgres_dbms.get_refferal_promo_info_by_promoID(used_ref_promo_id)
await postgres_dbms.add_subscription_time(client_creator_id, days=30)
await postgres_dbms.add_subscription_period(client_creator_id, days=30)

# notify old client about new bonus
# if client's username exists (add whitespace for good string formatting)
Expand Down

0 comments on commit d0b07e9

Please sign in to comment.