(request, response)
| 11 | } |
| 12 | |
| 13 | async function TussSearchAdvanced(request, response) { |
| 14 | const { q, name, tuss, match, sort, order, limit, offset, fields } = |
| 15 | request.query; |
| 16 | |
| 17 | let data = searchTussAdvanced({ q, name, tuss, match, sort, order }); |
| 18 | |
| 19 | // fields projection (comma-separated) |
| 20 | const fieldList = |
| 21 | typeof fields === 'string' && fields.trim().length > 0 |
| 22 | ? fields |
| 23 | .split(',') |
| 24 | .map((f) => f.trim()) |
| 25 | .filter(Boolean) |
| 26 | : null; |
| 27 | |
| 28 | if (fieldList) { |
| 29 | data = data.map((item) => project(item, fieldList)); |
| 30 | } |
| 31 | |
| 32 | // Pagination support |
| 33 | let limitNum; |
| 34 | let offsetNum = 0; |
| 35 | if (typeof limit === 'string') { |
| 36 | const parsed = Number.parseInt(limit, 10); |
| 37 | if (!Number.isNaN(parsed) && parsed > 0) { |
| 38 | limitNum = parsed; |
| 39 | } |
| 40 | } |
| 41 | if (typeof offset === 'string') { |
| 42 | const parsed = Number.parseInt(offset, 10); |
| 43 | if (!Number.isNaN(parsed) && parsed >= 0) { |
| 44 | offsetNum = parsed; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | const total = Array.isArray(data) ? data.length : 0; |
| 49 | let items = data; |
| 50 | if (typeof limitNum === 'number') { |
| 51 | items = items.slice(offsetNum, offsetNum + limitNum); |
| 52 | } else if (offsetNum > 0) { |
| 53 | items = items.slice(offsetNum); |
| 54 | } |
| 55 | |
| 56 | return response.status(200).json({ |
| 57 | total, |
| 58 | limit: typeof limitNum === 'number' ? limitNum : null, |
| 59 | offset: offsetNum, |
| 60 | items, |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | export default app().get(TussSearchAdvanced); |
nothing calls this directly
no test coverage detected