Files
snakkimo-API/src/lib/pairCategories.js
admin 9738d3e35a feat: Profil-Kategorien + Begrüßung in Zielsprache
languages.greeting (de/en/sv geseedet), neue pair_categories-Tabelle
(abgeleitet aus statement- und objektverknüpften Wörtern via
word_categories) inkl. Backfill für bereits veröffentlichte Pairs.
derivePairCategories() wird beim Publish (pairs + pipeline) aufgerufen.
/auth/me liefert language_target_greeting, /auth/stats liefert
categories[] mit Punkten je Kategorie fürs Profil.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-15 12:55:57 +02:00

43 lines
1.7 KiB
JavaScript

const { query } = require('../db');
// Leitet die Kategorien eines (oder mehrerer) Pairs aus den verknüpften Wörtern ab und
// materialisiert sie in pair_categories. Quellen:
// - Statements (positiv/negativ) → statement_*_words → word_categories
// - Objekte → object_words → word_categories
// (Questions haben keine Wort-M2M und entfallen.)
// Re-Run-sicher: löscht vorhandene Zuordnungen der betroffenen Pairs und schreibt neu,
// damit eine erneute Veröffentlichung nach Inhaltsänderungen die Kategorien aktualisiert.
async function derivePairCategories(pairIds) {
const ids = (Array.isArray(pairIds) ? pairIds : [pairIds]).filter(Boolean);
if (!ids.length) return 0;
await query(`DELETE FROM pair_categories WHERE pair_id = ANY($1)`, [ids]);
const r = await query(
`INSERT INTO pair_categories (pair_id, category_id)
SELECT DISTINCT pid, category_id FROM (
SELECT p.id AS pid, wc.category_id
FROM pairs p
JOIN (
SELECT statement_id, word_id FROM statement_positive_words
UNION
SELECT statement_id, word_id FROM statement_negative_words
) sw ON sw.statement_id IN (p.positive_statement_id, p.negative_statement_id)
JOIN word_categories wc ON wc.word_id = sw.word_id
WHERE p.id = ANY($1)
UNION
SELECT op.pair_id AS pid, wc.category_id
FROM object_pairs op
JOIN object_words ow ON ow.object_id = op.object_id
JOIN word_categories wc ON wc.word_id = ow.word_id
WHERE op.pair_id = ANY($1)
) src
WHERE category_id IS NOT NULL
ON CONFLICT (pair_id, category_id) DO NOTHING`,
[ids]
);
return r.rowCount;
}
module.exports = { derivePairCategories };