feat: add Tjing course import

Search and import courses with layouts from Tjing's GraphQL API.
Total par is calculated from individual hole data. Courses are saved
with a tjing.se link as unique identifier to prevent duplicates.
This commit is contained in:
Samuel Enocsson
2026-03-20 08:35:37 +01:00
parent 808619a04b
commit eb77b1f32b
5 changed files with 281 additions and 1 deletions
+53 -1
View File
@@ -1,7 +1,8 @@
const express = require('express');
const router = express.Router();
const { db } = require('../db');
const { getAllCoursesFromDB, getLayoutsForCourse, updateLayoutRating } = require('../models/course');
const { getAllCoursesFromDB, getLayoutsForCourse, updateLayoutRating, saveCourseToDB, getOrCreateLayout } = require('../models/course');
const { searchTjingCourses, getTjingCourse } = require('../scrapers/tjing');
const { launchBrowser } = require('../scrapers/browser');
const { layoutEventCache, scrapeCourseDirectory, scrapeCourseLayouts, scrapeEventResults } = require('../scrapers/course-puppeteer');
const logger = require('../logger');
@@ -369,4 +370,55 @@ router.post('/api/scrape-event-results/:courseId', async (req, res) => {
}
});
// Search Tjing for courses
router.get('/api/tjing/search', async (req, res) => {
try {
const { q } = req.query;
if (!q || q.trim().length < 2) {
return res.json([]);
}
const courses = await searchTjingCourses(q.trim());
res.json(courses);
} catch (error) {
logger.error({ err: error }, 'Error searching Tjing');
res.status(500).json({ error: 'Failed to search Tjing' });
}
});
// Import a course from Tjing
router.post('/api/tjing/import/:tjingId', async (req, res) => {
try {
const { tjingId } = req.params;
const courseData = await getTjingCourse(tjingId);
if (courseData.layouts.length === 0) {
return res.status(400).json({ error: 'Course has no published layouts on Tjing' });
}
const courseId = await saveCourseToDB({
name: courseData.name,
link: 'https://tjing.se/courses/' + tjingId,
city: courseData.address || ''
});
let layoutCount = 0;
for (const layout of courseData.layouts) {
await getOrCreateLayout(courseId, layout.name, layout.par);
layoutCount++;
}
logger.info(`Imported from Tjing: "${courseData.name}" with ${layoutCount} layouts`);
res.json({
success: true,
message: `Imported "${courseData.name}" with ${layoutCount} layouts`,
courseName: courseData.name,
layoutCount
});
} catch (error) {
logger.error({ err: error }, 'Error importing from Tjing');
res.status(500).json({ error: 'Failed to import course from Tjing' });
}
});
module.exports = router;
+95
View File
@@ -0,0 +1,95 @@
const logger = require('../logger');
const TJING_API = 'https://api.tjing.se/graphql';
async function searchTjingCourses(searchTerm) {
const query = `{
courses(first: 10, filter: { search: "${searchTerm.replace(/"/g, '\\"')}" }) {
id
name
address
type
}
}`;
const response = await fetch(TJING_API, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query })
});
if (!response.ok) {
throw new Error(`Tjing API returned ${response.status}`);
}
const data = await response.json();
if (data.errors) {
throw new Error(data.errors[0].message);
}
return data.data.courses || [];
}
async function getTjingCourse(courseId) {
const query = `{
course(courseId: "${courseId.replace(/"/g, '\\"')}") {
id
name
address
layouts {
id
name
published
latestVersion {
holes {
number
par
}
}
}
}
}`;
const response = await fetch(TJING_API, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query })
});
if (!response.ok) {
throw new Error(`Tjing API returned ${response.status}`);
}
const data = await response.json();
if (data.errors) {
throw new Error(data.errors[0].message);
}
const course = data.data.course;
if (!course) {
throw new Error('Course not found');
}
// Calculate total par per layout from holes
const layouts = (course.layouts || [])
.filter(l => l.published && l.latestVersion && l.latestVersion.holes.length > 0)
.map(l => ({
name: l.name,
par: l.latestVersion.holes.reduce((sum, h) => sum + h.par, 0),
holes: l.latestVersion.holes.length
}));
return {
name: course.name,
address: course.address,
tjingId: course.id,
layouts
};
}
module.exports = {
searchTjingCourses,
getTjingCourse
};