Skip to content

Commit

Permalink
[Bug]: 删除台账记录时应同时删除记录附件子表记录和附件文件 #6957 [2.6]
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhaolin committed Jul 12, 2024
1 parent 94c2ee0 commit 6217f0c
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions packages/standard-objects/base.trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,19 @@ const checkCompany = async (object_name, userId, doc) => {

const beforeInsertBase = async function () {
const { doc, userId } = this;
doc.created = new Date();
doc.modified = new Date();
if (userId) {
if (!doc.owner) {
doc.owner = userId;
}
if (doc.owner === '{userId}') {
doc.owner = userId;
}
doc.created_by = userId;
doc.modified_by = userId;
}
// 迁移到insert函数中提前执行了
// doc.created = new Date();
// doc.modified = new Date();
// if (userId) {
// if (!doc.owner) {
// doc.owner = userId;
// }
// if (doc.owner === '{userId}') {
// doc.owner = userId;
// }
// doc.created_by = userId;
// doc.modified_by = userId;
// }
var extras = ["spaces", "company", "organizations", "users", "space_users"];
if (extras.indexOf(this.object_name) < 0 && doc.space) {
/* company_ids/company_id默认值逻辑*/
Expand Down Expand Up @@ -100,10 +101,11 @@ const beforeUpdateBase = async function () {
if (!doc) {
return;
}
doc.modified = new Date();
if (userId) {
doc.modified_by = userId;
}
// 迁移到update函数中提前执行了
// doc.modified = new Date();
// if (userId) {
// doc.modified_by = userId;
// }
var extras = ["spaces", "company", "organizations", "users", "space_users"];
if (extras.indexOf(this.object_name) < 0) {
/* company_ids/company_id级联修改逻辑*/
Expand Down Expand Up @@ -144,30 +146,37 @@ const afterDeleteBase = async function () {

_.each(fieldsName, function (fieldName) {
const fieldProps = fields[fieldName];
const indexOfType = fieldProps && ['file','image'].indexOf(fieldProps.type);
if( indexOfType > -1 && previousDoc[fieldName] && previousDoc[fieldName].length ){
const collection = [cfs.files,cfs.images][indexOfType];
const indexOfType = fieldProps && ['file', 'image'].indexOf(fieldProps.type);
if (indexOfType > -1 && previousDoc[fieldName] && previousDoc[fieldName].length) {
const collection = [cfs.files, cfs.images][indexOfType];
let ids = previousDoc[fieldName]
if(typeof ids === 'string'){
if (typeof ids === 'string') {
ids = [ids]
}
_.each(ids,function (id){
_.each(ids, function (id) {
collection.remove({
"_id": id
});
})
}
});
if ("cms_files" != object_name) {
// 删除附件
const cmsFilesObj = objectql.getObject('cms_files')
const cmsFiles = await cmsFilesObj.find({
filters: [
["parent/o", "=", object_name],
["parent/ids", "=", previousDoc._id]
]
})
for (const cmsFile of cmsFiles) {
await cmsFilesObj.delete(cmsFile._id)
}
}
}

module.exports = {
listenTo: 'base',
beforeInsert: async function () {
return await beforeInsertBase.apply(this, arguments)
},
beforeUpdate: async function () {
return await beforeUpdateBase.apply(this, arguments)
},
afterDelete: async function () {
return await afterDeleteBase.apply(this, arguments)
}
Expand Down

0 comments on commit 6217f0c

Please sign in to comment.