def add_voucher_to_user_wallet(user_selected_voucher, user_id, username): expiry_date = TimeManager.gen_expiry_date() voucher_id_in_db = user_selected_voucher[1] voucher_title = user_selected_voucher[2] exchange_fee = user_selected_voucher[9] voucher_to_be_added_into_db = { 'telegram_id': user_id, 'asset_type': 'coupon', 'asset_id': voucher_id_in_db, 'asset_value': 1, 'expiry_date': expiry_date } result = UserManager.add_user_voucher(voucher_to_be_added_into_db) if result: exchange_voucher_id_to_be_updated = user_selected_voucher[8] VoucherManager.update_voucher_details( exchange_voucher_id_to_be_updated, 'exchangecoupons', 'issueamount') UserManager.update_user_snapcoin_amt(exchange_fee, '-', user_id) UserManager.update_user_voucher_history(user_id, 'exchanged', voucher_id_in_db) return True LoggerManager.exception_logs( f"Error occurred while adding {voucher_title} voucher to user {username} ({user_id})'s wallet!" )
def add_voucher_to_user_wallet(voucher_info, user_id, username): expiry_date = TimeManager.gen_expiry_date() voucher_id_in_db = voucher_info[2] voucher_to_be_added_into_db = { 'telegram_id': user_id, 'asset_type': 'coupon', 'asset_id': voucher_id_in_db, 'asset_value': 1, 'expiry_date': expiry_date } result = UserManager.add_user_voucher(voucher_to_be_added_into_db) logging_message = f"{voucher_info[0]} voucher to user {username} ({user_id})'s wallet!" if result: LoggerManager.general_logs(f"Successfully added {logging_message}") voucher_id_to_be_updated = voucher_info[7] VoucherManager.update_voucher_details(voucher_id_to_be_updated, 'dailycoupons', 'issueamount') return True else: LoggerManager.exception_logs( f"Error occurred while adding {logging_message}")
def exchange_start(update, context): ReusableComponents.log_command_accessed_timing(update) update.message.reply_text(MessageTemplates.loading_message) user_id = update.message.from_user.id username = update.message.from_user.username if is_not_a_registered_member(update): LoggerManager.general_logs( f"User: {username} ({user_id}) is not a member. Redirecting to registration now." ) update.message.reply_text(MessageTemplates.new_user_found_message) return ConversationHandler.END exchangeable_voucher_list = UserManager.get_user_exchangeable_vouchers_and_snapcoin_amt( user_id) if len(exchangeable_voucher_list) <= EMPTY_LIST_LENGTH: LoggerManager.general_logs(f"No vouchers to exchange currently.") update.message.reply_text( MessageTemplates.no_vouchers_claimable_message) ReusableComponents.prompt_user_next_action(update) return ConversationHandler.END send_exchangeable_message_to_user(update, context, exchangeable_voucher_list) return 'ExchangeInput'
def award_referrer_referee(context, referee_id, referee_username, referrer_telegram_id_and_snapcoin_amt): referrer_id = referrer_telegram_id_and_snapcoin_amt[0][0] referrer_snapcoin_amt = referrer_telegram_id_and_snapcoin_amt[0][1] updated_snapcoin_amt = referrer_snapcoin_amt + \ RewardManager.REFERRAL_BONUS_AMT LoggerManager.general_logs( f"Referrer {referrer_id} updated! Previously: {referrer_snapcoin_amt}, updated: {updated_snapcoin_amt}") UserManager.update_referrer_snapcoin_amt(referrer_id, updated_snapcoin_amt) send_message_to_referrer(context, referrer_id, referee_username) send_message_to_referee(context, referee_id, referee_username) UserManager.update_user_referal_history_table(referrer_id, referee_id)
def get_user_referral_code(user_id): referral_code_result = UserManager.get_user_referral_code(user_id) if len(referral_code_result) <= 0: return False return referral_code_result[0][0]
def check_membership_status(update): user_id = update.message.from_user.id LoggerManager.general_logs( f"Checking if user {update.message.from_user.username} ({user_id}) is a member" ) return UserManager.is_a_member(user_id)
def give_new_user_bonus_reward(user_login_details, user_id, username): no_of_days_as_new_user = user_login_details[0][0] reward_of_the_day = RewardManager.get_new_user_reward_of_the_day( no_of_days_as_new_user) response_result = give_reward_base_on_type(reward_of_the_day, user_id, username) updated_no_of_days_as_new_user = no_of_days_as_new_user + 1 date_today = TimeManager.get_current_date() UserManager.update_new_user_tracker(updated_no_of_days_as_new_user, date_today, user_id) if response_result: return reward_of_the_day
def add_vouchers_to_user_wallet(code_entered_by_user, vouchers_in_promo_code, update): user_id = update.message.from_user.id username = update.message.from_user.username LoggerManager.general_logs( f"Inserting promo vouchers into user {username} ({user_id})'s wallet in db" ) for voucher in vouchers_in_promo_code: voucher_id_in_db = voucher[1] voucher_title = voucher[2] expiry_date = voucher[8] voucher_to_be_added_into_db = { 'telegram_id': user_id, 'asset_type': 'coupon', 'asset_id': voucher_id_in_db, 'asset_value': 1, 'expiry_date': expiry_date } result = UserManager.add_user_voucher(voucher_to_be_added_into_db) if not result: LoggerManager.exception_logs( f"Error occurred while adding {voucher_title} into the db for user {username} ({user_id})" ) return promo_voucher_id = vouchers_in_promo_code[0][0] VoucherManager.update_voucher_details(promo_voucher_id, 'promocode', 'claimed_amount') UserManager.update_user_voucher_history(user_id, code_entered_by_user, None) return True
def handle_new_user_reward(update, user_id, username): result = UserManager.get_new_user_login_details(user_id) if user_does_not_exists(result, update): return False if not claimed_new_user_bonus_before(result, user_id, username): reward_of_the_day = give_new_user_bonus_reward(result, user_id, username) inform_new_user_reward_received(update, result, reward_of_the_day) return True
def get_all_vouchers_owned_by_user(user_id, username): list_of_vouchers_owned_by_user = UserManager.get_all_vouchers_owned_by_user( user_id) if len(list_of_vouchers_owned_by_user) == EMPTY_LIST_LENGTH: LoggerManager.general_logs( f"User {username} ({user_id}) does not have any vouchers in their wallet.") return filtered_list_of_vouchers = ExpiryManager.filter_expired_and_non_expired_vouchers( list_of_vouchers_owned_by_user, user_id) return filtered_list_of_vouchers
def register_new_user(update, context): update.message.reply_text(MessageTemplates.new_user_create_message) user_id = str(update.message.from_user.id) username = update.message.from_user.username referral_code = UserManager.gen_referral_code() new_user_entry = get_new_user_entry(context, referral_code, user_id, username) LoggerManager.general_logs( f"Registering user {username} ({user_id}) into the database now.") registration_result = UserManager.register_new_user(new_user_entry) send_registration_result(update, context, registration_result) ReusableComponents.prompt_user_next_action(update) # Empty user_data dictionary after registration context.user_data[user_id].clear()
def claimed_promo_code_before(code_entered_by_user, user_id, username): if UserManager.user_claimed_promo_code_before(code_entered_by_user, user_id): LoggerManager.general_logs( f"User {username} ({user_id}) has claimed the promo code {code_entered_by_user} before!" ) return True LoggerManager.general_logs( f"User {username} ({user_id}) has not claimed the promo code {code_entered_by_user} before! " + "Validating code now") return False
def add_coin_to_user_wallet(snapcoin_amount, arithmetic_operator, user_id, username): result = UserManager.update_user_snapcoin_amt(snapcoin_amount, arithmetic_operator, user_id) logging_message = f"{snapcoin_amount} to user {username} ({user_id})'s wallet!" if result: LoggerManager.general_logs(f"Successfully added {logging_message}") return True else: LoggerManager.exception_logs( f"Error occurred while adding {logging_message}")
def delete_voucher_from_user_wallet(context, username, user_id): LoggerManager.general_logs( f"Removing voucher from user {username} ({user_id})'s wallet now") user_selected_voucher_id_in_wallet = context.user_data[user_id]['user_selected_voucher'][0] response_result = UserManager.delete_voucher_from_user_wallet( user_selected_voucher_id_in_wallet) if not response_result: return voucher_id_in_db = context.user_data[user_id]['user_selected_voucher'][1] response_result = UserManager.update_user_voucher_history(user_id, 'claimed', voucher_id_in_db) if not response_result: return LoggerManager.general_logs( f"Voucher removed from user {username} ({user_id})'s wallet!") return True
def validate_referral_code(message_from_user): LoggerManager.general_logs(f"Verifying referral code {message_from_user}") referrer_telegram_id_and_snapcoin_amt = UserManager.is_valid_referral_code( message_from_user) if not referrer_telegram_id_and_snapcoin_amt: LoggerManager.exception_logs( f"Referral code: {message_from_user} is invalid! Prompting user to re-enter again.") return referrer_telegram_id = referrer_telegram_id_and_snapcoin_amt[0][0] referrer_snapcoin_amt = referrer_telegram_id_and_snapcoin_amt[0][1] LoggerManager.general_logs( f"Referral code found! Referrer's telegram id: {referrer_telegram_id} ({referrer_snapcoin_amt}) snapcoins.") return referrer_telegram_id_and_snapcoin_amt
def wallet_profile_start(update, context): ReusableComponents.log_command_accessed_timing(update) update.message.reply_text(MessageTemplates.loading_message) user_id = str(update.message.from_user.id) username = update.message.from_user.username dict_of_vouchers_owned_by_user = get_all_vouchers_owned_by_user(user_id, username) user_snapcoin_amount = UserManager.get_user_snap_coin_amount( user_id) if len(user_snapcoin_amount) == EMPTY_LIST_LENGTH: LoggerManager.general_logs( f"User: {username} ({user_id}) is not an existing member! Redirecting to sign up now!") update.message.reply_text(MessageTemplates.new_user_found_message) return ConversationHandler.END send_user_profile_message(update, dict_of_vouchers_owned_by_user, user_snapcoin_amount) if (not dict_of_vouchers_owned_by_user or len(dict_of_vouchers_owned_by_user['non_expired_vouchers']) == EMPTY_LIST_LENGTH): ReusableComponents.prompt_user_next_action(update) return ConversationHandler.END context.user_data.update({ f'{user_id}': {'claimable_vouchers': dict_of_vouchers_owned_by_user['non_expired_vouchers']} }) return 'ViewVoucher'
def remove_expired_voucher_from_db(user_id, user_asset_id, voucher_id): UserManager.update_user_voucher_history(user_id, 'expired', voucher_id) UserManager.delete_voucher_from_user_wallet(user_asset_id)