MCPcopy Create free account
hub / github.com/comuline/api / sync

Function sync

src/sync/schedule.ts:14–200  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12import { KAI_HEADERS } from "./headers"
13
14const sync = async () => {
15 if (!process.env.DATABASE_URL) throw new Error("DATABASE_URL env is missing")
16 if (!process.env.COMULINE_ENV) throw new Error("COMULINE_ENV env is missing")
17 if (!process.env.KRL_ENDPOINT_BASE_URL)
18 throw new Error("KRL_ENDPOINT_BASE_URL env is missing")
19
20 const { db } = new Database({
21 COMULINE_ENV: process.env.COMULINE_ENV,
22 DATABASE_URL: process.env.DATABASE_URL,
23 })
24
25 const stations = await db
26 .select({
27 id: stationTable.id,
28 metadata: stationTable.metadata,
29 name: stationTable.name,
30 })
31 .from(stationTable)
32
33 const batchSizes = 5
34 const totalBatches = Math.ceil(stations.length / batchSizes)
35
36 const schema = z.object({
37 status: z.number(),
38 data: z.array(
39 z.object({
40 train_id: z.string(),
41 ka_name: z.string(),
42 route_name: z.string(),
43 dest: z.string(),
44 time_est: z.string(),
45 color: z.string(),
46 dest_time: z.string(),
47 }),
48 ),
49 })
50
51 for (let i = 0; i < totalBatches; i++) {
52 const start = i * batchSizes
53 const end = start + batchSizes
54 const batch = stations.slice(start, end)
55
56 await Promise.allSettled(
57 batch.map(async ({ id, metadata }) => {
58 await sleep(5000)
59
60 const url = `${process.env.KRL_ENDPOINT_BASE_URL}/schedule?stationid=${id}&timefrom=00:00&timeto=23:00`
61
62 console.info(`[SYNC][SCHEDULE][${id}] Send preflight`)
63 const optionsResponse = await fetch(url, {
64 method: "OPTIONS",
65 headers: {
66 ...KAI_HEADERS,
67 "Access-Control-Request-Method": "GET",
68 "Access-Control-Request-Headers": "authorization,content-type",
69 },
70 credentials: "include",
71 mode: "cors",

Callers 1

schedule.tsFile · 0.70

Calls 3

parseTimeFunction · 0.90
fixNameFunction · 0.85
setMethod · 0.80

Tested by

no test coverage detected