feat: refs #7936 add save validation
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Jorge Penadés 2024-11-29 11:53:07 +01:00
parent c270ec3723
commit 1f6e401e39
3 changed files with 19 additions and 2 deletions

View File

@ -52,7 +52,8 @@ module.exports = Self => {
accountingEntries = await models.Xdiario.count({ASIEN: asien}, myOptions);
await models.Xdiario.destroyAll({ASIEN: asien}, myOptions);
await Self.updateAll({id: invoiceInId}, {isBooked: false}, myOptions);
const invoiceIn = await Self.findById(invoiceInId, myOptions);
await invoiceIn.updateAttribute('isBooked', false, myOptions);
} else {
const linkedBookEntry = await models.Xdiario.findOne({
fields: ['ASIEN'],

View File

@ -82,7 +82,7 @@ module.exports = Self => {
try {
const invoiceIn = await Self.findById(id, null, myOptions);
invoiceIn.updateAttributes({supplierFk,
await invoiceIn.updateAttributes({supplierFk,
supplierRef,
issued,
operated,
@ -94,6 +94,7 @@ module.exports = Self => {
companyFk,
withholdingSageFk
}, myOptions);
if (tx) await tx.commit();
return invoiceIn;
} catch (e) {

View File

@ -19,4 +19,19 @@ module.exports = Self => {
return new UserError(`This invoice has a linked vehicle.`);
return err;
});
Self.observe('before save', async function(ctx) {
if (ctx.newInstance) return;
const changes = ctx.data || ctx.instance;
const orgData = ctx.currentInstance;
let isNotEditable = orgData.isBooked || (!orgData.isBooked && changes.isBooked);
if (isNotEditable) {
for (const [key, value] of Object.entries(changes)) {
if (key !== 'isBooked' && value !== orgData[key])
throw new UserError('InvoiceIn is already booked');
}
}
});
};