Skip to content

Commit 54edffc

Browse files
committed
added script to import strike data from old bot
1 parent 5995400 commit 54edffc

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/scripts/import-strike-data.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { pool } from '../db'
2+
3+
const url = 'https://casjb.narwhalkid.com/strikes'
4+
5+
interface StrikeData {
6+
id: number
7+
userId: string
8+
amount: number
9+
reason: string
10+
reference: string
11+
time: Date
12+
}
13+
14+
async function fetchStrikeData(): Promise<StrikeData[] | null> {
15+
console.log(`Fetching strike data from: ${url}`)
16+
17+
try {
18+
const response = await fetch(url)
19+
if (!response.ok) {
20+
throw new Error(`HTTP error! status: ${response.status}`)
21+
}
22+
const data: StrikeData[] = await response.json()
23+
return data.map((item) => ({
24+
...item,
25+
time: new Date(item.time),
26+
userId: String(item.userId),
27+
amount: parseInt(`${item.amount}`),
28+
}))
29+
} catch (error) {
30+
console.error('Error fetching strike data:', error)
31+
return null
32+
}
33+
}
34+
35+
async function insertData() {
36+
const data = await fetchStrikeData()
37+
if (!data) return console.log('Data not found')
38+
console.log(`Found ${data.length} objects`)
39+
40+
for (const item of data) {
41+
await pool.query(
42+
`
43+
INSERT INTO strikes (user_id, reason, amount, reference, issued_at) VALUES ($1, $2, $3, $4, $5)
44+
`,
45+
[item.userId, item.reason, item.amount, item.reference, item.time],
46+
)
47+
}
48+
}
49+
50+
insertData()

0 commit comments

Comments
 (0)