Maintenance Endpoints Index

Maintenance Endpoints

GET /api/maintenance/database-stats

Get database statistics including user, subscription, promotion, and coupon counts (requires administrator access)

Headers:

Authorization: Bearer ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr...

Response (200 OK):

{
    "users": {
        "total": 1523,
        "active": 1450,
        "subscribers": 890,
        "nonSubscribers": 560,
        "admins": 3
    },
    "subscriptions": {
        "total": 920,
        "active": 890,
        "cancelled": 25,
        "pastDue": 5
    },
    "promotions": {
        "total": 45,
        "active": 32,
        "totalRedemptions": 478
    },
    "coupons": {
        "total": 12,
        "active": 8
    },
    "timestamp": "2024-01-15T10:30:00.000Z"
}

Error Responses:

{
    "message": "Administrator access required"
}

Notes:

  • Requires authentication token in the Authorization header
  • Only administrators (ADMI user type) can access this endpoint
  • All counts are retrieved directly from the database
  • User Statistics:
    • total - Total number of users in the system
    • active - Number of users with is_active = true
    • subscribers - Number of users with user_type_code = 'SUBS'
    • nonSubscribers - Number of users with user_type_code = 'NONS'
    • admins - Number of users with user_type_code = 'ADMI'
  • Subscription Statistics:
    • total - Total number of subscriptions
    • active - Number of subscriptions with status = 'active'
    • cancelled - Number of subscriptions with status = 'canceled'
    • pastDue - Number of subscriptions with status = 'past_due'
  • Promotion Statistics:
    • total - Total number of promotions
    • active - Number of promotions with is_active = true
    • totalRedemptions - Sum of all redemption_count values across all promotions
  • Coupon Statistics:
    • total - Total number of coupons
    • active - Number of coupons with is_active = true
  • This endpoint provides a quick overview of the system's health and usage
  • Useful for monitoring system growth and detecting potential issues
GET /api/maintenance/system-info

Get system information including database and server stats (requires administrator access)

Headers:

Authorization: Bearer ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr...

Response (200 OK):

{
    "database": {
        "version": "1.11",
        "connectionStatus": "connected",
        "poolSize": 20,
        "activeConnections": 5
    },
    "server": {
        "nodeVersion": "v18.17.0",
        "uptime": 86400,
        "memoryUsage": {
            "heapUsed": 45,
            "heapTotal": 60,
            "external": 2,
            "rss": 120
        }
    },
    "timestamp": "2024-01-15T10:30:00.000Z"
}

Error Responses:

{
    "message": "Administrator access required"
}

Notes:

  • Requires authentication token in the Authorization header
  • Only administrators (ADMI user type) can access this endpoint
  • Database version is retrieved from the database_version table
  • Memory usage values are in megabytes (MB)
  • Server uptime is in seconds
  • Pool size shows the total number of database connections available
  • Active connections shows how many connections are currently in use
POST /api/maintenance/activate-subscriptions

Activate subscriptions for NONS users who don't have NONS promotions applied (requires administrator access and CSRF token)

Headers:

Authorization: Bearer ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr...
x-csrf-token: a8d7f9c6e5b4a3c2d1e0f9a8d7f6c5b4a3

Request Body:

{
    "dryRun": true,  // Optional: Set to true to preview changes without applying them (default: false)
    "onlyWithSubscriptions": true  // Optional: If true, only activate users with active subscriptions; if false (default), activate all NONS users without NONS promotions
}

Response (200 OK) - Dry Run (All NONS Users):

{
    "message": "Dry run completed successfully",
    "dryRun": true,
    "usersProcessed": 50,
    "usersActivated": 48,
    "usersSkipped": 0,
    "errors": [],
    "details": [
        {
            "userId": 15,
            "userEmail": "john@example.com",
            "action": "activated"
        },
        {
            "userId": 23,
            "userEmail": "jane@example.com",
            "action": "activated"
        }
    ]
}

Response (200 OK) - Dry Run (Only Users With Subscriptions):

{
    "message": "Dry run completed successfully",
    "dryRun": true,
    "usersProcessed": 25,
    "usersActivated": 23,
    "usersSkipped": 0,
    "errors": [],
    "details": [
        {
            "userId": 15,
            "userEmail": "john@example.com",
            "action": "activated"
        },
        {
            "userId": 23,
            "userEmail": "jane@example.com",
            "action": "activated"
        }
    ]
}

Response (200 OK) - Actual Activation:

{
    "message": "Subscription activation completed",
    "dryRun": false,
    "usersProcessed": 25,
    "usersActivated": 23,
    "usersSkipped": 0,
    "errors": [
        "User 42 (user@example.com): Failed to activate: Database error"
    ],
    "details": [
        {
            "userId": 15,
            "userEmail": "john@example.com",
            "action": "activated"
        },
        {
            "userId": 23,
            "userEmail": "jane@example.com",
            "action": "activated"
        }
    ]
}

Error Responses:

{
    "message": "Administrator access required"
}

Notes:

POST /api/maintenance/deactivate-subscriptions

Deactivate subscriptions for SUBS users who don't have NONS promotions applied (requires administrator access and CSRF token)

Headers:

Authorization: Bearer ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr...
x-csrf-token: a8d7f9c6e5b4a3c2d1e0f9a8d7f6c5b4a3

Request Body:

{
    "dryRun": true,  // Optional: Set to true to preview changes without applying them (default: false)
    "onlyWithSubscriptions": true  // Optional: If true, only deactivate users with active subscriptions; if false (default), deactivate all SUBS users without NONS promotions
}

Response (200 OK) - Dry Run (All SUBS Users):

{
    "message": "Dry run completed successfully",
    "dryRun": true,
    "usersProcessed": 35,
    "usersDeactivated": 33,
    "usersSkipped": 0,
    "errors": [],
    "details": [
        {
            "userId": 31,
            "userEmail": "bob@example.com",
            "action": "deactivated"
        },
        {
            "userId": 45,
            "userEmail": "alice@example.com",
            "action": "deactivated"
        }
    ]
}

Response (200 OK) - Dry Run (Only Users With Subscriptions):

{
    "message": "Dry run completed successfully",
    "dryRun": true,
    "usersProcessed": 18,
    "usersDeactivated": 17,
    "usersSkipped": 0,
    "errors": [],
    "details": [
        {
            "userId": 31,
            "userEmail": "bob@example.com",
            "action": "deactivated"
        },
        {
            "userId": 45,
            "userEmail": "alice@example.com",
            "action": "deactivated"
        }
    ]
}

Response (200 OK) - Actual Deactivation:

{
    "message": "Subscription deactivation completed",
    "dryRun": false,
    "usersProcessed": 18,
    "usersDeactivated": 17,
    "usersSkipped": 0,
    "errors": [
        "User 56 (user@example.com): Failed to deactivate: Database error"
    ],
    "details": [
        {
            "userId": 31,
            "userEmail": "bob@example.com",
            "action": "deactivated"
        },
        {
            "userId": 45,
            "userEmail": "alice@example.com",
            "action": "deactivated"
        }
    ]
}

Error Responses:

{
    "message": "Administrator access required"
}

Notes:

POST /api/maintenance/migrate-legacy-user-groups

Migrate legacy user groups to coupons and promotions (requires administrator access and CSRF token)

Headers:

Authorization: Bearer ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr...
x-csrf-token: a8d7f9c6e5b4a3c2d1e0f9a8d7f6c5b4a3

Request Body:

{
    "dryRun": true
}

Response (200 OK) - Dry Run:

{
    "message": "Dry run completed successfully",
    "dryRun": true,
    "couponsCreated": 0,
    "promotionsCreated": 10,
    "usersProcessed": 0,
    "errors": [],
    "summary": {
        "totalGroups": 10,
        "totalUsers": 1462,
        "nonsCouponCount": 4,
        "discountCouponCount": 3,
        "uniqueDiscountPercentages": [25, 50, 75]
    }
}

Response (200 OK) - Actual Migration:

{
    "message": "Migration completed successfully",
    "dryRun": false,
    "couponsCreated": 3,
    "promotionsCreated": 10,
    "usersProcessed": 1462,
    "errors": [],
    "summary": {
        "totalGroups": 10,
        "totalUsers": 1462,
        "nonsCouponCount": 4,
        "discountCouponCount": 3,
        "uniqueDiscountPercentages": [25, 50, 75]
    }
}

Error Responses:

{
    "message": "Administrator access required"
}

Notes: