Changeset 4dff800 for database.js
- Timestamp:
- 02/22/26 20:32:09 (7 months ago)
- Branches:
- finki-main, main
- Children:
- 79fff4f
- Parents:
- 591278c
- File:
-
- 1 edited
-
database.js (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
database.js
r591278c r4dff800 1094 1094 // User functions for admin 1095 1095 function getAllUsers(callback) { 1096 const users = [];1096 const usersMap = new Map(); // Use Map to deduplicate by ID 1097 1097 1098 1098 // Get client users 1099 1099 database.all( 1100 `SELECT client_id as id, first_name, last_name, email, 'client' as user_type 1101 FROM client 1102 ORDER BY client_id`, 1100 `SELECT client_id as id, first_name, last_name, email, 'client' as user_type, 1101 NULL as username, NULL as role_priority 1102 FROM client 1103 ORDER BY client_id`, 1103 1104 [], 1104 1105 (err, rows) => { 1105 if (!err) { 1106 users.push(...(rows || [])); 1107 } 1108 1109 // Get personal users 1106 if (!err && rows) { 1107 rows.forEach(row => { 1108 // Clients have lowest priority (5) 1109 row.role_priority = 5; 1110 usersMap.set(row.id, row); 1111 }); 1112 } 1113 1114 // Get personal users (employees and store owners) 1110 1115 database.all( 1111 1116 `SELECT p.id, p.first_name, p.last_name, p.email, 1112 CASE WHEN b.boss_id IS NOT NULL THEN 'store_owner' 1113 WHEN e.employee_id IS NOT NULL THEN 'store_employee' 1114 ELSE 'personal' END as user_type 1115 FROM personal p 1116 LEFT JOIN boss b ON p.id = b.boss_id 1117 LEFT JOIN employees e ON p.id = e.employee_id 1118 ORDER BY p.id`, 1117 CASE 1118 WHEN b.boss_id IS NOT NULL THEN 'store_owner' 1119 ELSE 'store_employee' 1120 END as user_type, 1121 NULL as username, 1122 CASE 1123 WHEN b.boss_id IS NOT NULL THEN 2 -- store_owner priority 2 1124 ELSE 4 -- store_employee priority 4 1125 END as role_priority 1126 FROM personal p 1127 LEFT JOIN boss b ON p.id = b.boss_id 1128 LEFT JOIN employees e ON p.id = e.employee_id 1129 WHERE b.boss_id IS NOT NULL OR e.employee_id IS NOT NULL 1130 ORDER BY p.id`, 1119 1131 [], 1120 1132 (err, rows) => { 1121 if (!err) { 1122 users.push(...(rows || [])); 1133 if (!err && rows) { 1134 rows.forEach(row => { 1135 // Only add if not exists or current has higher priority (lower number) 1136 const existing = usersMap.get(row.id); 1137 if (!existing || (existing.role_priority && row.role_priority < existing.role_priority)) { 1138 usersMap.set(row.id, row); 1139 } 1140 }); 1123 1141 } 1124 1142 1125 // Get system users 1143 // Get system users (including admin) 1126 1144 database.all( 1127 `SELECT id, username, email, user_type 1128 FROM users 1129 ORDER BY id`, 1145 `SELECT id, username, email, user_type, 1146 CASE 1147 WHEN user_type = 'admin' THEN 1 -- admin highest priority 1148 ELSE 3 -- other system users priority 3 1149 END as role_priority 1150 FROM users 1151 ORDER BY id`, 1130 1152 [], 1131 1153 (err, rows) => { 1132 if (!err) { 1133 users.push(...(rows || [])); 1134 } 1154 if (!err && rows) { 1155 rows.forEach(row => { 1156 // System users have priority based on type 1157 const existing = usersMap.get(row.id); 1158 if (!existing || (existing.role_priority && row.role_priority < existing.role_priority)) { 1159 // For system users, format the response properly 1160 const userData = { 1161 id: row.id, 1162 username: row.username, 1163 email: row.email, 1164 user_type: row.user_type, 1165 role_priority: row.role_priority 1166 }; 1167 // Add first_name/last_name if not present 1168 if (row.user_type === 'admin') { 1169 userData.first_name = 'Admin'; 1170 userData.last_name = 'User'; 1171 } 1172 usersMap.set(row.id, userData); 1173 } 1174 }); 1175 } 1176 1177 // Convert Map to array and remove role_priority before sending 1178 const users = Array.from(usersMap.values()).map(user => { 1179 const { role_priority, ...userWithoutPriority } = user; 1180 return userWithoutPriority; 1181 }); 1182 1135 1183 callback(null, users); 1136 1184 }
Note:
See TracChangeset
for help on using the changeset viewer.
