|
| 1 | +const parsePositiveInt = (value) => { |
| 2 | + if (value === undefined || value === null || value === "") return null; |
| 3 | + const num = Number(value); |
| 4 | + if (!Number.isFinite(num) || num < 0) return null; |
| 5 | + return Math.floor(num); |
| 6 | +}; |
| 7 | + |
| 8 | +const escapeRegExp = (value) => |
| 9 | + value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 10 | + |
| 11 | +const getPagination = (req) => { |
| 12 | + const start = parsePositiveInt(req.query._start); |
| 13 | + const limit = parsePositiveInt(req.query._limit); |
| 14 | + |
| 15 | + if (start !== null || limit !== null) { |
| 16 | + return { skip: start || 0, limit: limit || 0 }; |
| 17 | + } |
| 18 | + |
| 19 | + const page = parsePositiveInt(req.query.page); |
| 20 | + const pageLimit = parsePositiveInt(req.query.limit); |
| 21 | + if (pageLimit !== null) { |
| 22 | + const safePage = page && page > 0 ? page : 1; |
| 23 | + return { skip: (safePage - 1) * pageLimit, limit: pageLimit }; |
| 24 | + } |
| 25 | + |
| 26 | + return { skip: 0, limit: 0 }; |
| 27 | +}; |
| 28 | + |
| 29 | +const parseCommaList = (value) => |
| 30 | + value |
| 31 | + .split(",") |
| 32 | + .map((item) => item.trim()) |
| 33 | + .filter(Boolean); |
| 34 | + |
| 35 | +const buildFilters = (query, allowedFields = []) => { |
| 36 | + const filters = {}; |
| 37 | + for (const field of allowedFields) { |
| 38 | + const raw = query[field]; |
| 39 | + if (raw === undefined) continue; |
| 40 | + if (typeof raw === "string" && raw.includes(",")) { |
| 41 | + filters[field] = { $in: parseCommaList(raw) }; |
| 42 | + } else { |
| 43 | + filters[field] = raw; |
| 44 | + } |
| 45 | + } |
| 46 | + return filters; |
| 47 | +}; |
| 48 | + |
| 49 | +const buildSearchFilter = (searchTerm, searchFields = []) => { |
| 50 | + if (!searchTerm || !searchFields.length) return null; |
| 51 | + const regex = new RegExp(escapeRegExp(searchTerm), "i"); |
| 52 | + return { |
| 53 | + $or: searchFields.map((field) => ({ [field]: regex })), |
| 54 | + }; |
| 55 | +}; |
| 56 | + |
| 57 | +const applyPopulate = (query, req, allowedPopulate = [], defaultPopulate = []) => { |
| 58 | + const raw = req.query.populate; |
| 59 | + const requested = typeof raw === "string" ? parseCommaList(raw) : []; |
| 60 | + const populateFields = new Set([ |
| 61 | + ...defaultPopulate, |
| 62 | + ...requested.filter((path) => allowedPopulate.includes(path)), |
| 63 | + ]); |
| 64 | + for (const path of populateFields) { |
| 65 | + query = query.populate(path); |
| 66 | + } |
| 67 | + return query; |
| 68 | +}; |
| 69 | + |
| 70 | +const pickAllowed = (payload, allowedFields) => { |
| 71 | + if (!Array.isArray(allowedFields) || !allowedFields.length) { |
| 72 | + return { ...payload }; |
| 73 | + } |
| 74 | + const picked = {}; |
| 75 | + for (const field of allowedFields) { |
| 76 | + if (payload[field] !== undefined) { |
| 77 | + picked[field] = payload[field]; |
| 78 | + } |
| 79 | + } |
| 80 | + return picked; |
| 81 | +}; |
| 82 | + |
| 83 | +const isAdmin = (req) => req.user && req.user.role === "admin"; |
| 84 | + |
| 85 | +const ensureOwner = (doc, req, ownerField) => { |
| 86 | + if (!ownerField || isAdmin(req)) return true; |
| 87 | + const ownerId = doc?.[ownerField]?.toString(); |
| 88 | + const userId = req.user?._id?.toString(); |
| 89 | + return ownerId && userId && ownerId === userId; |
| 90 | +}; |
| 91 | + |
| 92 | +const assignOwner = (payload, req, ownerField, allowAdminOverride = true) => { |
| 93 | + if (!ownerField || !req.user) return payload; |
| 94 | + if (!isAdmin(req) || !allowAdminOverride) { |
| 95 | + return { ...payload, [ownerField]: req.user._id }; |
| 96 | + } |
| 97 | + if (!payload[ownerField]) { |
| 98 | + return { ...payload, [ownerField]: req.user._id }; |
| 99 | + } |
| 100 | + return payload; |
| 101 | +}; |
| 102 | + |
| 103 | +const sanitizePayload = (payload, req, ownerField, allowedFields) => { |
| 104 | + const cleaned = pickAllowed(payload, allowedFields); |
| 105 | + if (ownerField && !isAdmin(req)) { |
| 106 | + delete cleaned[ownerField]; |
| 107 | + } |
| 108 | + return cleaned; |
| 109 | +}; |
| 110 | + |
| 111 | +export const buildCrudController = ( |
| 112 | + Model, |
| 113 | + { |
| 114 | + resourceName = "Resource", |
| 115 | + searchFields = [], |
| 116 | + filterFields = [], |
| 117 | + defaultSort = "-createdAt", |
| 118 | + allowedPopulate = [], |
| 119 | + defaultPopulate = [], |
| 120 | + ownerField = null, |
| 121 | + assignOwnerOnCreate = false, |
| 122 | + allowAdminOverride = true, |
| 123 | + allowedFields = null, |
| 124 | + scopeToUser = false, |
| 125 | + } = {} |
| 126 | +) => { |
| 127 | + const list = async (req, res, next) => { |
| 128 | + try { |
| 129 | + const { skip, limit } = getPagination(req); |
| 130 | + const filters = buildFilters(req.query, filterFields); |
| 131 | + if (scopeToUser && ownerField && req.user && !isAdmin(req)) { |
| 132 | + filters[ownerField] = req.user._id; |
| 133 | + } |
| 134 | + |
| 135 | + const searchTerm = (req.query.q || req.query.search || "").trim(); |
| 136 | + const searchFilter = buildSearchFilter(searchTerm, searchFields); |
| 137 | + |
| 138 | + let query = Model.find(filters); |
| 139 | + if (searchFilter) { |
| 140 | + query = query.find(searchFilter); |
| 141 | + } |
| 142 | + |
| 143 | + if (skip) query = query.skip(skip); |
| 144 | + if (limit) query = query.limit(limit); |
| 145 | + query = query.sort(req.query.sort || defaultSort); |
| 146 | + query = applyPopulate(query, req, allowedPopulate, defaultPopulate); |
| 147 | + |
| 148 | + const docs = await query.exec(); |
| 149 | + res.json(docs); |
| 150 | + } catch (error) { |
| 151 | + next(error); |
| 152 | + } |
| 153 | + }; |
| 154 | + |
| 155 | + const getById = async (req, res, next) => { |
| 156 | + try { |
| 157 | + let query = Model.findById(req.params.id); |
| 158 | + query = applyPopulate(query, req, allowedPopulate, defaultPopulate); |
| 159 | + const doc = await query.exec(); |
| 160 | + if (!doc) { |
| 161 | + res.status(404); |
| 162 | + throw new Error(`${resourceName} not found`); |
| 163 | + } |
| 164 | + res.json(doc); |
| 165 | + } catch (error) { |
| 166 | + next(error); |
| 167 | + } |
| 168 | + }; |
| 169 | + |
| 170 | + const create = async (req, res, next) => { |
| 171 | + try { |
| 172 | + let payload = sanitizePayload(req.body || {}, req, ownerField, allowedFields); |
| 173 | + if (assignOwnerOnCreate) { |
| 174 | + payload = assignOwner(payload, req, ownerField, allowAdminOverride); |
| 175 | + } |
| 176 | + const created = await Model.create(payload); |
| 177 | + res.status(201).json(created); |
| 178 | + } catch (error) { |
| 179 | + next(error); |
| 180 | + } |
| 181 | + }; |
| 182 | + |
| 183 | + const update = async (req, res, next) => { |
| 184 | + try { |
| 185 | + const doc = await Model.findById(req.params.id); |
| 186 | + if (!doc) { |
| 187 | + res.status(404); |
| 188 | + throw new Error(`${resourceName} not found`); |
| 189 | + } |
| 190 | + if (!ensureOwner(doc, req, ownerField)) { |
| 191 | + res.status(403); |
| 192 | + throw new Error("Not authorized to update this resource"); |
| 193 | + } |
| 194 | + const updates = sanitizePayload(req.body || {}, req, ownerField, allowedFields); |
| 195 | + Object.assign(doc, updates); |
| 196 | + const updated = await doc.save(); |
| 197 | + res.json(updated); |
| 198 | + } catch (error) { |
| 199 | + next(error); |
| 200 | + } |
| 201 | + }; |
| 202 | + |
| 203 | + const remove = async (req, res, next) => { |
| 204 | + try { |
| 205 | + const doc = await Model.findById(req.params.id); |
| 206 | + if (!doc) { |
| 207 | + res.status(404); |
| 208 | + throw new Error(`${resourceName} not found`); |
| 209 | + } |
| 210 | + if (!ensureOwner(doc, req, ownerField)) { |
| 211 | + res.status(403); |
| 212 | + throw new Error("Not authorized to delete this resource"); |
| 213 | + } |
| 214 | + await doc.deleteOne(); |
| 215 | + res.json({ message: `${resourceName} deleted`, id: req.params.id }); |
| 216 | + } catch (error) { |
| 217 | + next(error); |
| 218 | + } |
| 219 | + }; |
| 220 | + |
| 221 | + return { list, getById, create, update, remove }; |
| 222 | +}; |
| 223 | + |
| 224 | +export { |
| 225 | + applyPopulate, |
| 226 | + buildFilters, |
| 227 | + buildSearchFilter, |
| 228 | + escapeRegExp, |
| 229 | + getPagination, |
| 230 | + parsePositiveInt, |
| 231 | +}; |
0 commit comments