forked from UKSOURCE/cms.hailearning.edu.vn
feat: standardize admin form limits and guidance
This commit is contained in:
@@ -1,6 +1,22 @@
|
||||
const {addBaseUrlToImages} = require("../utils/imageHelper");
|
||||
const Activity = require("../models/activity");
|
||||
const mongoose = require('mongoose');
|
||||
const {
|
||||
validateLengthRules,
|
||||
summarizeLengthErrors,
|
||||
} = require("../utils/lengthValidation");
|
||||
const {
|
||||
ACTIVITY_LENGTH_RULES,
|
||||
} = require("../constants/contentLengthRules");
|
||||
|
||||
const getActivityLengthMessage = (validation) =>
|
||||
summarizeLengthErrors(validation, 3) ||
|
||||
"One or more fields exceed the allowed length.";
|
||||
|
||||
const redirectWithLengthError = (req, res, path, validation) => {
|
||||
req.flash("error_msg", getActivityLengthMessage(validation));
|
||||
return req.session.save(() => res.redirect(path));
|
||||
};
|
||||
|
||||
// -------------------- Public (API) exports --------------------
|
||||
|
||||
@@ -302,6 +318,15 @@ exports.updateFilters = async (req, res) => {
|
||||
try {
|
||||
// Provide minimal valid fields when inserting a new filters document so
|
||||
// schema validators (e.g., age validator) do not fail on upsert.
|
||||
const filterLengthValidation = validateLengthRules(
|
||||
{ filters: sanitizedFilters },
|
||||
ACTIVITY_LENGTH_RULES,
|
||||
);
|
||||
if (!filterLengthValidation.valid) {
|
||||
req.flash("error_msg", getActivityLengthMessage(filterLengthValidation));
|
||||
return res.redirect("/admin/activity");
|
||||
}
|
||||
|
||||
const setOnInsert = {
|
||||
name: "_filters_doc",
|
||||
price: 0,
|
||||
@@ -353,6 +378,14 @@ exports.updateHero = async (req, res) => {
|
||||
bannerImageBooking: bannerImageBooking || '/uploads/banner/b9.jpg',
|
||||
};
|
||||
|
||||
const heroLengthValidation = validateLengthRules(
|
||||
{ hero },
|
||||
ACTIVITY_LENGTH_RULES,
|
||||
);
|
||||
if (!heroLengthValidation.valid) {
|
||||
return redirectWithLengthError(req, res, "/admin/activity", heroLengthValidation);
|
||||
}
|
||||
|
||||
// Update all activity docs to keep hero consistent
|
||||
await Activity.updateMany({ isFiltersDoc: { $ne: true } }, { $set: { hero } });
|
||||
|
||||
@@ -413,6 +446,16 @@ exports.create = async (req, res) => {
|
||||
try {
|
||||
const activityData = parseActivityFormData(req.body);
|
||||
|
||||
const lengthValidation = validateLengthRules(activityData, ACTIVITY_LENGTH_RULES);
|
||||
if (!lengthValidation.valid) {
|
||||
return redirectWithLengthError(
|
||||
req,
|
||||
res,
|
||||
"/admin/activity/create",
|
||||
lengthValidation,
|
||||
);
|
||||
}
|
||||
|
||||
const newActivity = new Activity(activityData);
|
||||
await newActivity.save();
|
||||
|
||||
@@ -465,6 +508,16 @@ exports.update = async (req, res) => {
|
||||
// Force status to active on update (always set isActive true when editing)
|
||||
activityData.isActive = true;
|
||||
|
||||
const lengthValidation = validateLengthRules(activityData, ACTIVITY_LENGTH_RULES);
|
||||
if (!lengthValidation.valid) {
|
||||
return redirectWithLengthError(
|
||||
req,
|
||||
res,
|
||||
`/admin/activity/${req.params.id}/edit`,
|
||||
lengthValidation,
|
||||
);
|
||||
}
|
||||
|
||||
await Activity.findByIdAndUpdate(req.params.id, activityData, {new: true});
|
||||
|
||||
req.flash("success_msg", "Activity updated successfully");
|
||||
|
||||
Reference in New Issue
Block a user