backend_adaptive_learning/middlewares/checkLevel.js

30 lines
764 B
JavaScript
Raw Normal View History

import models from "../models/index.js";
import response from "../response.js";
export const checkMaxLevelsPerTopic = async (req, res, next) => {
const { topic_id } = req.body;
try {
// Hitung jumlah level yang ada pada topic_id yang diberikan
const levelCount = await models.Level.count({
where: { topic_id },
});
// Periksa apakah jumlah level sudah mencapai 5
if (levelCount >= 5) {
return response(
400,
null,
"Cannot add more than 5 levels to a single topic",
res
);
}
// Lanjutkan ke middleware atau route handler berikutnya jika belum mencapai 5
next();
} catch (error) {
console.log(error);
return response(500, null, "Internal Server Error", res);
}
};