This commit is contained in:
rgrgogu
2026-05-14 12:59:42 +08:00
parent 06c4286c4a
commit c3663572ae
12 changed files with 570 additions and 536 deletions
+13 -10
View File
@@ -89,7 +89,7 @@ function flattenJsonb(jsonbSchema = {}, prefix = "", exclude = []) {
* @param {string[]} exclude - field names to exclude (e.g. ["password", "otp_code"])
* @returns {Array} attributes array
*/
function modelToAttributes(model, { jsonbSchemas = {}, exclude = [], timestampLabels = {} } = {}) {
function modelToAttributes(model, { jsonbSchemas = {}, exclude = [], timestampLabels = {}, context = "" } = {}) {
const rawAttrs = model.rawAttributes || model.tableAttributes;
const attributes = [];
@@ -113,14 +113,14 @@ function modelToAttributes(model, { jsonbSchemas = {}, exclude = [], timestampLa
// Ordered audit sequence
const auditSequence = [
{ field: 'updatedAt', type: 'date' },
{ field: 'modifiedAt', type: 'date' },
{ field: 'updatedBy', type: 'text' },
{ field: 'createdAt', type: 'date' },
{ field: 'createdBy', type: 'text' },
{ field: 'deletedAt', type: 'date' },
{ field: 'deletedBy', type: 'text' },
];
{ field: "updatedAt", type: "date", hiddenOnList: false, hiddenOnArchived: true },
{ field: "modifiedAt", type: "date", hiddenOnList: false, hiddenOnArchived: true },
{ field: "updatedBy", type: "text", hiddenOnList: false, hiddenOnArchived: true },
{ field: "createdAt", type: "date", hiddenOnList: false, hiddenOnArchived: true },
{ field: "createdBy", type: "text", hiddenOnList: false, hiddenOnArchived: true },
{ field: "deletedAt", type: "date", hiddenOnList: true, hiddenOnArchived: false },
{ field: "deletedBy", type: "text", hiddenOnList: true, hiddenOnArchived: false },
];
// ── Normal fields (excluding audit) ─────────────────────────────────────────
for (const [field, def] of Object.entries(rawAttrs)) {
@@ -148,15 +148,18 @@ function modelToAttributes(model, { jsonbSchemas = {}, exclude = [], timestampLa
}
// ── Audit fields in correct sequence ────────────────────────────────────────
for (const { field, type, order } of auditSequence) {
for (const { field, type, order, hiddenOnList, hiddenOnArchived } of auditSequence) {
if (exclude.includes(field)) continue;
if (!rawAttrs[field]) continue; // skip if field doesn't exist on model
const isArchived = context === "archived";
attributes.push({
name: defaultTimestampLabels[field],
type,
field,
order,
hidden: isArchived ? hiddenOnArchived : hiddenOnList,
options: resolveOptions(rawAttrs[field]?.type),
});
}
+3 -2
View File
@@ -81,7 +81,8 @@ async function paginate(model, req, {
jsonbColumn = null,
findOptions = {},
auditOptions = null, // ← { mdl_Users, parentAlias },
computedAttributes = []
computedAttributes = [],
context = "list",
} = {}) {
const page = Math.max(PAGE_START, parseInt(req.query.page, 10) || PAGE_START);
const limit = Math.min(parseInt(req.query.limit, 10) || PAGE_SIZE, MAX_LIMIT);
@@ -94,7 +95,7 @@ async function paginate(model, req, {
const jsonbExclude = excludeAttributes.filter((f) => f.includes('.'));
const jsonbAttr = jsonbColumn ? excludeJsonbPaths(jsonbColumn, jsonbExclude) : null;
const attributes = modelToAttributes(model, { exclude: excludeAttributes, jsonbSchemas });
const attributes = modelToAttributes(model, { exclude: excludeAttributes, jsonbSchemas, context });
const ALLOWED_FIELDS = attributes.map((a) => a.field);
const { where, order } = buildQuery(filters, sort, ALLOWED_FIELDS);