"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "FormBuilderChatCleaner", {
    enumerable: true,
    get: function() {
        return FormBuilderChatCleaner;
    }
});
const _config = /*#__PURE__*/ _interop_require_default(require("config"));
const _sequelize = require("sequelize");
const _tasks = require("@tamanu/shared/tasks");
const _logging = require("@tamanu/shared/services/logging");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
let FormBuilderChatCleaner = class FormBuilderChatCleaner extends _tasks.ScheduledTask {
    getName() {
        return 'FormBuilderChatCleaner';
    }
    getWhere() {
        return {
            expiresAt: {
                [_sequelize.Op.lt]: new Date()
            }
        };
    }
    async countQueue() {
        const { AiChatSession, FormBuilderChatJob } = this.store.models;
        const expiredSessions = await AiChatSession.count({
            where: this.getWhere()
        });
        const expiredJobs = await FormBuilderChatJob.count({
            where: this.getWhere()
        });
        return expiredSessions + expiredJobs;
    }
    async run() {
        const { AiChatSession, FormBuilderChatJob } = this.store.models;
        // Models are paranoid; force a hard delete so the rows are actually reclaimed.
        await AiChatSession.destroy({
            where: this.getWhere(),
            force: true
        });
        await FormBuilderChatJob.destroy({
            where: this.getWhere(),
            force: true
        });
    }
    constructor(context){
        const { schedule, jitterTime, enabled } = _config.default.schedules.formBuilderChatCleaner;
        super(schedule, _logging.log, jitterTime, enabled);
        this.store = context.store;
    }
};

//# sourceMappingURL=FormBuilderChatCleaner.js.map