#638 新建记录时也应该执行公式计算,允许公式中带return语句

This commit is contained in:
殷亮辉 2020-08-18 10:08:04 +08:00
parent 2bd334d4e1
commit a949e6368b
3 changed files with 22 additions and 3 deletions

View File

@ -62,7 +62,11 @@ export const runFieldFormular = function (formula: string, params: Array<Steedos
formulaParams[key] = value;
formula = formula.replace(`{${key}}`, `__params["${key}"]`);
});
let formulaFun = `module.exports = function (__params) { return ${formula} }`;
if(!/\breturn\b/.test(formula)){
// 如果里面没有return语句则在最前面加上return前缀
formula = `return ${formula}`;
}
let formulaFun = `module.exports = function (__params) { ${formula} }`;
// console.log("==runFieldFormular==formulaFun===", formulaFun);
// console.log("==runFieldFormular==formulaParams===", formulaParams);
let result = _eval(formulaFun)(formulaParams);

View File

@ -8,10 +8,14 @@ const runFieldFormular = async function (fieldFormulaConfig: SteedosFieldFormula
export const fieldFormulaTriggers = {
beforeUpdate: async function () {
// console.log("===fieldFormulaTriggers======beforeUpdate===", this);
const configs = getObjectFieldFormulaConfigs(this.object_name);
for (const config of configs) {
// console.log("====config.formula==", config.formula);
await runFieldFormular.bind(this)(config)
}
},
beforeInsert: async function () {
const configs = getObjectFieldFormulaConfigs(this.object_name);
for (const config of configs) {
await runFieldFormular.bind(this)(config)
}
}

View File

@ -68,6 +68,10 @@ const fieldFormulaBeforeUpdate = function(userId, doc, fieldNames, modifier, opt
wrapAsync(objectql.fieldFormulaTriggers.beforeUpdate, Object.assign({userId: userId, spaceId: doc.space, id: doc._id, doc: modifier.$set, previousDoc: doc, object_name: this.object_name}))
}
const fieldFormulaBeforeInsert = function(userId, doc){
wrapAsync(objectql.fieldFormulaTriggers.beforeInsert, Object.assign({userId: userId, spaceId: doc.space, doc: doc, object_name: this.object_name}))
}
module.exports = {
extend: 'base',
actions: {
@ -545,6 +549,13 @@ module.exports = {
fieldFormulaBeforeUpdate.bind(this)(userId, doc, fieldNames, modifier, options);
}
},
"before.insert.server.fieldFormula": {
on: "server",
when: "before.insert",
todo: function (userId, doc) {
fieldFormulaBeforeInsert.bind(this)(userId, doc);
}
},
}
};
function setDetailOwner(doc, object_name, userId) {