Skip to content

Commit

Permalink
feat: destroyclaim object available in injected methods
Browse files Browse the repository at this point in the history
  • Loading branch information
DaTebe committed Jan 27, 2023
1 parent f6f38e2 commit 1dbc808
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 26 deletions.
7 changes: 6 additions & 1 deletion example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ support.addDestroySubjectExtension(
}
);
support.addDestroyContactExtension("std:agent", stdAgentSchema, {
evaluation: () => true,
evaluation: (contact, destroyclaim) => {
console.log(
`Can access current destroyclaim using last parameter. ${destroyclaim.getId()}`
);
return true;
},
});
support.addDestroyConditionExtension(
"std:fromPointInTime",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "@datebe/destroyclaim-js",
"displayName": "Destroy Claim JS",
"description": "",
"version": "1.0.3",
"main": "dist/functions/module.js",
"version": "1.1.0",
"main": "index.js",
"publishConfig": {
"access": "public"
},
Expand Down
14 changes: 10 additions & 4 deletions src/modules/DestroyActionExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,36 @@ class DestroyActionExtension extends Extension {

/**
* start processing destroy action and corresponding destroy subject in real mode
* @param {Object} subject destroy subject that refers to this action
* @param {Object} destroyclaim Destroyclaim object this subject is part of
* @returns {*} return type depends on the injected realModeCallback.
*/
async processRealMode(subject) {
async processRealMode(subject, destroyclaim) {
if (_.isUndefined(this.#realModeCallback)) {
throw new TypeError(
"DestroyActionExtension: realMode function was not set on construction"
);
}
await this.#preProcess(this);
await this.#preProcess(this, destroyclaim);
await this.#realModeCallback(this, subject);
await this.#postProcess(this);
await this.#postProcess(this, destroyclaim);
}

/**
* start processing destroy action and corresponding destroy subject in simulation mode
* @param {Object} subject destroy subject that refers to this action
* @param {Object} destroyclaim Destroyclaim object this subject is part of
* @returns {*} return type depends on the injected simulationModeCallback.
*/
async processSimulationMode(subject) {
async processSimulationMode(subject, destroyclaim) {
if (_.isUndefined(this.#simulationModeCallback)) {
throw new TypeError(
"DestroyActionExtension: simulationMode function was not set on construction"
);
}
await this.#preProcess(this, destroyclaim);
await this.#simulationModeCallback(this, subject);
await this.#postProcess(this, destroyclaim);
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/modules/DestroyClaim.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class DestroyClaim {
const evaluateSubjects = this.#destroySubjects.map(async (subject) => ({
id: subject.getId(),
evaluation:
(await subject.evaluate()) &&
(await subject.evaluate(this)) &&
subject.getName() in this.#support.extensions, // second check is for normal mode, need to check if extension is supported
}));

Expand All @@ -201,7 +201,7 @@ class DestroyClaim {
const evaluateContacts = this.#destroyContacts.map(async (contact) => ({
id: contact.getId(),
evaluation:
(await contact.evaluate()) &&
(await contact.evaluate(this)) &&
contact.getName() in this.#support.extensions,
}));

Expand All @@ -224,7 +224,7 @@ class DestroyClaim {
async (condition) => ({
id: condition.getId(),
evaluation:
(await condition.evaluate()) &&
(await condition.evaluate(this)) &&
condition.getName() in this.#support.extensions,
})
);
Expand All @@ -247,7 +247,7 @@ class DestroyClaim {
const evaluateActions = this.#destroyActions.map(async (action) => ({
id: action.getId(),
evaluation:
(await action.evaluate()) &&
(await action.evaluate(this)) &&
action.getName() in this.#support.extensions,
}));

Expand Down Expand Up @@ -590,17 +590,17 @@ class DestroyClaim {
) {
if (_.isNull(subject.getAction())) {
if (!isSimulationMode(this.#destroyclaimOriginal)) {
return subject.processRealMode();
return subject.processRealMode(this);
}
return subject.processSimulationMode();
return subject.processSimulationMode(this);
}
const action = this.#destroyActions.find(
(o) => o.getId() === subject.getAction()
);
if (!isSimulationMode(this.#destroyclaimOriginal)) {
return action.processRealMode(subject);
return action.processRealMode(subject, this);
}
return action.processSimulationMode(subject);
return action.processSimulationMode(subject, this);
}
return Promise.resolve(true);
});
Expand Down
17 changes: 10 additions & 7 deletions src/modules/DestroySubjectExtension.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable class-methods-use-this */
const { Type } = require("ajv/dist/compile/util");
const _ = require("lodash");
const Extension = require("./Extension");

Expand Down Expand Up @@ -96,30 +95,34 @@ class DestroySubjectExtension extends Extension {

/**
* start processing destroy subject in real mode
* @param {Object} destroyclaim Destroyclaim object this subject is part of
* @returns {*} return type depends on the injected realModeCallback.
*/
async processRealMode() {
async processRealMode(destroyclaim) {
if (_.isUndefined(this.#realModeCallback)) {
throw new TypeError(
"DestroySubjectExtension: realMode function was not set on construction"
);
}
await this.#preProcess(this);
await this.#realModeCallback(this);
await this.#postProcess(this);
await this.#preProcess(this, destroyclaim);
await this.#realModeCallback(this, destroyclaim);
await this.#postProcess(this, destroyclaim);
}

/**
* start processing destroy subject in simulation mode
* @param {Object} destroyclaim Destroyclaim object this subject is part of
* @returns {*} return type depends on the injected simulationModeCallback.
*/
async processSimulationMode() {
async processSimulationMode(destroyclaim) {
if (_.isUndefined(this.#simulationModeCallback)) {
throw new TypeError(
"DestroySubjectExtension: simulationMode function was not set on construction"
);
}
await this.#simulationModeCallback(this);
await this.#preProcess(this, destroyclaim);
await this.#simulationModeCallback(this, destroyclaim);
await this.#postProcess(this, destroyclaim);
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/modules/Extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,13 @@ class Extension {

/**
* evaluate the extension
* @param {Object} destroyclaim Destroyclaim object this extension is part of
* @returns {Boolean} returns if evaluation was positive (true) or negative (false)
*/
async evaluate() {
await this.#preEvaluation(this);
this.#evaluationResult = await this.#evaluation(this);
await this.#postEvaluation(this);
async evaluate(destroyclaim) {
await this.#preEvaluation(this, destroyclaim);
this.#evaluationResult = await this.#evaluation(this, destroyclaim);
await this.#postEvaluation(this, destroyclaim);
return this.#evaluationResult;
}
}
Expand Down

0 comments on commit 1dbc808

Please sign in to comment.