4.4 KiB
4.4 KiB
EMI Backend Agent Notes
What this service is
- Node.js + Express API for EMI social features (profiles, posts, groups/courses, songs, payments, Bible/subsplash integrations).
- Main entrypoint:
index.js. - MongoDB Atlas-backed via
MONGO_URLusingmongodb@3.6.x.
Runbook
- Install:
npm install - Start:
npm start(binds toPORT, default3000) - Test:
npm test(single auth test file) - API docs:
GET /api-docs
High-level architecture
index.js: middleware setup, auth routes, route mounting, Swagger, web-push setup.mongoDB.js: creates shared DB object + collections + utility methods, then extends with:dbTools/profile.jsdbTools/post.jsdbTools/payments.jsdbTools/songs.js
middleware/sessionChecker.js: cookie/session validation and profile context hydration.routes/*.js: feature-specific routers.def/*.js: lightweight constructors forProfile,Post,Songs.
Auth + session model
- Cookies used:
user_sidsession_idprofile_id
sessionCheckerverifies ObjectId format, then checks session intokenscollection.- On missing/invalid session/profile, user is redirected to
/login. - Most app routes are protected with
sessionCheckerexcept:/signup,/login,/logout,/resetPassword/payments/*/subsplash/*/invite/:email
Key route surfaces
routes/profile.js:- Profile CRUD, invites, follow/unfollow, group/course discovery, subscribe/approve/reject flows.
routes/post.js:- Feed endpoints, tags/media filters, create/edit/delete posts, reactions/comments/bookmarks.
- Merges organic + non-organic posts (news/popular recommendations).
routes/payments.js:- Stripe payment intent creation + result registration; can toggle subscription timestamp.
routes/songs.js:- Song CRUD (ownership checks are effectively placeholder).
routes/bible.js:- Proxies scripture.api.bible endpoints using hardcoded API key in source.
routes/subsplash.js:- Scrapes Subsplash HTML with cheerio for events/media.
Data model (collections)
users: auth identity + password hash + optional customer.tokens: session documents (uidpoints to user).invitation: invite gating for signup.profiles: user/group/course/chat profile documents.posts: feed posts, reactions, comments, bookmarks, tags, non-organic type.payments: intent and payment result records.songs: song content metadata and reactions/comments.
Important operational dependencies
- Mongo connection is required before server starts listening (
index.jswaits forDB.getDB). - Notifications:
- Email via
nodemailerSMTP (mail.emmint.com, envEMAILPASS). - Mobile push via Expo (
expo-server-sdk). - Web push VAPID keys (
PUBLIC_VAPID_KEY,PRIVATE_VAPID_KEY,WEB_PUSH_EMAIL).
- Email via
- Analytics via PostHog (
POSTHOG_API_KEY). - Stripe via
STRIPE.
Environment/cookie/cors behavior
- Cookies configured in
config/cookiesOptions.js:- production or
COOKIE_SECURE=true=>secure: true,sameSite: none - local HTTP =>
secure: false,sameSite: lax
- production or
- Allowed CORS origins in
config/corsOptions.jsare explicit list-based.
Known code risks and maintenance hotspots
- Mixed ESM/CommonJS utility scripts (
AITools.jsuses ESM style while app is CommonJS). routes/bible.jshas duplicate/booksroute and a probable bug in/books/:bookId(bibleIdreference).- Hardcoded external API key in
routes/bible.jsshould be moved to env. routes/songs.jssongBelongsToUseralways returns true (authorization gap).- Some endpoints return redirect-to-login for API callers instead of structured 401 JSON.
- Inconsistent error handling/response shapes across routes.
- Legacy driver/runtime tension:
- Dependency is
mongodb@3.6.x Dockerfileuses Node 22, but code warns Node 22 is not fully tested; Node 20 LTS is safer.
- Dependency is
Testing state
- Only
test/auth.test.jsexists; no broad coverage for routes/db tools. - Auth test expects existing seeded user behavior, so reliability depends on DB fixture state.
Suggested workflow for future changes
- Keep fixes scoped and defensive (null checks + stable JSON).
- For auth/session changes:
- update both
sessionCheckerandutils/sessionUtils.js.
- update both
- For profile/post behavior:
- confirm DB helper method side effects in
dbTools/*.
- confirm DB helper method side effects in
- For production incidents:
- first validate
MONGO_URLconnectivity and cookie security mode alignment.
- first validate