@@ -7,6 +7,13 @@ const ENUM_SORT_ORDER = {
|
||||
reg_type: ["system", "google"],
|
||||
};
|
||||
|
||||
// createdBy/updatedBy/deletedBy filter options come from getFieldValues()
|
||||
// as { value: user_id, label: full_name } — the filter sheet selects by id,
|
||||
// so these need an exact match against the bigint column, never the
|
||||
// substring-on-text-cast path used for everything else (that path compares
|
||||
// against the raw id and can never match a name-looking value).
|
||||
const AUDIT_ID_FIELDS = new Set(["createdBy", "updatedBy", "deletedBy"]);
|
||||
|
||||
/**
|
||||
* Builds a Sequelize `where` clause from an array of filters.
|
||||
*
|
||||
@@ -22,6 +29,11 @@ function buildWhere(filters = [], allowedFields = new Set()) {
|
||||
|
||||
const values = Array.isArray(value) ? value : [value];
|
||||
|
||||
if (AUDIT_ID_FIELDS.has(id)) {
|
||||
where.push({ [id]: { [Op.in]: values } });
|
||||
continue;
|
||||
}
|
||||
|
||||
const conditions = values.map((v) =>
|
||||
id.startsWith("personal_info.")
|
||||
? Sequelize.where(
|
||||
|
||||
@@ -30,25 +30,30 @@ const getFieldValues = (Model, logTag, options = {}) => async (req, res) => {
|
||||
return R.error(res, "Invalid or restricted field.", 400);
|
||||
|
||||
if (auditByFields.includes(field)) {
|
||||
// Filtering must match the audit column's real (bigint) id — returning
|
||||
// just the display name here previously made buildWhere() compare a
|
||||
// name string against the raw id column via a text cast, which can
|
||||
// never match. Carry both: `value` (id) is what gets filtered on,
|
||||
// `label` (full_name) is what the filter sheet displays.
|
||||
const tableName = Model.getTableName();
|
||||
const [rows] = selfJoin
|
||||
? await sequelize.query(`
|
||||
SELECT DISTINCT u2."personal_info"->'name'->>'full_name' AS value
|
||||
SELECT DISTINCT u1."${field}" AS value, u2."personal_info"->'name'->>'full_name' AS label
|
||||
FROM "${tableName}" u1
|
||||
JOIN "${tableName}" u2 ON u2.user_id = u1."${field}"
|
||||
WHERE u1."${field}" IS NOT NULL
|
||||
AND u2."personal_info"->'name'->>'full_name' IS NOT NULL
|
||||
ORDER BY value ASC
|
||||
ORDER BY label ASC
|
||||
`)
|
||||
: await sequelize.query(`
|
||||
SELECT DISTINCT u."personal_info"->'name'->>'full_name' AS value
|
||||
SELECT DISTINCT t."${field}" AS value, u."personal_info"->'name'->>'full_name' AS label
|
||||
FROM "${tableName}" t
|
||||
JOIN users u ON u.user_id = t."${field}"
|
||||
WHERE t."${field}" IS NOT NULL
|
||||
AND u."personal_info"->'name'->>'full_name' IS NOT NULL
|
||||
ORDER BY value ASC
|
||||
ORDER BY label ASC
|
||||
`);
|
||||
return R.success(res, "Field values retrieved.", rows.map((r) => r.value).filter(Boolean));
|
||||
return R.success(res, "Field values retrieved.", rows.filter((r) => r.label));
|
||||
}
|
||||
|
||||
if (dateFields.includes(field)) {
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/***********************************************************************************************************************************************************************
|
||||
* File Name : notificationVisibility.util.js
|
||||
* Type : Utility
|
||||
* Description : Shared visibility-window filter for AdminNotification/
|
||||
* UserNotification rows carrying a denormalized start_date/
|
||||
* end_date (copied from NotificationBroadcast at send time —
|
||||
* see notificationBroadcasts.controller.js#sendBroadcast).
|
||||
* Non-announcement notifications have both columns null, so
|
||||
* this filter is a no-op for them.
|
||||
*
|
||||
* Author: Kenneth Obsequio (@lash0000)
|
||||
* Date Created: Jul. 11, 2026
|
||||
***********************************************************************************************************************************************************************/
|
||||
const { Op } = require('sequelize');
|
||||
|
||||
function notInFutureOrExpired(now = new Date()) {
|
||||
return {
|
||||
[Op.and]: [
|
||||
{ [Op.or]: [{ start_date: null }, { start_date: { [Op.lte]: now } }] },
|
||||
{ [Op.or]: [{ end_date: null }, { end_date: { [Op.gte]: now } }] },
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { notInFutureOrExpired };
|
||||
Reference in New Issue
Block a user