Skip to content

Commit

Permalink
♻️ Make Revision behaviour use the appropriate document methods/prope…
Browse files Browse the repository at this point in the history
…rties
  • Loading branch information
skerit committed Feb 19, 2024
1 parent f9165d3 commit 60386e4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
35 changes: 14 additions & 21 deletions lib/app/behaviour/revision_behaviour.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,14 @@ Revision.setMethod(function compare(left, right) {
*
* @author Jelle De Loecker <[email protected]>
* @since 0.0.1
* @version 1.0.5
* @version 1.4.0
*/
Revision.setMethod(function beforeSave(record, options, creating) {

let main = record[this.model.name];
let main = record.$main;

if (!main) {
throw new Error('Unable to find main "' + this.model.name + '" data');
throw new Error('Unable to find main "' + this.model.model_name + '" data');
}

// No revision to save when creating a record
Expand All @@ -221,14 +221,12 @@ Revision.setMethod(function beforeSave(record, options, creating) {
next = this.wait('series');

// Find the original record
Model.get(that.model.name).findById(main._id, async function gotRecord(err, result) {

var ori;
Model.get(this.model.model_name).findById(record.$pk, async function gotRecord(err, result) {

if (result) {

// Get the original data
ori = await that.model.convertRecordToDatasourceFormat(result);
let ori = await that.model.convertRecordToDatasourceFormat(result);

// Store the original data in a weakmap for later
revision_before.set(options, ori);
Expand All @@ -250,17 +248,11 @@ Revision.setMethod(function beforeSave(record, options, creating) {
*
* @author Jelle De Loecker <[email protected]>
* @since 0.0.1
* @version 1.1.0
* @version 1.4.0
*/
Revision.setMethod(function afterSave(record, options, created) {

var that = this,
earlier_data,
right,
main,
that,
left,
next;
let earlier_data;

if (created) {
earlier_data = {};
Expand All @@ -273,11 +265,12 @@ Revision.setMethod(function afterSave(record, options, created) {
return;
}

next = this.wait();
main = record[that.model.name] || record;
let doc = this.model.createDocument(record);
let next = this.wait();
const that = this;

// Find the complete saved item
Model.get(that.model.name).findById(main._id, async function gotRecord(err, result) {
Model.get(this.model.model_name).findByPk(doc.$pk, async function gotRecord(err, result) {

if (result) {

Expand All @@ -287,8 +280,8 @@ Revision.setMethod(function afterSave(record, options, created) {
if (new_data) {

// Convert the objects so they can be diffed properly
left = JSON.toDryObject(earlier_data);
right = JSON.toDryObject(new_data);
let left = JSON.toDryObject(earlier_data),
right = JSON.toDryObject(new_data);

// Diff them
let delta = that.compare(left, right);
Expand All @@ -311,7 +304,7 @@ Revision.setMethod(function afterSave(record, options, created) {

// Add the delta information
revision_data = {
[that.revision_model.name] : revision_data
[that.revision_model.model_name] : revision_data
};

// Save the data
Expand Down
8 changes: 2 additions & 6 deletions lib/class/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1181,10 +1181,6 @@ Model.setMethod(function auditRecord(document, options, callback) {
*/
Model.setMethod(function convertRecordToDatasourceFormat(record, options, callback) {

var that = this,
pledge,
data;

if (typeof options == 'function') {
callback = options;
options = {};
Expand All @@ -1194,12 +1190,12 @@ Model.setMethod(function convertRecordToDatasourceFormat(record, options, callba
options = {};
}

data = record[this.name] || record;
let data = record.$main || record[this.model_name] || record;

// Normalize the data
data = this.compose(data, options);

pledge = this.datasource.toDatasource(this, data);
let pledge = this.datasource.toDatasource(this, data);

pledge.handleCallback(callback);

Expand Down

0 comments on commit 60386e4

Please sign in to comment.