Switch onboarding city selection to manual text input
All checks were successful
CI/CD / lint (push) Successful in 7s
CI/CD / build (push) Successful in 18s
CI/CD / deploy (push) Successful in 40s

This commit is contained in:
Jaymiesh 2026-02-11 16:35:20 +07:00
parent a8cab41a00
commit 8033377cdf
4 changed files with 38 additions and 16 deletions

View File

@ -57,6 +57,24 @@ export async function getCityById(id: string) {
return rows[0] || null
}
export async function getOrCreateCityByName(name: string) {
const normalizedName = name.trim().replace(/\s+/g, ' ')
const { rows: existing } = await pool.query(
'SELECT * FROM cities WHERE lower(name) = lower($1) LIMIT 1',
[normalizedName]
)
if (existing[0]) return existing[0]
const { rows } = await pool.query(
`INSERT INTO cities (name, country, description, status)
VALUES ($1, $2, $3, 'active')
RETURNING *`,
[normalizedName, 'Unknown', 'User-added city']
)
return rows[0]
}
// ============================================
// Quests
// ============================================

View File

@ -2,7 +2,7 @@ import type { Conversation } from '@grammyjs/conversations'
import type { BotContext } from '../types.js'
type BotConversation = Conversation<BotContext, BotContext>
import { getActiveCities, findUserByTelegramId } from '../../api/_lib/db.js'
import { getOrCreateCityByName, findUserByTelegramId } from '../../api/_lib/db.js'
import { t, getLang } from '../i18n/index.js'
import { createQuestFromOnboarding } from '../services/quest.service.js'
@ -22,21 +22,23 @@ const PACE_MAP: Record<string, string> = {
export async function onboardingConversation(conversation: BotConversation, ctx: BotContext) {
const lang = getLang(ctx.from?.language_code)
// Step 1: City selection
const cities = await conversation.external(() => getActiveCities())
// Step 1: City input
await ctx.reply(t(lang, 'choose_city'))
const cityButtons = cities.map((city: { id: string; name: string }) => ([
{ text: city.name, callback_data: `city_${city.id}` }
]))
let cityName = ''
while (true) {
const cityResponse = await conversation.waitFor('message:text')
const input = cityResponse.message.text.trim().replace(/\s+/g, ' ')
if (input.length >= 2 && !input.startsWith('/')) {
cityName = input
break
}
await ctx.reply(t(lang, 'invalid_city'))
}
await ctx.reply(t(lang, 'choose_city'), {
reply_markup: { inline_keyboard: cityButtons },
})
const cityResponse = await conversation.waitForCallbackQuery(/^city_/)
const cityId = cityResponse.callbackQuery!.data!.replace('city_', '')
const cityName = cities.find((c: { id: string }) => c.id === cityId)?.name || 'Unknown'
await cityResponse.answerCallbackQuery()
const city = await conversation.external(() => getOrCreateCityByName(cityName))
const cityId = city.id
cityName = city.name
// Step 2: Number of days
await ctx.reply(t(lang, 'choose_days'))

View File

@ -9,7 +9,8 @@ Let's begin! Where are you headed?`,
welcome_back: `Welcome back! You have an active quest in {city}. Open the app to continue.`,
// Onboarding
choose_city: 'Choose a city for your adventure:',
choose_city: 'Which city are you planning your quest in? Type the city name.',
invalid_city: 'Please enter a valid city name (at least 2 characters).',
choose_days: 'How many days are you planning? (1-30)',
invalid_days: 'Please enter a number from 1 to 30.',
choose_companions: 'Who are you traveling with?',

View File

@ -9,7 +9,8 @@ export const ru: Record<string, string> = {
welcome_back: `С возвращением! У тебя есть активный квест по городу {city}. Открой приложение, чтобы продолжить.`,
// Onboarding
choose_city: 'Выбери город для приключения:',
choose_city: 'В каком городе планируешь квест? Напиши название города.',
invalid_city: 'Введи корректное название города (минимум 2 символа).',
choose_days: 'На сколько дней планируешь поездку? (1-30)',
invalid_days: 'Введи число от 1 до 30.',
choose_companions: 'С кем путешествуешь?',