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

Function sync

src/sync/station.ts:10–143  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

8 `st_${type}_${id}`.toLocaleLowerCase()
9
10const sync = async () => {
11 if (!process.env.DATABASE_URL) throw new Error("DATABASE_URL env is missing")
12 if (!process.env.COMULINE_ENV) throw new Error("COMULINE_ENV env is missing")
13 if (!process.env.KRL_ENDPOINT_BASE_URL)
14 throw new Error("KRL_ENDPOINT_BASE_URL env is missing")
15
16 const { db } = new Database({
17 COMULINE_ENV: process.env.COMULINE_ENV,
18 DATABASE_URL: process.env.DATABASE_URL,
19 })
20
21 const schema = z.object({
22 status: z.number(),
23 message: z.string(),
24 data: z.array(
25 z.object({
26 sta_id: z.string(),
27 sta_name: z.string(),
28 group_wil: z.number(),
29 fg_enable: z.number(),
30 }),
31 ),
32 })
33
34 const url = `${process.env.KRL_ENDPOINT_BASE_URL}/krl-station`
35
36 const req = await fetch(url, {
37 method: "GET",
38 headers: KAI_HEADERS,
39 })
40
41 if (!req.ok)
42 throw new Error(
43 `[SYNC][STATION] Request failed with status: ${req.status}`,
44 {
45 cause: await req.text(),
46 },
47 )
48
49 const data = await req.json()
50
51 const parsedData = schema.safeParse(data)
52
53 if (!parsedData.success) {
54 throw new Error(parsedData.error.message, {
55 cause: parsedData.error.cause,
56 })
57 }
58
59 const filteredStation = parsedData.data.data.filter(
60 (d) => !d.sta_id.includes("WIL"),
61 )
62
63 const stations = filteredStation.map((s) => {
64 return {
65 uid: createStationKey("KRL", s.sta_id),
66 id: s.sta_id,
67 name: s.sta_name,

Callers 1

station.tsFile · 0.70

Calls 1

createStationKeyFunction · 0.85

Tested by

no test coverage detected