Commit ba7d56e
authored
feat(api): add analytics-scoped public API and clean up analytics data access (#1119)
* feat(api): add analytics-scoped public API and clean up analytics data access
Publishes all 30 internal analytics procedures under a new `analytics`
token scope at /v1/analytics/*, closing the gap where analytics tokens
could only reach error logs. Since apps/mcp-server derives its tool
list from /public-spec.json, this also exposes the endpoints as MCP
tools with no MCP-side work.
Also moves inline db.* queries out of packages/analytics services and
into their matching repositories, fixing the same layering violation
the broader data-access cleanup targets, in the domain this PR already
touches.
* fix(analytics): scope broadcast stats to workspace and close analytics public-API review gaps
The new analytics public API (#1119) made broadcastStats reachable by
API token for the first time, exposing a pre-existing IDOR:
getBatchStats/getContacts never filtered on workspaceId, so a
workspace-A token could read workspace-B's broadcast delivery/seen/
click/failure counts and contact lists via a guessable broadcastId.
Both now join Broadcast and scope on workspaceId.
resetStatsSession had a related but lower-severity issue: it inserted
a FlowAnalyticsSession row for any flowId without verifying it belongs
to the caller's workspace, letting a foreign flowId write an orphan
row into the caller's workspace on every call. It now no-ops when the
flow isn't found in-scope.
Also addresses the rest of the PR #1119 review:
- extend the public-spec workspaceId leak guard to request schemas
(parameters + body), not just responses
- replace withPublicPaging's `as never` cast with shape destructuring
so a schema missing page/perPage fails at compile time instead of
crashing every public route at module load
- de-duplicate findContactInboxesWithContact into BaseRepository
- reuse existing @chatbotx.io/analytics/schemas response schemas
instead of hand-copying four of them in schema/public.ts
- finish the db.* cleanup: add LinkStatsRepository.insertStats() and
move magic-link/ref-link click inserts off direct db.insert
- swap sql.raw for sql.identifier in sequence-stats.repository.ts
- tighten the refLinkContacts PII test to the full allow-list form
* chore: change contact update method to contact repository
* chore: fix public-spec test1 parent 870e35a commit ba7d56e
33 files changed
Lines changed: 2724 additions & 399 deletions
File tree
- apps/builder
- __tests__
- __snapshots__
- src
- features
- analytics
- api
- schema
- workspace-members/api
- lib/public-api
- routers
- docs/developer
- packages
- analytics
- __tests__
- src
- repositories/postgres
- services
- database/src/repositories/contact
Lines changed: 150 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
60 | 210 | | |
61 | 211 | | |
62 | 212 | | |
| |||
0 commit comments