A web application that tracks MOT tests for UK vehicles and sends notifications when new MOT tests are recorded. The application leverages MongoDB for server-side monitoring and browser notifications for real-time alerts.
- Server-Side Monitoring: Checks for MOT updates hourly in the background
- Real-Time Notifications: Receive notifications when new MOT tests are recorded
- Client-Side Polling: Quickly see updates while the browser tab is open
- Mobile-Friendly Interface: Works well on desktop and mobile devices
This application uses a combination of server and client-side technologies to provide real-time MOT test notifications:
- scheduled-mot-check.js: Runs hourly to check for MOT updates for all monitored vehicles
- MongoDB Database: Stores vehicle information, notification preferences, and MOT history
- REST API Endpoints: Allow the client to retrieve data and manage notification preferences
- Real-Time Polling: Checks for updates every 10-300 seconds (configurable by user)
- Browser Notifications: Shows alerts when new MOT tests are detected
- Service Worker: Enables background notifications even when the browser is closed
- scheduled-mot-check.js: The heart of the system - checks MOT API for updates
- getPendingNotifications.js: Allows the client to retrieve pending notifications
- App.jsx: Main React component for the user interface
- notificationService.js: Handles browser notifications and update polling
The following environment variables must be set in your Netlify site settings:
Backend Configuration:
MONGODB_URI: MongoDB connection stringMONGODB_DB_NAME: Database nameTOKEN_URL: MOT API token URLCLIENT_ID: MOT API client IDCLIENT_SECRET: MOT API client secretAPI_KEY: MOT API keySCOPE: MOT API scope
Push Notification Configuration:
VAPID_PUBLIC_KEY: VAPID public key for push notificationsVAPID_PRIVATE_KEY: VAPID private key for push notificationsVAPID_MAILTO: Contact email for VAPID configuration
Frontend Configuration:
REACT_APP_VAPID_PUBLIC_KEY: VAPID public key for client-side use
-
Monitoring Setup:
- User enters a vehicle registration and enables notifications
- The registration is saved to MongoDB with monitoring enabled
-
Server-Side Checking:
- Every hour, the scheduled function queries the MOT API for all monitored vehicles
- If a new MOT test is detected, it marks the vehicle for notification
- Details about the new test are stored in MongoDB
-
Client-Side Polling:
- When the app is open, it polls for updates every 10-300 seconds
- The client only asks "are there updates?" rather than directly querying the MOT API
- This is much more efficient and reduces API usage
-
Notification Delivery:
- When a new test is detected, a browser notification is shown
- The notification includes details about the test result
- Clicking the notification takes the user to the app to see full details
- Batched Processing: The server processes vehicles in small batches to avoid API rate limits
- Centralized Token Management: OAuth tokens are cached with race condition protection
- Optimized Index Creation: Database indexes are only created when needed
- Efficient Polling: The client only checks for updates rather than fetching full data
- No-Cache Headers: All API responses include headers to prevent caching of outdated data
- Connection Pooling: MongoDB connections are cached and reused across function invocations
If notifications are not working properly:
- Make sure notifications are enabled in your browser settings
- Check that MongoDB is properly connected
- Verify that the scheduled function is running (check Netlify function logs)
- Confirm that the MOT API credentials are valid
- Ensure all required environment variables are set (see list above)
- Check that VAPID keys are properly configured for push notifications
- Fixed notification system: The
hasUpdateflag was not being set properly, causing notifications to fail completely - Status: All notifications should now work correctly
- Added authentication to
renewToken.jsendpoint - Removed hardcoded VAPID keys from client-side code
- Enhanced input validation across all functions
- Centralized token management with race condition protection
- Optimized database index creation
- Standardized API timeouts (10 seconds)
- Created shared token management utility
- Standardized error response formats
- Improved environment variable validation
The MongoDB database contains a 'notifications' collection with documents in this format:
{
"registration": "AB12CDE",
"enabled": true,
"lastCheckedDate": "2023-12-31T12:00:00.000Z",
"lastMotTestDate": "2023-06-15T10:30:00.000Z",
"hasUpdate": false,
"createdAt": "2023-01-01T00:00:00.000Z",
"updateDetails": {
"previousDate": "2023-06-15T10:30:00.000Z",
"newDate": "2023-12-30T14:20:00.000Z",
"testResult": "PASSED",
"vehicle": {
"make": "Ford",
"model": "Focus",
"registration": "AB12CDE",
"color": "Blue"
}
}
}To set up for local development:
- Clone the repository
- Run
npm install - Create a
.envfile with the required environment variables - Run
npm run devto start the Netlify dev server
The application is designed to be deployed on Netlify with the following features:
- Netlify Functions
- Scheduled Functions (requires Netlify Pro or team plan)
- Environment variables
Make sure to set up all required environment variables in your Netlify site settings.