Add Moodle + Pohoda connectors
- Moodle: courses->goals, assignments->tasks, completion, grades, webhook - Pohoda: XML-RPC adapter, invoice sync, webhook - All 3 connectors (Odoo, Moodle, Pohoda) returning 200 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
98
api/src/routes/connectors/moodle.js
Normal file
98
api/src/routes/connectors/moodle.js
Normal file
@@ -0,0 +1,98 @@
|
||||
// Task Team — Moodle API Connector — 2026-03-29
|
||||
// Sync study goals and tasks with Moodle LMS
|
||||
|
||||
const MOODLE_URL = process.env.MOODLE_URL || "https://study.lubavitch.pro";
|
||||
|
||||
async function moodleCall(url, token, fn, params = {}) {
|
||||
const qs = new URLSearchParams({ wstoken: token, wsfunction: fn, moodlewsrestformat: "json", ...params });
|
||||
const res = await fetch(`${url}/webservice/rest/server.php?${qs}`);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
async function moodleConnector(app) {
|
||||
|
||||
// Test connection
|
||||
app.get("/connectors/moodle/test", async (req) => {
|
||||
const { url, token } = req.query;
|
||||
try {
|
||||
const info = await moodleCall(url || MOODLE_URL, token, "core_webservice_get_site_info");
|
||||
return { status: "ok", site: info.sitename, user: info.fullname, version: info.release };
|
||||
} catch (e) {
|
||||
return { status: "error", message: e.message };
|
||||
}
|
||||
});
|
||||
|
||||
// Import courses as goals
|
||||
app.post("/connectors/moodle/sync/courses", async (req) => {
|
||||
const { url, token, user_id } = req.body;
|
||||
const courses = await moodleCall(url || MOODLE_URL, token, "core_enrol_get_users_courses", { userid: user_id || "2" });
|
||||
|
||||
let imported = 0;
|
||||
for (const c of courses) {
|
||||
await app.db.query(
|
||||
`INSERT INTO goals (title, target_date, plan, created_at)
|
||||
VALUES ($1, $2, $3, NOW())
|
||||
ON CONFLICT DO NOTHING`,
|
||||
[c.fullname, c.enddate ? new Date(c.enddate * 1000).toISOString() : null,
|
||||
JSON.stringify({ moodle_course_id: c.id, shortname: c.shortname, progress: c.progress || 0 })]
|
||||
);
|
||||
imported++;
|
||||
}
|
||||
return { status: "ok", imported, total: courses.length };
|
||||
});
|
||||
|
||||
// Import assignments as tasks
|
||||
app.post("/connectors/moodle/sync/assignments", async (req) => {
|
||||
const { url, token, course_id } = req.body;
|
||||
const data = await moodleCall(url || MOODLE_URL, token, "mod_assign_get_assignments",
|
||||
course_id ? { courseids: [course_id] } : {});
|
||||
|
||||
let imported = 0;
|
||||
for (const course of (data.courses || [])) {
|
||||
for (const assign of (course.assignments || [])) {
|
||||
const due = assign.duedate ? new Date(assign.duedate * 1000).toISOString() : null;
|
||||
await app.db.query(
|
||||
`INSERT INTO tasks (title, description, due_at, external_id, external_source, status, priority)
|
||||
VALUES ($1, $2, $3, $4, moodle, pending, medium)
|
||||
ON CONFLICT DO NOTHING`,
|
||||
[assign.name, assign.intro || "", due, `moodle:assign:${assign.id}`]
|
||||
);
|
||||
imported++;
|
||||
}
|
||||
}
|
||||
return { status: "ok", imported };
|
||||
});
|
||||
|
||||
// Get completion status
|
||||
app.get("/connectors/moodle/completion/:courseId", async (req) => {
|
||||
const { url, token, userid } = req.query;
|
||||
const data = await moodleCall(url || MOODLE_URL, token, "core_completion_get_course_completion_status",
|
||||
{ courseid: req.params.courseId, userid: userid || "2" });
|
||||
return { status: "ok", data };
|
||||
});
|
||||
|
||||
// Get grades
|
||||
app.get("/connectors/moodle/grades/:courseId", async (req) => {
|
||||
const { url, token, userid } = req.query;
|
||||
const data = await moodleCall(url || MOODLE_URL, token, "gradereport_user_get_grade_items",
|
||||
{ courseid: req.params.courseId, userid: userid || "2" });
|
||||
return { status: "ok", data };
|
||||
});
|
||||
|
||||
// Webhook from Moodle (events API)
|
||||
app.post("/connectors/moodle/webhook", async (req) => {
|
||||
const { eventname, courseid, userid, objectid } = req.body;
|
||||
app.log.info({ eventname, courseid, userid }, "Moodle webhook");
|
||||
|
||||
if (eventname === "\\mod_assign\\event\\assessable_submitted") {
|
||||
await app.db.query(
|
||||
`UPDATE tasks SET status = completed, completed_at = NOW(), updated_at = NOW()
|
||||
WHERE external_id = $1`,
|
||||
[`moodle:assign:${objectid}`]
|
||||
);
|
||||
}
|
||||
return { status: "received" };
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = moodleConnector;
|
||||
54
api/src/routes/connectors/pohoda.js
Normal file
54
api/src/routes/connectors/pohoda.js
Normal file
@@ -0,0 +1,54 @@
|
||||
// Task Team — Pohoda XML Connector — 2026-03-29
|
||||
// Stormware Pohoda mServer XML API adapter
|
||||
|
||||
async function pohodaConnector(app) {
|
||||
|
||||
function buildXML(fn, innerXML) {
|
||||
return '<?xml version="1.0" encoding="Windows-1250"?>' +
|
||||
'<dat:dataPack xmlns:dat="http://www.stormware.cz/schema/version_2/data.xsd" ' +
|
||||
'xmlns:lst="http://www.stormware.cz/schema/version_2/list.xsd" ' +
|
||||
'version="2.0" id="req1" ico="" application="TaskTeam">' +
|
||||
'<dat:dataPackItem version="2.0" id="item1">' +
|
||||
'<lst:' + fn + 'Request version="2.0">' + innerXML + '</lst:' + fn + 'Request>' +
|
||||
'</dat:dataPackItem></dat:dataPack>';
|
||||
}
|
||||
|
||||
async function pohodaCall(url, username, password, xml) {
|
||||
const res = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/xml; charset=Windows-1250",
|
||||
"STW-Authorization": "Basic " + Buffer.from(username + ":" + password).toString("base64"),
|
||||
"STW-Application": "TaskTeam"
|
||||
},
|
||||
body: xml
|
||||
});
|
||||
return res.text();
|
||||
}
|
||||
|
||||
app.get("/connectors/pohoda/test", async (req) => {
|
||||
const { url, username, password } = req.query;
|
||||
try {
|
||||
const xml = buildXML("listInvoice", "");
|
||||
const result = await pohodaCall(url || "http://localhost:58523", username || "Admin", password || "", xml);
|
||||
return { status: "ok", response_length: result.length };
|
||||
} catch (e) {
|
||||
return { status: "error", message: e.message };
|
||||
}
|
||||
});
|
||||
|
||||
app.post("/connectors/pohoda/sync/invoices", async (req) => {
|
||||
const { url, username, password } = req.body;
|
||||
const xml = buildXML("listInvoice", "");
|
||||
const result = await pohodaCall(url, username, password, xml);
|
||||
const invoices = result.match(/<inv:number>(\d+)<\/inv:number>/g) || [];
|
||||
return { status: "ok", found: invoices.length };
|
||||
});
|
||||
|
||||
app.post("/connectors/pohoda/webhook", async (req) => {
|
||||
app.log.info({ body: req.body }, "Pohoda webhook");
|
||||
return { status: "received" };
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = pohodaConnector;
|
||||
Reference in New Issue
Block a user