Skip to content

Commit

Permalink
🐛 make sure that
Browse files Browse the repository at this point in the history
the original_record attribute is preserved when saving a document
  • Loading branch information
skerit committed Nov 27, 2023
1 parent 422ebeb commit 9d0af76
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions lib/app/helper_model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1807,8 +1807,7 @@ Model.setMethod(function createRecord(document, options, callback) {
// Normalize & clone the data, set default values, ...
let data = this.compose(document, options);

// Turn it into a new document
document = this.createDocument(data);
document = createDocumentForSaving(this, document, data);

Function.series(function recompute(next) {
let result = document.recomputeValues();
Expand Down Expand Up @@ -1852,6 +1851,29 @@ Model.setMethod(function createRecord(document, options, callback) {
})
});

/**
* Create a document used for saving
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 1.3.21
* @version 1.3.21
*
* @param {Model} model
* @param {Document|Object} original_input
* @param {Object} data
*/
function createDocumentForSaving(model, original_input, data) {

// Turn it into a new document
let document = model.createDocument(data);

if (original_input?.$attributes.original_record) {
document.$attributes.original_record = original_input.$attributes.original_record;
}

return document;
}

/**
* Update a record in the database
*
Expand All @@ -1871,7 +1893,7 @@ Model.setMethod(function updateRecord(document, options, callback) {
let data = this.compose(document, Object.assign({update: true}, options));

// Turn it into a new document
document = this.createDocument(data);
document = createDocumentForSaving(this, document, data);

Function.series(function recompute(next) {
let result = document.recomputeValues();
Expand Down

0 comments on commit 9d0af76

Please sign in to comment.