Skip to content

Commit 2a794ed

Browse files
committed
remove FromStatus
1 parent c682d4f commit 2a794ed

File tree

4 files changed

+40
-46
lines changed

4 files changed

+40
-46
lines changed

workers/src/handlers/commands.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { sendTelegramMessage, sendAdmin } from '../services/telegram'
22
import { addFlightTracking, clearUserTracking, untrackFlight } from '../services/tracking'
3-
import {
4-
getFlightIdByNumberFromStatus,
5-
getNotTrackedFlightsFromStatus,
6-
generateFakeFlights,
7-
storeFlightsInStatus,
8-
} from '../services/flightData'
3+
import { getFlightIdByNumber, getNotTrackedFlights, generateFakeFlights, storeFlights } from '../services/flightData'
94
import { getCurrentIdtTime, formatTimeAgo, formatTimestampForDisplay } from '../utils/dateTime'
105
import { formatTrackingListOptimized, formatFlightSuggestions, escapeMarkdown } from '../utils/formatting'
116
import { isValidFlightCode } from '../utils/validation'
@@ -133,7 +128,7 @@ export const handleCommand = async (request: Request, env: Env, ctx: DurableObje
133128
const results = []
134129
for (const code of flightCodes) {
135130
if (isValidFlightCode(code)) {
136-
const flightId = getFlightIdByNumberFromStatus(code.toUpperCase().replace(' ', ''), ctx)
131+
const flightId = getFlightIdByNumber(code.toUpperCase().replace(' ', ''), ctx)
137132
if (flightId) {
138133
addFlightTracking(chatId, flightId, env, ctx)
139134
results.push(`✓ Now tracking ${code.toUpperCase()}`)
@@ -168,7 +163,7 @@ export const handleCommand = async (request: Request, env: Env, ctx: DurableObje
168163
const currentPage = parseInt(cursorStr) || 0
169164
const nextPage = currentPage + 1
170165

171-
const eligibleFlights = getNotTrackedFlightsFromStatus(chatId, ctx)
166+
const eligibleFlights = getNotTrackedFlights(chatId, ctx)
172167
const startIndex = nextPage * 5
173168
const endIndex = startIndex + 5
174169
const pageFlights = eligibleFlights.slice(startIndex, endIndex)
@@ -329,7 +324,7 @@ export const handleCommand = async (request: Request, env: Env, ctx: DurableObje
329324
} else if (data === 'show_suggestions') {
330325
ctx.storage.kv.put(`pagination_cursor_${chatId}`, '0')
331326

332-
const eligibleFlights = getNotTrackedFlightsFromStatus(chatId, ctx)
327+
const eligibleFlights = getNotTrackedFlights(chatId, ctx)
333328

334329
const { text, replyMarkup: suggestionsMarkup } = formatFlightSuggestions(
335330
eligibleFlights.slice(0, 5),
@@ -366,7 +361,7 @@ export const handleCommand = async (request: Request, env: Env, ctx: DurableObje
366361
} else if (data.startsWith('suggestions_page:')) {
367362
// Handle pagination
368363
const page = parseInt(data.split(':')[1])
369-
const eligibleFlights = getNotTrackedFlightsFromStatus(chatId, ctx)
364+
const eligibleFlights = getNotTrackedFlights(chatId, ctx)
370365
const startIndex = page * 5
371366
const endIndex = startIndex + 5
372367
const pageFlights = eligibleFlights.slice(startIndex, endIndex)
@@ -533,7 +528,7 @@ const handleTrack = async (chatId: number, text: string, env: Env, ctx: DurableO
533528
const results = []
534529
for (const code of flightCodes) {
535530
if (isValidFlightCode(code)) {
536-
const flightId = getFlightIdByNumberFromStatus(code.toUpperCase().replace(' ', ''), ctx)
531+
const flightId = getFlightIdByNumber(code.toUpperCase().replace(' ', ''), ctx)
537532
if (flightId) {
538533
addFlightTracking(chatId, flightId, env, ctx)
539534
results.push(`✓ Now tracking ${code.toUpperCase()}`)
@@ -562,7 +557,7 @@ const handleClearTracked = async (chatId: number, env: Env, ctx: DurableObjectSt
562557
}
563558

564559
const handleTestTracking = async (chatId: number, env: Env, ctx: DurableObjectState<DOProps>) => {
565-
const eligibleFlights = getNotTrackedFlightsFromStatus(chatId, ctx)
560+
const eligibleFlights = getNotTrackedFlights(chatId, ctx)
566561

567562
const { text, replyMarkup } = formatFlightSuggestions(eligibleFlights.slice(0, 5), 0, eligibleFlights.length, ctx)
568563
await sendTelegramMessage(chatId, text, env, false, replyMarkup)
@@ -587,7 +582,7 @@ const handleTestData = async (chatId: number, env: Env, ctx: DurableObjectState<
587582
const fakeFlights = generateFakeFlights(ctx)
588583

589584
// Store fake flights using new JSON approach (replaces SQLite storage)
590-
storeFlightsInStatus(fakeFlights, ctx)
585+
storeFlights(fakeFlights, ctx)
591586

592587
await sendTelegramMessage(
593588
chatId,

workers/src/handlers/cron.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
fetchLatestFlights,
3-
cleanupCompletedFlightsFromStatus,
4-
getCurrentFlightsFromStatus,
5-
storeFlightsInStatus,
3+
cleanupCompletedFlights,
4+
getCurrentFlights,
5+
storeFlights,
66
detectChanges,
77
writeStatusData,
88
writeErrorStatus,
@@ -23,8 +23,8 @@ export const runScheduledJob = async (env: Env, ctx: DurableObjectState<DOProps>
2323
const currentFlights = await fetchLatestFlights(env, ctx)
2424
writeStatusData(ctx, currentFlights.length)
2525

26-
const previousFlights = getCurrentFlightsFromStatus(ctx)
27-
storeFlightsInStatus(currentFlights, ctx)
26+
const previousFlights = getCurrentFlights(ctx)
27+
storeFlights(currentFlights, ctx)
2828

2929
const subscribedResult = ctx.storage.sql.exec('SELECT DISTINCT flight_id FROM subscriptions')
3030
const subscribedFlightIds = new Set(
@@ -70,7 +70,7 @@ export const runScheduledJob = async (env: Env, ctx: DurableObjectState<DOProps>
7070

7171
if (now - lastCleanupTime >= tenMinutes) {
7272
console.log('Running cleanup (10 minute interval)')
73-
cleanupCompletedFlightsFromStatus(env, ctx)
73+
cleanupCompletedFlights(env, ctx)
7474

7575
ctx.storage.sql.exec(
7676
"INSERT INTO status (key, value) VALUES ('last_cleanup_time', ?) ON CONFLICT(key) DO UPDATE SET value = EXCLUDED.value",

workers/src/services/flightData.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ export const generateFakeFlights = (ctx: DurableObjectState<DOProps>): Flight[]
4747
]
4848
}
4949

50-
export const cleanupCompletedFlightsFromStatus = (env: Env, ctx: DurableObjectState<DOProps>): number => {
50+
export const cleanupCompletedFlights = (env: Env, ctx: DurableObjectState<DOProps>): number => {
5151
const nowIdt = getCurrentIdtTime(ctx)
5252
const cutoffTimestamp = nowIdt.getTime() - 1 * 60 * 60 * 1000 // 1 hour ago
5353

5454
console.log(`Cleanup: cutoff=${new Date(cutoffTimestamp).toLocaleString()}`)
5555

5656
try {
57-
const currentFlights = getCurrentFlightsFromStatus(ctx)
57+
const currentFlights = getCurrentFlights(ctx)
5858

5959
const completedFlightIds = currentFlights
6060
.filter(
@@ -205,7 +205,7 @@ export const writeFlightsData = (flights: Flight[], ctx: DurableObjectState<DOPr
205205
}
206206
}
207207

208-
export const getCurrentFlightsFromStatus = (ctx: DurableObjectState<DOProps>): Flight[] => {
208+
export const getCurrentFlights = (ctx: DurableObjectState<DOProps>): Flight[] => {
209209
const cache = ctx.props.cache
210210

211211
if (cache.flights) {
@@ -228,7 +228,7 @@ export const getCurrentFlightsFromStatus = (ctx: DurableObjectState<DOProps>): F
228228
return cache.flights
229229
}
230230

231-
export const storeFlightsInStatus = (flights: Flight[], ctx: DurableObjectState<DOProps>): void => {
231+
export const storeFlights = (flights: Flight[], ctx: DurableObjectState<DOProps>): void => {
232232
ctx.storage.sql.exec(
233233
'INSERT OR REPLACE INTO status (key, value) VALUES (?, ?)',
234234
'flights_data',
@@ -237,30 +237,27 @@ export const storeFlightsInStatus = (flights: Flight[], ctx: DurableObjectState<
237237
console.log(`Stored ${flights.length} flights as JSON in status table`)
238238
}
239239

240-
export const getUserTrackedFlightsFromStatus = (userId: number, ctx: DurableObjectState<DOProps>): Flight[] => {
241-
const allFlights = getCurrentFlightsFromStatus(ctx)
240+
export const getUserTrackedFlights = (userId: number, ctx: DurableObjectState<DOProps>): Flight[] => {
241+
const allFlights = getCurrentFlights(ctx)
242242

243243
const result = ctx.storage.sql.exec('SELECT flight_id FROM subscriptions WHERE telegram_id = ?', userId)
244244
const subscribedIds = result.toArray().map((row) => (row as { flight_id: string }).flight_id)
245245

246246
return allFlights.filter((flight) => subscribedIds.includes(flight.id)).sort((a, b) => a.eta - b.eta)
247247
}
248248

249-
export const getFlightByIdFromStatus = (flightId: string, ctx: DurableObjectState<DOProps>): Flight | undefined => {
250-
const allFlights = getCurrentFlightsFromStatus(ctx)
249+
export const getFlightById = (flightId: string, ctx: DurableObjectState<DOProps>): Flight | undefined => {
250+
const allFlights = getCurrentFlights(ctx)
251251
return allFlights.find((flight) => flight.id === flightId)
252252
}
253253

254-
export const getFlightByNumberFromStatus = (
255-
flightNumber: string,
256-
ctx: DurableObjectState<DOProps>
257-
): Flight | undefined => {
258-
const allFlights = getCurrentFlightsFromStatus(ctx)
254+
export const getFlightByNumber = (flightNumber: string, ctx: DurableObjectState<DOProps>): Flight | undefined => {
255+
const allFlights = getCurrentFlights(ctx)
259256
return allFlights.find((flight) => flight.flight_number === flightNumber)
260257
}
261258

262-
export const getNotTrackedFlightsFromStatus = (chatId: number, ctx: DurableObjectState<DOProps>): Flight[] => {
263-
const allFlights = getCurrentFlightsFromStatus(ctx)
259+
export const getNotTrackedFlights = (chatId: number, ctx: DurableObjectState<DOProps>): Flight[] => {
260+
const allFlights = getCurrentFlights(ctx)
264261

265262
const result = ctx.storage.sql.exec('SELECT flight_id FROM subscriptions WHERE telegram_id = ?', chatId)
266263
const subscribedIds = result.toArray().map((row) => (row as { flight_id: string }).flight_id)
@@ -271,12 +268,9 @@ export const getNotTrackedFlightsFromStatus = (chatId: number, ctx: DurableObjec
271268
.filter((flight) => flight.status !== 'LANDED' && flight.status !== 'CANCELED')
272269
}
273270

274-
export const getFlightIdByNumberFromStatus = (
275-
flightNumber: string,
276-
ctx: DurableObjectState<DOProps>
277-
): string | undefined => {
271+
export const getFlightIdByNumber = (flightNumber: string, ctx: DurableObjectState<DOProps>): string | undefined => {
278272
const nowIdt = getCurrentIdtTime(ctx)
279-
const allFlights = getCurrentFlightsFromStatus(ctx)
273+
const allFlights = getCurrentFlights(ctx)
280274

281275
const futureFlights = allFlights
282276
.filter((flight) => flight.flight_number === flightNumber && flight.eta > nowIdt.getTime())

workers/src/utils/formatting.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getUserTrackedFlightsFromStatus } from '../services/flightData'
1+
import { getUserTrackedFlights } from '../services/flightData'
22
import { getCurrentIdtTime } from './dateTime'
33
import type { Env } from '../env'
44
import type { Flight, InlineKeyboardButton, InlineKeyboardMarkup, DOProps } from '../types'
@@ -49,7 +49,7 @@ export const formatTrackingListOptimized = (
4949
env: Env,
5050
ctx: DurableObjectState<DOProps>
5151
): { text: string; replyMarkup: InlineKeyboardMarkup | null } => {
52-
const flights = getUserTrackedFlightsFromStatus(chatId, ctx)
52+
const flights = getUserTrackedFlights(chatId, ctx)
5353

5454
if (flights.length === 0)
5555
return {
@@ -115,24 +115,29 @@ export const escapeMarkdown = (text: string) => {
115115
.replace(/~/g, '\\~') // Escape tildes
116116
}
117117

118-
export const formatFlightSuggestions = (flights: Flight[], currentPage: number = 0, totalFlights: number = 0, ctx?: DurableObjectState<DOProps>) => {
118+
export const formatFlightSuggestions = (
119+
flights: Flight[],
120+
currentPage: number = 0,
121+
totalFlights: number = 0,
122+
ctx?: DurableObjectState<DOProps>
123+
) => {
119124
if (flights.length === 0) {
120125
return {
121126
text: 'No flights available for tracking right now (need 1+ hour until arrival).',
122127
replyMarkup: null,
123128
}
124129
}
125-
130+
126131
const startFlightNumber = currentPage * 5 + 1
127132
const endFlightNumber = startFlightNumber + flights.length - 1
128-
133+
129134
let message = `🎯 These flights arrive next:\n\n`
130-
135+
131136
// Add page info if we have pagination
132137
if (totalFlights > 5) {
133138
message += `📄 Showing flights ${startFlightNumber}-${endFlightNumber} of ${totalFlights}\n\n`
134139
}
135-
140+
136141
const inlineKeyboard: InlineKeyboardButton[][] = []
137142

138143
flights.forEach((flight, index) => {

0 commit comments

Comments
 (0)