258 lines
9.4 KiB
JavaScript
258 lines
9.4 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.buildVoterKey = buildVoterKey;
|
|
exports.publicFaqFileUrl = publicFaqFileUrl;
|
|
exports.resolveFaqFilePath = resolveFaqFilePath;
|
|
exports.listPublicCategories = listPublicCategories;
|
|
exports.listPublicFaqs = listPublicFaqs;
|
|
exports.listPopularFaqs = listPopularFaqs;
|
|
exports.getPublicFaq = getPublicFaq;
|
|
exports.recordFaqView = recordFaqView;
|
|
exports.recordFaqHelpful = recordFaqHelpful;
|
|
exports.listAdminCategories = listAdminCategories;
|
|
exports.createAdminCategory = createAdminCategory;
|
|
exports.updateAdminCategory = updateAdminCategory;
|
|
exports.deleteAdminCategory = deleteAdminCategory;
|
|
exports.listAdminFaqs = listAdminFaqs;
|
|
exports.createAdminFaq = createAdminFaq;
|
|
exports.updateAdminFaq = updateAdminFaq;
|
|
exports.deleteAdminFaq = deleteAdminFaq;
|
|
exports.readFaqFile = readFaqFile;
|
|
const path_1 = __importDefault(require("path"));
|
|
const fs_1 = __importDefault(require("fs"));
|
|
const crypto_1 = __importDefault(require("crypto"));
|
|
const repo = __importStar(require("../repositories/faqRepository"));
|
|
const faqUpload_1 = require("../middleware/faqUpload");
|
|
function slugify(text) {
|
|
return text
|
|
.trim()
|
|
.toLowerCase()
|
|
.normalize('NFD')
|
|
.replace(/[\u0300-\u036f]/g, '')
|
|
.replace(/[^a-z0-9]+/g, '-')
|
|
.replace(/^-+|-+$/g, '')
|
|
.slice(0, 64);
|
|
}
|
|
function mapCategory(row) {
|
|
return {
|
|
id: row.id,
|
|
slug: row.slug,
|
|
name: row.name,
|
|
sortOrder: row.sort_order,
|
|
isActive: row.is_active,
|
|
createdAt: row.created_at.toISOString(),
|
|
updatedAt: row.updated_at.toISOString(),
|
|
};
|
|
}
|
|
function mapEntryPublic(row) {
|
|
return {
|
|
id: row.id,
|
|
categoryId: row.category_id,
|
|
categorySlug: row.category_slug,
|
|
categoryName: row.category_name,
|
|
question: row.question,
|
|
answerHtml: row.answer_html,
|
|
sortOrder: row.sort_order,
|
|
viewCount: row.view_count,
|
|
helpfulYes: row.helpful_yes,
|
|
helpfulNo: row.helpful_no,
|
|
};
|
|
}
|
|
function mapEntryAdmin(row) {
|
|
return {
|
|
...mapEntryPublic(row),
|
|
isActive: row.is_active,
|
|
createdBy: row.created_by,
|
|
updatedBy: row.updated_by,
|
|
createdByName: row.created_by_name,
|
|
updatedByName: row.updated_by_name,
|
|
createdAt: row.created_at.toISOString(),
|
|
updatedAt: row.updated_at.toISOString(),
|
|
};
|
|
}
|
|
function buildVoterKey(req, userId) {
|
|
if (userId)
|
|
return `user:${userId}`;
|
|
const ip = req.ip ?? req.socket.remoteAddress ?? 'unknown';
|
|
const ua = req.get('user-agent') ?? '';
|
|
const hash = crypto_1.default.createHash('sha256').update(`${ip}|${ua}`).digest('hex').slice(0, 32);
|
|
return `anon:${hash}`;
|
|
}
|
|
function publicFaqFileUrl(filename) {
|
|
return `/api/v1/faq/files/${encodeURIComponent(filename)}`;
|
|
}
|
|
function resolveFaqFilePath(filename) {
|
|
return path_1.default.join((0, faqUpload_1.ensureFaqUploadDir)(), path_1.default.basename(filename));
|
|
}
|
|
async function listPublicCategories() {
|
|
const rows = await repo.listCategories(true);
|
|
return rows.map(mapCategory);
|
|
}
|
|
async function listPublicFaqs(filters) {
|
|
const rows = await repo.listPublicEntries({
|
|
categorySlug: filters.category,
|
|
search: filters.search,
|
|
});
|
|
return rows.map(mapEntryPublic);
|
|
}
|
|
async function listPopularFaqs(limit) {
|
|
const rows = await repo.listPopularEntries(limit ?? 5);
|
|
return rows.map(mapEntryPublic);
|
|
}
|
|
async function getPublicFaq(id) {
|
|
const row = await repo.getEntryById(id);
|
|
if (!row || !row.is_active)
|
|
throw new Error('FAQ not found');
|
|
return mapEntryPublic(row);
|
|
}
|
|
async function recordFaqView(id) {
|
|
const row = await repo.getEntryById(id);
|
|
if (!row || !row.is_active)
|
|
throw new Error('FAQ not found');
|
|
await repo.incrementViewCount(id);
|
|
return { ok: true };
|
|
}
|
|
async function recordFaqHelpful(id, helpful, voterKey) {
|
|
const row = await repo.getEntryById(id);
|
|
if (!row || !row.is_active)
|
|
throw new Error('FAQ not found');
|
|
return repo.recordHelpfulVote(id, voterKey, helpful);
|
|
}
|
|
async function listAdminCategories() {
|
|
const rows = await repo.listCategories(false);
|
|
return rows.map(mapCategory);
|
|
}
|
|
async function createAdminCategory(body) {
|
|
const name = typeof body.name === 'string' ? body.name.trim() : '';
|
|
if (!name)
|
|
throw new Error('Category name is required');
|
|
const slug = typeof body.slug === 'string' && body.slug.trim()
|
|
? slugify(body.slug)
|
|
: slugify(name);
|
|
if (!slug)
|
|
throw new Error('Invalid category slug');
|
|
return mapCategory(await repo.createCategory({
|
|
slug,
|
|
name,
|
|
sortOrder: typeof body.sortOrder === 'number' ? body.sortOrder : 0,
|
|
isActive: body.isActive !== false,
|
|
}));
|
|
}
|
|
async function updateAdminCategory(id, body) {
|
|
const updated = await repo.updateCategory(id, {
|
|
slug: typeof body.slug === 'string' ? slugify(body.slug) : undefined,
|
|
name: typeof body.name === 'string' ? body.name.trim() : undefined,
|
|
sortOrder: typeof body.sortOrder === 'number' ? body.sortOrder : undefined,
|
|
isActive: typeof body.isActive === 'boolean' ? body.isActive : undefined,
|
|
});
|
|
if (!updated)
|
|
throw new Error('Category not found');
|
|
return mapCategory(updated);
|
|
}
|
|
async function deleteAdminCategory(id) {
|
|
const count = await repo.countEntriesInCategory(id);
|
|
if (count > 0) {
|
|
throw new Error('Cannot delete category that still contains FAQ entries');
|
|
}
|
|
const ok = await repo.deleteCategory(id);
|
|
if (!ok)
|
|
throw new Error('Category not found');
|
|
return { ok: true };
|
|
}
|
|
async function listAdminFaqs(filters) {
|
|
const rows = await repo.listAdminEntries(filters);
|
|
return rows.map(mapEntryAdmin);
|
|
}
|
|
async function createAdminFaq(userId, body) {
|
|
const question = typeof body.question === 'string' ? body.question.trim() : '';
|
|
const answerHtml = typeof body.answerHtml === 'string' ? body.answerHtml : '';
|
|
const categoryId = Number(body.categoryId);
|
|
if (!question)
|
|
throw new Error('Question is required');
|
|
if (!answerHtml.trim())
|
|
throw new Error('Answer is required');
|
|
if (!Number.isFinite(categoryId))
|
|
throw new Error('categoryId is required');
|
|
const cat = await repo.getCategoryById(categoryId);
|
|
if (!cat)
|
|
throw new Error('Category not found');
|
|
const row = await repo.createEntry({
|
|
categoryId,
|
|
question,
|
|
answerHtml,
|
|
isActive: body.isActive !== false,
|
|
sortOrder: typeof body.sortOrder === 'number' ? body.sortOrder : 0,
|
|
userId,
|
|
});
|
|
const adminRows = await repo.listAdminEntries({});
|
|
const full = adminRows.find((r) => r.id === row.id);
|
|
if (!full)
|
|
throw new Error('FAQ created but not found');
|
|
return mapEntryAdmin(full);
|
|
}
|
|
async function updateAdminFaq(id, userId, body) {
|
|
const updated = await repo.updateEntry(id, {
|
|
categoryId: typeof body.categoryId === 'number' ? body.categoryId : undefined,
|
|
question: typeof body.question === 'string' ? body.question.trim() : undefined,
|
|
answerHtml: typeof body.answerHtml === 'string' ? body.answerHtml : undefined,
|
|
isActive: typeof body.isActive === 'boolean' ? body.isActive : undefined,
|
|
sortOrder: typeof body.sortOrder === 'number' ? body.sortOrder : undefined,
|
|
userId,
|
|
});
|
|
if (!updated)
|
|
throw new Error('FAQ not found');
|
|
const adminRows = await repo.listAdminEntries({});
|
|
const full = adminRows.find((r) => r.id === id);
|
|
if (!full)
|
|
throw new Error('FAQ not found');
|
|
return mapEntryAdmin(full);
|
|
}
|
|
async function deleteAdminFaq(id) {
|
|
const ok = await repo.deleteEntry(id);
|
|
if (!ok)
|
|
throw new Error('FAQ not found');
|
|
return { ok: true };
|
|
}
|
|
function readFaqFile(filename) {
|
|
const filePath = resolveFaqFilePath(filename);
|
|
if (!fs_1.default.existsSync(filePath))
|
|
return null;
|
|
return filePath;
|
|
}
|
|
//# sourceMappingURL=faqService.js.map
|