
# Subscription Expiry Cron Job

This document explains the cron-based subscription expiry system.

## Overview

Instead of using a real-time delayed queue, the system uses a **scheduled cron job** to periodically check for and expire subscriptions. This is a simpler, resource-efficient approach suitable for this use case.

### Logic
1.  **Schedule**: Runs every **4 hours** (at minute 0 of the hour).
    -   Cron expression: `0 */4 * * *` (e.g., 00:00, 04:00, 08:00, 12:00, ...).
2.  **Task**:
    -   Queries the database for subscriptions where:
        -   `status` is `'Active'`
        -   `endDate` is strictly less than `now` (in the past).
    -   Updates matched subscriptions to `status: 'Expired'`.
    -   Sets `updatedAt` to the current time.

### File Structure
-   `server/src/cron/subscriptionCron.ts`: Contains the cron logic and initialization function.
-   `server/src/index.ts`: Imports and starts the cron job when the server starts.

### Resilience
-   **Server Restarts**: The cron job is re-initialized whenever the server restarts.
-   **Missed Jobs**: Since the logic checks for *any* active subscription with a past end date, it automatically catches up on any expirations that might have been missed if the server was down during a scheduled run.

### Setup
No additional processes are needed. The cron runs inside the main API server process.

```bash
npm start
```

## Monitoring
Check the server logs for the following messages:
-   `[Cron] Subscription expiry job scheduled (runs every 4 hours).` (On startup)
-   `[Cron] Running subscription expiry check at ...` (Every 4 hours)
-   `[Cron] Expired X overdue subscriptions.` (If expirations occurred)
