mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
adjusted
This commit is contained in:
@@ -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"]];
|
||||
|
||||
Reference in New Issue
Block a user