Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dictionary Release 1.20 #423

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
node_modules
dictionary.json
populated_dictionary.json
Expand Down
97 changes: 68 additions & 29 deletions references/validationFunctions/treatment/checkWhenNoTreatment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,81 @@
* You should have received a copy of the GNU Affero General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
*
*/

/**
* If treatment_type is 'No treatment', core treatment fields should not be submitted.
* If treatment_type is 'No treatment' or 'Unknown', core treatment fields should not be submitted.
*/

const validation = () =>
(function validate(inputs) {
const {$row, $name, $field} = inputs;
let result = {valid: true, message: "Ok"};
const coreFields = ['treatment_start_interval', 'treatment_duration', 'is_primary_treatment', 'treatment_intent', 'treatment_setting', 'response_to_treatment_criteria_method', 'response_to_treatment'];

// checks for a string just consisting of whitespace
const checkforEmpty = (entry) => {return /^\s+$/g.test(decodeURI(entry).replace(/^"(.*)"$/, '$1'))};
const treatmentType = ($row.treatment_type).map(value => value.toLowerCase());

if (!treatmentType.includes("no treatment") && coreFields.includes($name) && (!$field || $field === null || checkforEmpty($field))) {
result = { valid: false, message: `The '${$name}' field must be submitted when the 'treatment_type' field is '${treatmentType}'`};
}
else if (treatmentType.includes("no treatment") && ($field && $field != null && !(checkforEmpty($field)))) {
if (coreFields.includes($name) || (typeof($field) === 'string' && $field.trim().toLowerCase() != 'not applicable') || typeof($field) === 'number') {
result = { valid: false, message: `The '${$name}' field cannot be submitted if the 'treatment_type' field is '${treatmentType}'`};
const validation = () =>
(function validate(inputs) {
const { $row, $name, $field } = inputs;
const result = { valid: true, message: 'Ok' };

const arrayItemsInSecondArray = (arr1, arr2) => {
return arr2.some(arr2Item => {
return arr1.includes(arr2Item);
});
};

const coreFields = [
'treatment_start_interval',
'treatment_duration',
'is_primary_treatment',
'treatment_intent',
'treatment_setting',
'response_to_treatment_criteria_method',
'response_to_treatment',
];

const treatmentExceptionTypes = ['no treatment', 'unknown'];

// checks for a string just consisting of whitespace
const checkforEmpty = entry => {
return /^\s+$/g.test(decodeURI(entry).replace(/^"(.*)"$/, '$1'));
};
const treatmentTypes = $row.treatment_type.map(value => value.toLowerCase());

const recordHasTreatments = !arrayItemsInSecondArray(
treatmentExceptionTypes,
treatmentTypes,
);

if (recordHasTreatments) {
if (
coreFields.includes($name) &&
(!$field || $field === null || checkforEmpty($field))
) {
return {
valid: false,
message: `The '${$name}' field must be submitted when the 'treatment_type' field is '${treatmentTypes}'`,
};
}

} else if ($field && $field != null && !checkforEmpty($field)) {
if (
coreFields.includes($name) ||
(typeof $field === 'string' && $field.trim().toLowerCase() != 'not applicable') ||
typeof $field === 'number'
) {
return {
valid: false,
message: `The '${$name}' field cannot be submitted if the 'treatment_type' field is '${treatmentTypes}'`,
};
}
}
}
return result;
});
return result;
});

module.exports = validation;
Loading