在删除log时,删除与此log关联的oper

This commit is contained in:
Pan Qiancheng 2024-10-22 17:10:05 +08:00
parent 9ac752687a
commit e3881534b9
1 changed files with 11 additions and 29 deletions

View File

@ -378,43 +378,25 @@ export const logTriggers: Trigger<EntityDict & BaseEntityDict, 'log', AsyncConte
return (result.oper?.remove || 0) + (result2.log?.remove || 0);
}
} as CreateTriggerInTxn<EntityDict & BaseEntityDict, 'log', AsyncContext<EntityDict & BaseEntityDict>>,
// 在删除log时除与此log关联的oper
// 在删除log时除与此log关联的oper
{
name: '当删除log时除与此log关联的oper',
name: '当删除log时除与此log关联的oper',
entity: 'log',
action: 'remove',
when: 'before',
fn: async ({ operation }, context) => {
const { filter } = operation;
assert(filter);
// 查询这次删除操作涉及到的所有log
const logs = await context.select('log', {
data: {
id: 1,
oper$log: {
$entity: 'oper',
data: {
id: 1,
},
},
},
filter,
// 查询这次删除操作涉及到的所有log
const result = await context.operate('oper', {
id: "dummy",
action: 'remove',
data: {},
filter: {
log: filter,
}
}, {});
const operIds = logs.flatMap(log => (log as BaseEntityDict['log']['Schema']).oper$log!.map(oper => oper.id));
let result = 0;
for (const operId of operIds) {
const result2 = await context.operate('oper', {
id: operId,
action: 'update',
data: {
logId: null,
},
}, {});
result += result2.oper?.update || 0;
}
return result;
return result.oper?.remove || 0;
}
} as RemoveTriggerInTxn<EntityDict & BaseEntityDict, 'log', AsyncContext<EntityDict & BaseEntityDict>>,
];