"use strict"; const { Op } = require("sequelize"); const onlyDeleted = { deletedAt: { [Op.not]: null } }; /** * Permanently delete a single archived record. * Returns the record on success, null if not found, false if found but not archived. * * @param {Model} Model - Sequelize model * @param {object} where - Where clause (primary key lookup) * @param {object} t - Sequelize transaction */ async function permanentDeleteOne(Model, where, t) { const record = await Model.findOne({ where, paranoid: false, transaction: t }); if (!record) return null; if (!record.deletedAt) return false; await record.destroy({ force: true, transaction: t }); return record; } /** * Permanently delete many archived records by primary key. * Returns the count of deleted records. * * @param {Model} Model - Sequelize model * @param {string} pkColumn - Primary key column name (e.g. "course_id") * @param {Array} ids - Array of primary key values to delete * @param {object} t - Sequelize transaction */ async function permanentDeleteMany(Model, pkColumn, ids, t) { if (!ids?.length) return 0; const count = await Model.destroy({ where: { [pkColumn]: ids, ...onlyDeleted }, transaction: t, paranoid: false, force: true, }); return count; } module.exports = { permanentDeleteOne, permanentDeleteMany };