flexbiz-server
Version:
Flexible Server
7 lines (6 loc) • 2.01 kB
JavaScript
'use strict';const {isBookLocked}=require("./utils");module.exports=function($schema$$){$schema$$.pre("deleteOne",preDelete);$schema$$.pre("deleteMany",preDelete);$schema$$.post("deleteOne",deletePost);$schema$$.post("deleteMany",deletePost);$schema$$.post("remove",deletePost);$schema$$.post("findByIdAndRemove",deletePost)};
function deletePost($modelName_res_rs$$){$modelName_res_rs$$&&$modelName_res_rs$$.deletedCount&&(!($modelName_res_rs$$=(this.mongooseCollection||{}).modelName)||0<=global.model_books.indexOf($modelName_res_rs$$)||($modelName_res_rs$$=this._conditions)&&(Array.isArray($modelName_res_rs$$)?$modelName_res_rs$$.forEach($v$$=>{deleteBooks($v$$)}):deleteBooks($modelName_res_rs$$)))}
async function deleteBooks($query$$){const $id_ct$$=$query$$._id;$id_ct$$&&global.model_books.forEach($r$$=>{global.getModel($r$$).deleteMany({id_ct:$id_ct$$})})}
async function preDelete($next$$){const $modelName$$=(this.mongooseCollection||{}).modelName;if(!$modelName$$)return $next$$();let $model$$=global.getModel($modelName$$),$deletedData$$;if($model$$.schema.paths.ngay_ct&&$model$$.schema.paths.id_app&&0<=global.model_books.indexOf($modelName$$)){$deletedData$$=await $model$$.findOne(this._conditions).lean();if(!$deletedData$$)return $next$$();if(await isBookLocked($deletedData$$))return $next$$(Error(`Kh\u00f4ng th\u1ec3 xo\u00e1 s\u1ed5 ${$modelName$$}. Data \u0111\u00e3 \u0111\u01b0\u1ee3c kho\u00e1`))}if($modelName$$&&
0<=$modelName$$.indexOf("tdttno_tt")){$deletedData$$||($deletedData$$=await $model$$.findOne(this._conditions).lean());if(!$deletedData$$)return $next$$();$deletedData$$.id_tdtt&&await global.getModel("tdttno").updateOne({_id:$deletedData$$.id_tdtt},{tat_toan:!1})}if($modelName$$&&0<=$modelName$$.indexOf("tdttco_tt")){$deletedData$$||($deletedData$$=await $model$$.findOne(this._conditions).lean());if(!$deletedData$$)return $next$$();$deletedData$$.id_tdtt&&await global.getModel("tdttco").updateOne({_id:$deletedData$$.id_tdtt},
{tat_toan:!1})}$next$$()};