mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
Adjusted
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
"use strict";
|
||||
|
||||
const { Op } = require("sequelize");
|
||||
|
||||
const onlyDeleted = { deletedAt: { [Op.not]: null } };
|
||||
|
||||
/**
|
||||
* Soft-restore a single record by clearing deletedAt / deletedBy.
|
||||
* Returns the record on success, null if not found.
|
||||
*
|
||||
* @param {Model} Model - Sequelize model
|
||||
* @param {object} where - Where clause (must include onlyDeleted or equivalent)
|
||||
* @param {*} restoredBy - User ID stamped onto updatedBy
|
||||
* @param {object} t - Sequelize transaction
|
||||
*/
|
||||
async function restoreOne(Model, where, restoredBy, t) {
|
||||
const record = await Model.findOne({ where, paranoid: false });
|
||||
if (!record) return null;
|
||||
|
||||
await record.restore();
|
||||
await record.update({ updatedBy: restoredBy, deletedBy: null })
|
||||
|
||||
return record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Soft-restore many records by their primary key column.
|
||||
* Returns the count of restored 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 restore
|
||||
* @param {*} restoredBy - User ID stamped onto updatedBy
|
||||
* @param {object} t - Sequelize transaction
|
||||
*/
|
||||
async function restoreMany(Model, pkColumn, ids, restoredBy, t) {
|
||||
const [count] = await Model.update(
|
||||
{ deletedAt: null, deletedBy: null, updatedBy: restoredBy ?? null },
|
||||
{ where: { [pkColumn]: ids, ...onlyDeleted }, transaction: t, paranoid: false }
|
||||
);
|
||||
return count;
|
||||
}
|
||||
|
||||
module.exports = { restoreOne, restoreMany };
|
||||
Reference in New Issue
Block a user