32 lines
800 B
TypeScript
32 lines
800 B
TypeScript
import express, { Router } from 'express'
|
|
|
|
import { ApiKeyModel } from '../../../models/apikey.model.ts'
|
|
|
|
import { syncWanikaniData } from '../../../services/wanikaniService.ts'
|
|
|
|
const router = express.Router()
|
|
|
|
interface ApiKeyDocument {
|
|
apiKey?: string;
|
|
}
|
|
|
|
router.get('/sync', async (req, res) => {
|
|
try {
|
|
const apiKeyDoc = await ApiKeyModel.findOne() as ApiKeyDocument | null
|
|
|
|
const apiKey = apiKeyDoc?.apiKey
|
|
|
|
console.log(apiKey, apiKeyDoc)
|
|
if (!apiKey || apiKey.trim() === '') {
|
|
return res.status(401).json({ error: 'API Key not configured. Please sync your key first.' })
|
|
}
|
|
await syncWanikaniData(apiKey)
|
|
res.json({ success: true })
|
|
} catch (err) {
|
|
console.error(err)
|
|
res.status(500).json({ error: 'Failed to sync WaniKani data' })
|
|
}
|
|
})
|
|
|
|
export default router as Router
|