"use strict";
// oms/src/services/dailyActivityDetector.ts
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = run;
const date_fns_1 = require("date-fns");
const messageActivityModel_1 = require("../../src/models/messageActivityModel");
const rabbitmqService_1 = __importDefault(require("../../src/services/rabbitmqService"));
const QUEUE_NAME = "message-record-generation-queue";
/**
 * 核心检测函数，由外部的 node-cron 任务调用
 * 连接数据库，查找所有已发布的每日活动，并判断是否需要触发消息生成
 */
async function run() {
    console.log("--- Starting daily message activity check ---");
    try {
        // 2. 查找所有符合条件的活动：已发布且设置为每日推送
        const activities = await messageActivityModel_1.MessageActivity.find({
            status: 1, // 1 表示已发布 (published)
            everyday: true,
        });
        if (activities.length === 0) {
            console.log("No daily published activities found.");
        }
        // 3. 遍历活动，检查是否需要推送
        for (const activity of activities) {
            console.log(`Checking activity: ${activity.name} (ID: ${activity._id})`);
            const lastPubDate = activity.lastPubDate;
            const needsNewPush = !lastPubDate || !(0, date_fns_1.isToday)(lastPubDate);
            if (needsNewPush) {
                console.log(`-> Activity requires a new push. Last publish date: ${lastPubDate ? lastPubDate.toISOString() : "None"}.`);
                // 向rabbitmq投递一条消息， 以便异步生成消息推送记录
                await rabbitmqService_1.default.publishActivityMessage(QUEUE_NAME, { activityId: activity._id });
            }
            else {
                console.log("-> Activity has already run today. Skipping.");
            }
        }
        console.log("--- Daily message activity check finished ---");
    }
    catch (error) {
        if (error instanceof Error) {
            console.error("An error occurred during the daily check:", error.message);
        }
        else {
            console.error("An unknown error occurred during the daily check.");
        }
    }
    finally {
    }
}
// 启动时立即运行一次，以便在部署后立即处理积压的消息
run();
