|
2 | 2 |
|
3 | 3 | const { capitalizeFirstLetter } = require('../../libs/util'); |
4 | 4 |
|
5 | | - |
| 5 | +function flattenArrayValues(obj) { |
| 6 | + const result = {}; |
| 7 | + Object.keys(obj).forEach(key => { |
| 8 | + const value = obj[key]; |
| 9 | + if (Array.isArray(value)) { |
| 10 | + value.forEach((v, idx) => { |
| 11 | + result[`${key}_${idx}`] = v; |
| 12 | + }); |
| 13 | + } else { |
| 14 | + result[key] = value; |
| 15 | + } |
| 16 | + }); |
| 17 | + return result; |
| 18 | +} |
6 | 19 |
|
7 | 20 | module.exports = function (app) { |
8 | 21 | const config = app.get('config'); |
@@ -974,6 +987,7 @@ module.exports = function (app) { |
974 | 987 | const favourite = req.query.favourite; |
975 | 988 | const folderId = req.query.folderId; |
976 | 989 | const showModerated = req.query.showModerated || false; |
| 990 | + const demographicsFilter = req.query.demographics ? JSON.parse(req.query.demographics) : null; |
977 | 991 | let status = req.query.status || null; |
978 | 992 | if (!req.user?.id || !req.user?.userId) { |
979 | 993 | status = 'published'; |
@@ -1013,6 +1027,40 @@ module.exports = function (app) { |
1013 | 1027 | } else { |
1014 | 1028 | where += ` AND ("Idea"."status" = 'published' OR "Idea"."status" = 'draft' AND "Idea"."authorId"=:userId) `; |
1015 | 1029 | } |
| 1030 | + if (demographicsFilter) { |
| 1031 | + const conditions = []; |
| 1032 | + const demoReplacements = {}; |
| 1033 | + Object.keys(demographicsFilter).forEach(key => { |
| 1034 | + const filterValue = demographicsFilter[key]; |
| 1035 | + if (Array.isArray(filterValue)) { |
| 1036 | + const subConditions = []; |
| 1037 | + filterValue.forEach((val, idx) => { |
| 1038 | + const paramName = `demographics_${key}_${idx}`; |
| 1039 | + if (typeof val === 'string' && val.toLowerCase() === 'other') { |
| 1040 | + subConditions.push(`("Idea".demographics ->> '${key}') ILIKE '%' || :${paramName} || '%'`); |
| 1041 | + } else { |
| 1042 | + subConditions.push(`"Idea".demographics ->> '${key}' = :${paramName}`); |
| 1043 | + } |
| 1044 | + demoReplacements[paramName] = val; |
| 1045 | + }); |
| 1046 | + if (subConditions.length) { |
| 1047 | + conditions.push('(' + subConditions.join(' OR ') + ')'); |
| 1048 | + } |
| 1049 | + } else { |
| 1050 | + const paramName = `demographics_${key}`; |
| 1051 | + if (typeof filterValue === 'string' && filterValue.toLowerCase() === 'other') { |
| 1052 | + conditions.push(`("Idea".demographics ->> '${key}') ILIKE '%' || :${paramName} || '%'`); |
| 1053 | + } else { |
| 1054 | + conditions.push(`"Idea".demographics ->> '${key}' = :${paramName}`); |
| 1055 | + } |
| 1056 | + demoReplacements[paramName] = filterValue; |
| 1057 | + } |
| 1058 | + }); |
| 1059 | + |
| 1060 | + if (conditions.length) { |
| 1061 | + where += ' AND ' + conditions.join(' AND '); |
| 1062 | + } |
| 1063 | + } |
1016 | 1064 | let orderSql = ' iv."up.count" DESC, "replies.count" DESC, "Idea"."createdAt" DESC '; |
1017 | 1065 | if (!showModerated || showModerated == "false") { |
1018 | 1066 | where += ` AND "Idea"."deletedAt" IS NULL `; |
@@ -1124,6 +1172,9 @@ module.exports = function (app) { |
1124 | 1172 | userId: req.user?.id || req.user?.userId, |
1125 | 1173 | ideationId, |
1126 | 1174 | authorId, |
| 1175 | + ...(demographicsFilter?.age && flattenArrayValues({ demographics_age: demographicsFilter.age })), |
| 1176 | + demographics_gender: demographicsFilter?.gender, |
| 1177 | + demographics_residence: demographicsFilter?.residence, |
1127 | 1178 | status, |
1128 | 1179 | favourite, |
1129 | 1180 | folderId, |
|
0 commit comments