Health Trend Seller Project Structure Health Trend Seller Project Structure
Navigation Links
Project Tree
health-trend-seller/
├─ src/
│ ├─ index.js
│ ├─ config/env.js
│ ├─ api/
│ │ ├─ routes/
│ │ │ ├─ trends.js
│ │ │ ├─ contacts.js
│ │ │ ├─ catalog.js
│ │ │ ├─ orders.js
│ │ │ └─ crm.js # CRM route that can call mapToCRM
│ │ └─ server.js
│ ├─ services/
│ │ ├─ ingest/
│ │ │ ├─ facebook.js
│ │ │ ├─ twitter.js
│ │ │ ├─ instagram.js
│ │ │ └─ linkedin.js
│ │ ├─ scoring/engine.js
│ │ ├─ crm/
│ │ │ ├─ hubspot.js
│ │ │ └─ mapToCRM.js # π your new CRM mapping service
│ │ ├─ email/sendgrid.js
│ │ ├─ sms/twilio.js
│ │ ├─ payments/stripe.js
│ │ └─ receipts/pdf.js
│ ├─ db/prisma.js
│ ├─ queue/worker.js
│ └─ utils/validators.js
├─ prisma/
│ ├─ schema.prisma
│ └─ seed.js
├─ scripts/
│ ├─ seed-catalog.js
│ └─ rotate-keys.js
├─ .env.example
├─ package.json
└─ README.md
Routes Call Breakdown
Business Logic & Third-Party Integrations
| File Location | When to Call | Reason/Purpose |
| services/crm/mapToCRM.js | After a contact is scored or updated | Transform internal data into CRM schema |
| services/crm/hubspot.js | On "Sync" or "Save" | Perform API handshake with HubSpot |
| services/scoring/engine.js | When new trend data arrives via trends.js | Calculate health trend score |
| services/payments/stripe.js | At checkout endpoint in orders.js | Process transactions |
| services/email/sendgrid.js | After successful order or submission | Send confirmation emails |
| services/receipts/pdf.js | When order finalized or requested | Generate PDF invoice |
Data Persistence
| File Location | When to Call | Reason/Purpose |
| db/prisma.js | Almost every route | CRUD operations |
Cross-Cutting Concerns
| File Location | When to Call | Reason/Purpose |
| utils/validators.js | At start of POST/PUT/PATCH | Validate request body |
| config/env.js | Pre-loaded | Provide API keys securely |
Background Processing
| File Location | When to Call | Reason/Purpose |
| queue/worker.js | On bulk requests | Offload heavy tasks |
CRM Route Example
The crm.js route fetches contacts from db/prisma.js, maps them with mapToCRM.js, and syncs to HubSpot via hubspot.js.
const express = require('express');
const router = express.Router();
const { mapLeadToHubSpot } = require('../../services/crm/mapToCRM');
const { syncToHubSpot } = require('../../services/crm/hubspot.js');
const prisma = require('../../db/prisma');
router.post('/sync/:contactId', async (req, res) => {
try {
const { contactId } = req.params;
const contact = await prisma.contact.findUnique({
where: { id: contactId },
include: { trendScores: true }
});
if (!contact) return res.status(404).json({ error: 'Contact not found' });
const hubspotPayload = mapLeadToHubSpot(contact);
const result = await syncToHubSpot(hubspotPayload);
res.status(200).json({ message: 'Sync successful', hubspotId: result.id });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
module.exports = router;
Summary of the Request Flow
Additional Layers
- Ingestion Layer:
trends.js calls facebook.js, instagram.js, etc. to pull raw mentions.
- Communication Layer:
contacts.js and orders.js trigger twilio.js or sendgrid.js for notifications.
- Validation Utility:
utils/validators.js ensures clean data before hitting the DB.
Implementation Notes
- Define Prisma schema for contacts and trends (
prisma/schema.prisma).
- Generate logic for scoring engine (
services/scoring/engine.js).
- Create validation schemas for routes (
utils/validators.js).
No comments:
Post a Comment