forked from UKSOURCE/cms.lams
Refactor the CMS page content system to use a more modular configuration-driven approach. This replaces the monolithic `pageContentConfig.js` with individual configuration files for each page and introduces a shared field utility to standardize editor components. Key changes: - Implement a generic `_renderSingletonPageView` helper to reduce duplication across controllers. - Enhance `_createPageContentController` with `normalizeForEditor`, `normalizeForApi`, and `preparePayload` hooks for custom data transformation. - Create dedicated configuration and view structures for Partnerships, History, Accreditation, Admissions, and Policies pages. - Add a specialized section editor for Policies to manage complex nested content. - Improve the frontend `page-content-editor.js` with support for visibility logic, auto-sequencing for arrays, and URL synchronization for active tabs. - Update data JSON files to align with the new schema.
122 lines
3.9 KiB
JavaScript
122 lines
3.9 KiB
JavaScript
const { addBaseUrlToImages } = require("../utils/imageHelper");
|
|
const jsonHelper = require("../utils/jsonHelper");
|
|
const writeAuditLog = require("../audit/writeAuditLog");
|
|
const diffObject = require("../audit/diffObject");
|
|
|
|
function createPageContentController({
|
|
model,
|
|
modelName,
|
|
auditAction,
|
|
editorConfig,
|
|
normalizeForEditor,
|
|
normalizeForApi,
|
|
preparePayload,
|
|
}) {
|
|
return {
|
|
async index(req, res) {
|
|
try {
|
|
const doc = await model.getSingle();
|
|
const rawData = doc.toObject();
|
|
const data = normalizeForEditor ? normalizeForEditor(rawData, req) : rawData;
|
|
const frontendUrl = process.env.FRONTEND_URL || "http://localhost:3000";
|
|
const backendUrl =
|
|
process.env.BACKEND_URL ?? `${req.protocol}://${req.get("host")}`;
|
|
|
|
res.render("admin/pageContent/index", {
|
|
layout: "layouts/main",
|
|
title: editorConfig.title,
|
|
subtitle: editorConfig.subtitle,
|
|
data,
|
|
editorConfig,
|
|
activeTab: req.query.tab || editorConfig.tabs[0].key,
|
|
frontendUrl,
|
|
backendUrl,
|
|
previewUrl: `${frontendUrl}${editorConfig.previewPath}`,
|
|
currentPath: req.path,
|
|
user: req.session.user,
|
|
});
|
|
} catch (error) {
|
|
console.error(`${editorConfig.key} index error:`, error);
|
|
req.flash("error_msg", `Error loading ${editorConfig.title}`);
|
|
return req.session.save(() => res.redirect("/admin/dashboard"));
|
|
}
|
|
},
|
|
|
|
async update(req, res) {
|
|
try {
|
|
const rawPayload =
|
|
typeof req.body.pageJson === "string"
|
|
? JSON.parse(req.body.pageJson)
|
|
: req.body.pageJson || {};
|
|
|
|
const activeTab = req.body.activeTab || editorConfig.tabs[0].key;
|
|
const doc = await model.getSingle();
|
|
const beforeData = JSON.parse(JSON.stringify(doc.toObject()));
|
|
const payload = preparePayload
|
|
? preparePayload(rawPayload, { req, doc, beforeData })
|
|
: rawPayload;
|
|
|
|
doc.set(payload);
|
|
Object.keys(payload).forEach((key) => doc.markModified(key));
|
|
await doc.save();
|
|
|
|
const afterData = JSON.parse(JSON.stringify(doc.toObject()));
|
|
const changes = diffObject(beforeData, afterData);
|
|
|
|
if (changes.length > 0) {
|
|
await writeAuditLog({
|
|
model: modelName,
|
|
documentId: doc._id,
|
|
action: auditAction,
|
|
before: beforeData,
|
|
after: afterData,
|
|
changes,
|
|
req,
|
|
});
|
|
}
|
|
|
|
const finalData = await model
|
|
.findOne()
|
|
.select("-_id -__v -createdAt -updatedAt")
|
|
.lean();
|
|
jsonHelper.writeJsonFile(editorConfig.dataFile, finalData);
|
|
|
|
req.flash("success_msg", `${editorConfig.title} updated successfully`);
|
|
return req.session.save(() =>
|
|
res.redirect(`${editorConfig.routeBase}?tab=${activeTab}`),
|
|
);
|
|
} catch (error) {
|
|
console.error(`${editorConfig.key} update error:`, error);
|
|
req.flash(
|
|
"error_msg",
|
|
`Error updating ${editorConfig.title}: ${error.message}`,
|
|
);
|
|
return req.session.save(() =>
|
|
res.redirect(
|
|
`${editorConfig.routeBase}?tab=${req.body.activeTab || ""}`,
|
|
),
|
|
);
|
|
}
|
|
},
|
|
|
|
async api(req, res) {
|
|
try {
|
|
const doc = await model.getSingle();
|
|
const rawData = doc.toObject();
|
|
const backendUrl =
|
|
process.env.BACKEND_URL ?? `${req.protocol}://${req.get("host")}`;
|
|
const normalized = normalizeForApi ? normalizeForApi(rawData, req) : rawData;
|
|
const processed = addBaseUrlToImages(normalized, backendUrl);
|
|
return res.json(processed);
|
|
} catch (error) {
|
|
console.error(`${editorConfig.key} api error:`, error);
|
|
return res
|
|
.status(500)
|
|
.json({ error: `Error loading ${editorConfig.key} data` });
|
|
}
|
|
},
|
|
};
|
|
}
|
|
|
|
module.exports = createPageContentController;
|