Skip to content

Commit 0e7d3e3

Browse files
authored
add demographics filter (#466)
1 parent cbe9e48 commit 0e7d3e3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

routes/api/ideation.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ module.exports = function (app) {
984984
const favourite = req.query.favourite;
985985
const folderId = req.query.folderId;
986986
const showModerated = req.query.showModerated || false;
987+
const demographicsFilter = req.query.demographics ? JSON.parse(req.query.demographics) : null;
987988
let status = req.query.status || null;
988989
if (!req.user?.id || !req.user?.userId) {
989990
status = 'published';
@@ -1023,6 +1024,24 @@ module.exports = function (app) {
10231024
} else {
10241025
where += ` AND ("Idea"."status" = 'published' OR "Idea"."status" = 'draft' AND "Idea"."authorId"=:userId) `;
10251026
}
1027+
if (demographicsFilter) {
1028+
const conditions = [];
1029+
const demoReplacements = {};
1030+
Object.keys(demographicsFilter).forEach(key => {
1031+
if (demographicsFilter[key].toLowerCase() === 'other') {
1032+
// Use ILIKE substring matching for value "other"
1033+
conditions.push(`("Idea".demographics ->> '${key}') ILIKE '%' || :demographics_${key} || '%'`);
1034+
} else {
1035+
// Use strict equality check
1036+
conditions.push(`"Idea".demographics ->> '${key}' = :demographics_${key}`);
1037+
}
1038+
demoReplacements[`demographics_${key}`] = demographicsFilter[key];
1039+
});
1040+
1041+
if (conditions.length) {
1042+
where += ' AND ' + conditions.join(' AND ');
1043+
}
1044+
}
10261045
let orderSql = ' iv."up.count" DESC, "replies.count" DESC, "Idea"."createdAt" DESC ';
10271046
if (!showModerated || showModerated == "false") {
10281047
where += ` AND "Idea"."deletedAt" IS NULL `;
@@ -1134,6 +1153,9 @@ module.exports = function (app) {
11341153
userId: req.user?.id || req.user?.userId,
11351154
ideationId,
11361155
authorId,
1156+
demographics_age: demographicsFilter?.age,
1157+
demographics_gender: demographicsFilter?.gender,
1158+
demographics_residence: demographicsFilter?.residence,
11371159
status,
11381160
favourite,
11391161
folderId,

0 commit comments

Comments
 (0)