A comprehensive loyalty program backend system built for Salla merchants, providing points-based customer rewards, coupon generation, analytics, and subscription management.
- Merchant Management: Automatic app installation handling via Salla webhooks
- Customer Management: Track customer points, activities, and reward redemptions
- Loyalty Program: Configurable point earning rules and reward systems
- Reward Rules: Create, edit, and manage different types of rewards
- Coupon System: Generate and redeem discount coupons
- Analytics & Reporting: Comprehensive dashboard with customer participation, points flow, and reward performance
- Subscription Management: Multi-tier subscription plans with feature access control
- JWT-based authentication with secure cookie management
- Password hashing with bcrypt
- Protected routes with middleware
- Subscription-based feature access control
- Email notifications for points earned, rewards redeemed, and birthday wishes
- Configurable notification settings per merchant
- Support for Arabic and English content
- Salla app installation/uninstallation handling
- Order creation tracking for automatic point allocation
- Customer login detection for birthday rewards
- Review and feedback processing
- Node.js (v14 or higher)
- MongoDB (v4.4 or higher)
- Salla Partner Account
- SMTP Email Service (Hostinger or SendGrid recommended)
-
Clone the repository
git clone <repository-url> cd Loyality-App-Backend
-
Install dependencies
npm install
-
Environment Configuration Create a
.envfile based on the provided.env.example:cp .env.example .env
Update the
.envfile with your configuration:# Node.js Environment NODE_ENV=development PORT=5000 # Database MONGO_URI=mongodb://localhost:27017/loyalty-app # JWT JWT_SECRET=your-super-secret-jwt-key # Salla API SALLA_CLIENT_ID=your-salla-client-id SALLA_CLIENT_SECRET=your-salla-client-secret SALLA_API_BASE_URL=https://api.salla.dev # Email Configuration APP_EMAIL_ADDRESS=noreply@yourdomain.com APP_EMAIL_PASSWORD=your-email-password SMTP_HOST=smtp.hostinger.com SMTP_PORT=587
-
Start the development server
npm run dev
http://localhost:5000/api/v1
Most endpoints require authentication via JWT token stored in HTTP-only cookies.
POST /api/v1/merchant/login
Content-Type: application/json
{
"email": "merchant@example.com",
"password": "password"
}GET /api/v1/merchant/dashboardGET /api/v1/merchant/LoyaltySettings
PUT /api/v1/merchant/LoyaltySettingsGET /api/v1/customer/POST /api/v1/customer/
Content-Type: application/json
{
"customerId": "cust_123",
"name": "John Doe",
"email": "john@example.com",
"phone": "+966501234567",
"dateOfBirth": "1990-01-15"
}POST /api/v1/customer/:id/adjust-points
Content-Type: application/json
{
"points": 100,
"type": "add",
"reason": "Manual adjustment for complaint resolution"
}GET /api/v1/reward/POST /api/v1/reward/
Content-Type: application/json
{
"description": "10 SAR discount",
"pointsRequired": 100,
"rewardType": "discountOrderPrice",
"rewardValue": 10,
"expiresAt": "2024-12-31"
}POST /api/v1/reward/apply
Content-Type: application/json
{
"customerId": "cust_123",
"rewardType": "discountOrderPrice"
}POST /api/v1/reward/redeem
Content-Type: application/json
{
"code": "DISC123456",
"orderId": "order_789"
}GET /api/v1/analytics/dashboard?startDate=2024-01-01&endDate=2024-01-31GET /api/v1/analytics/customer-participation?period=30dGET /api/v1/analytics/points?period=30dGET /api/v1/analytics/rewards?period=30dGET /api/v1/subscription/infoGET /api/v1/subscription/plansPOST /api/v1/subscription/check-feature
Content-Type: application/json
{
"feature": "analytics_advanced"
}POST /webhook
Content-Type: application/json
{
"event": "app.installed",
"data": {
// Salla webhook payload
}
}Supported webhook events:
app.installed- App installationapp.uninstalled- App uninstallationapp.store.authorize- Store authorizationorder.created- New order processingcustomer.login- Customer login (birthday check)review.added- Customer product review
- Purchase Points: Earn points based on order value
- Welcome Points: First-time customer bonus
- Birthday Points: Annual birthday rewards
- Review Points: Points for product reviews
- Referral Points: Sharing and referral bonuses
- Threshold Points: Bonus for spending above certain amounts
- Discount on Order Price: Fixed amount discount (e.g., 10 SAR off)
- Percentage Discount: Percentage-based discount (e.g., 10% off)
- Free Shipping: Shipping cost discount
- Cashback: Money back to customer
- Free Product: Complimentary product
- Points earned notifications
- Coupon generation alerts
- Birthday wishes
- Referral bonus notifications
- Up to 100 customers
- 1 reward type
- Basic points tracking
- 50 coupons per month
- Up to 1,000 customers
- 5 reward types
- Email notifications
- 500 coupons per month
- Basic analytics (90 days)
- Unlimited customers
- Unlimited rewards
- SMS notifications
- Unlimited coupons
- Advanced analytics (1 year)
- Custom branding
- API access
protect: Verifies JWT token and loads merchant data
requireSubscription(features): Checks feature access based on subscriptioncheckUsageLimits(limitType): Enforces usage limits per planrefreshSubscriptionData: Updates subscription data from Salla
Loyality-App-Backend/
βββ config/
β βββ crypto.js # Password generation utilities
β βββ db.js # MongoDB connection
βββ controllers/
β βββ analytics.controller.js
β βββ customer.controller.js
β βββ merchant.controller.js
β βββ notification.controller.js
β βββ redeemCoupon.controller.js
β βββ reward.controller.js
β βββ subscription.controller.js
β βββ webhook.controller.js
βββ middlewares/
β βββ protect.js # Authentication middleware
β βββ subscription.js # Subscription management
βββ models/
β βββ coupon.model.js
β βββ customer.model.js
β βββ customerLoyalityActivitySchema.model.js
β βββ merchant.model.js
β βββ reward.model.js
β βββ transaction.model.js
βββ routes/
β βββ analytics.route.js
β βββ customer.route.js
β βββ merchant.route.js
β βββ reward.route.js
β βββ subscription.route.js
β βββ webhook.route.js
βββ services/
β βββ loyalityEngine.js # Core loyalty logic
β βββ getCustomers.js
β βββ getOrders.js
β βββ refreshAccessToken.js
βββ utils/
β βββ generateCouponCode.js
β βββ generateToken.js
β βββ sendEmail.js
β βββ templates/
βββ server.js
-
Server Configuration
# Install PM2 for process management npm install -g pm2 # Start application pm2 start server.js --name loyalty-app
-
Nginx Configuration
server { listen 80; server_name yourdomain.com; location / { proxy_pass http://localhost:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }
-
SSL Setup with Let's Encrypt
sudo certbot --nginx -d yourdomain.com
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the ISC License.
For support and questions:
- Email: support@yourdomain.com
- Documentation: API Docs
- Issues: GitHub Issues
Built with β€οΈ for Salla merchants