This commit is contained in:
rgrgogu
2026-05-06 14:15:04 +08:00
parent a8f10a25d7
commit d5fcc574a4
12 changed files with 389 additions and 219 deletions
+28 -9
View File
@@ -1,6 +1,12 @@
// utils/queryBuilder.js
const { Sequelize, Op } = require("sequelize");
// ─── Custom sort order for ENUM fields ────────────────────────────────────────
const ENUM_SORT_ORDER = {
acc_type: ["admin", "staff", "user"],
reg_type: ["system", "google"],
};
/**
* Builds a Sequelize `where` clause from an array of filters.
*
@@ -12,8 +18,6 @@ function buildWhere(filters = [], allowedFields = new Set()) {
for (const { id, value } of filters) {
if (!id || value === undefined || value === null || value === "") continue;
// Reject fields not in whitelist (if whitelist is provided)
if (allowedFields.size && !allowedFields.has(id)) continue;
const values = Array.isArray(value) ? value : [value];
@@ -48,15 +52,30 @@ function buildOrder(sort = [], allowedFields = new Set()) {
for (const { id, desc } of sort) {
if (!id) continue;
// Reject fields not in whitelist (if whitelist is provided)
if (allowedFields.size && !allowedFields.has(id)) continue;
order.push(
id.startsWith("personal_info.")
? [Sequelize.json(id), desc ? "DESC" : "ASC"]
: [id, desc ? "DESC" : "ASC"]
);
// ─── ENUM fields — use CASE for custom sort order ─────────────────────
if (ENUM_SORT_ORDER[id]) {
const sequence = desc
? [...ENUM_SORT_ORDER[id]].reverse()
: ENUM_SORT_ORDER[id];
const caseExpr = sequence
.map((val, i) => `WHEN '${val}' THEN ${i}`)
.join(" ");
order.push([Sequelize.literal(`CASE "${id}" ${caseExpr} END`)]);
continue;
}
// ─── JSONB dot-notation fields ─────────────────────────────────────────
if (id.startsWith("personal_info.")) {
order.push([Sequelize.json(id), desc ? "DESC" : "ASC"]);
continue;
}
// ─── Regular fields ───────────────────────────────────────────────────
order.push([id, desc ? "DESC" : "ASC"]);
}
return order.length ? order : [["createdAt", "DESC"]];