diff --git a/src/database/postgres_dbms.py b/src/database/postgres_dbms.py index ed640a7..d094924 100644 --- a/src/database/postgres_dbms.py +++ b/src/database/postgres_dbms.py @@ -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 @@ -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( ''' @@ -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( @@ -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( ''' diff --git a/src/handlers/other.py b/src/handlers/other.py index b01ad1d..b3512e5 100644 --- a/src/handlers/other.py +++ b/src/handlers/other.py @@ -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') diff --git a/src/handlers/user_authorized.py b/src/handlers/user_authorized.py index 5a54615..deca0a0 100644 --- a/src/handlers/user_authorized.py +++ b/src/handlers/user_authorized.py @@ -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 @@ -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) diff --git a/src/localizations/en.json b/src/localizations/en.json index ff6ddbb..e9be352 100644 --- a/src/localizations/en.json +++ b/src/localizations/en.json @@ -13,7 +13,7 @@ "ref_promo_accepted": "Wow! A promo code has been accepted from client {0}, giving {1} days of free subscription!", "sub_info": "🌟 Connected {0} with cost {1:g}₽ per month.", "invalid_promo": "There is no such promo code! Try to enter it again", - "need_renew_sub": "❗️ To get the configuration, you need to renew your subscription!" + "need_renew_sub": "❗️ To get the first configuration, you need to renew your subscription" }, "keyboard_buttons": { diff --git a/src/localizations/ru.json b/src/localizations/ru.json index dc40301..72141e5 100644 --- a/src/localizations/ru.json +++ b/src/localizations/ru.json @@ -13,7 +13,7 @@ "ref_promo_accepted": "✨ Ух ты! Принят промокод от пользователя {0}, дающий {1} дней бесплатной подписки!", "sub_info": "🌟 Подключена {0} со стоимостью {1:g}₽ в месяц.", "invalid_promo": "Такого промокода нет! Попробуйте ввести его еще раз", - "need_renew_sub": "❗️ Чтобы получить конфигурацию, Вам необходимо продлить подписку!" + "need_renew_sub": "❗️ Для получения первой конфигурации Вам необходимо продлить подписку" }, "keyboard_buttons": { diff --git a/src/services/internal_functions.py b/src/services/internal_functions.py index ad4d0e5..411d5d5 100644 --- a/src/services/internal_functions.py +++ b/src/services/internal_functions.py @@ -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, @@ -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)