Repository navigation
Create Biocontainment Observation Review Page #1021
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
Changes from all commits
e5c4280
448c14c
e19a751
089501f
c7cc0f6
644f3c9
d752dc2
765174c
be5d91f
f4e1ebc
deb8acb
5bfeccb
8832749
f42fd9a
556c74b
102a807
f50ee09
1d9f9e9
42c70e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /* | ||
| * Copyright (c) 2026 SNPRC | ||
| */ | ||
| require("ehr/triggers").initScript(this); | ||
|
|
||
| // The 13 scored parameters on TXB 904-1 — each carries its own reason comment (9/1 decision 7) | ||
| var SCORED_FIELDS = ['WeightLoss', 'TemperatureChange', 'Responsiveness', 'HairCoat', 'Respiration', 'Petechia', | ||
| 'Bleeding', 'NasalDischarge', 'FeedEaten', 'FoodEnrichment', 'Stool', 'FluidIntake', 'Dehydration']; | ||
|
|
||
| // Carry-over shares its parameter's reason column, so a carry-over change counts as a change to that parameter | ||
| var CARRY_OVER = {WeightLoss: 'WeightLossCO', TemperatureChange: 'TemperatureChangeCO', Petechia: 'PetechiaCO', | ||
| FeedEaten: 'FeedEatenCO', FoodEnrichment: 'FoodEnrichmentCO', Dehydration: 'DehydrationCO'}; | ||
|
|
||
| var REVIEW_REQUIRED = 'Review Required'; | ||
| var COMPLETED = 'Completed'; | ||
|
|
||
| function sameValue(a, b){ | ||
| return (a == null ? null : String(a)) === (b == null ? null : String(b)); | ||
| } | ||
|
|
||
| // Trimmed text, with empty and blank both meaning "no value" | ||
| function trimmed(value){ | ||
| var text = value == null ? '' : String(value).trim(); | ||
| return text === '' ? null : text; | ||
| } | ||
|
|
||
| function sameNumber(a, b){ | ||
| return (a == null || a === '' || b == null || b === '') ? sameValue(a, b) : Number(a) === Number(b); | ||
| } | ||
|
|
||
| function sameCarryOver(a, b){ | ||
| return !!Number(a) === !!Number(b); | ||
| } | ||
|
|
||
| // Milliseconds for a date, a Java date, or LabKey's 'yyyy-MM-dd HH:mm:ss.SSS' string; NaN if it can't be read. | ||
| // Rhino 1.7R3 can't parse that string, and new Date(javaDate) drops the milliseconds, so neither is used. | ||
| function toMillis(value){ | ||
| if (value == null) | ||
| return NaN; | ||
| if (typeof value.getTime === 'function') | ||
| return Number(value.getTime()); | ||
| var m = /^(\d{4})[-\/](\d{1,2})[-\/](\d{1,2})[ T](\d{1,2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3}))?)?/.exec(String(value)); | ||
| if (!m) | ||
| return NaN; | ||
| var ms = m[7] ? Number((m[7] + '00').substring(0, 3)) : 0; | ||
| return new Date(Number(m[1]), Number(m[2]) - 1, Number(m[3]), Number(m[4]), Number(m[5]), Number(m[6] || 0), ms).getTime(); | ||
| } | ||
|
|
||
| function changedFields(row, oldRow){ | ||
| return SCORED_FIELDS.filter(function(field){ | ||
| var co = CARRY_OVER[field]; | ||
| return !sameValue(row[field], oldRow[field]) || (co && !sameCarryOver(row[co], oldRow[co])); | ||
| }); | ||
| } | ||
|
|
||
| // Compared to the second: EHR drops the milliseconds from row.date before this trigger runs, but oldRow.date keeps them | ||
| function sameDate(a, b){ | ||
| var x = toMillis(a), y = toMillis(b); | ||
| return isNaN(x) || isNaN(y) ? sameValue(a, b) : Math.floor(x / 1000) === Math.floor(y / 1000); | ||
| } | ||
|
|
||
| // Everything else the approver signed for: the animal, date, location, comment and every reason | ||
| function recordChanged(row, oldRow){ | ||
| return trimmed(row.Id) !== trimmed(oldRow.Id) | ||
| || !sameDate(row.date, oldRow.date) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per the sameDate() issue on line 58, this is always true for dates with milliseconds. Clicking Approve on such a record goes into the “changed” branch at line 116, which calls clearReview and returns without an error. The page says “Observation approved.”, but the record stays in Review Required with no signature. The ETL branch (line 107) is affected too: every re-merge resets the approval on those records. |
||
| || !sameNumber(row.Location, oldRow.Location) | ||
| || trimmed(row.Comment) !== trimmed(oldRow.Comment) | ||
| || SCORED_FIELDS.some(function(field){ | ||
| return trimmed(row[field + 'Comments']) !== trimmed(oldRow[field + 'Comments']); | ||
| }); | ||
| } | ||
|
|
||
| var SIGNATURE_FIELDS = ['reviewedBy', 'reviewedByName', 'reviewedDate', 'reviewMeaning']; | ||
|
|
||
| // Copies the signature from another row, or clears it when from is null | ||
| function setSignature(row, from){ | ||
| SIGNATURE_FIELDS.forEach(function(f){ | ||
| row[f] = from ? from[f] : null; | ||
| }); | ||
| } | ||
|
|
||
| function clearReview(row){ | ||
| row.QCStateLabel = REVIEW_REQUIRED; | ||
| setSignature(row, null); | ||
| } | ||
|
|
||
| // Every scored parameter on the form must have a value (LabKey checks this as well as CAMP) | ||
| function requireValues(scriptErrors, row){ | ||
| SCORED_FIELDS.forEach(function(field){ | ||
| if (row[field] == null || row[field] === '') | ||
| EHR.Server.Utils.addError(scriptErrors, field, 'A value is required for ' + field, 'ERROR'); | ||
| }); | ||
| } | ||
|
|
||
| function onInsert(helper, scriptErrors, row){ | ||
| requireValues(scriptErrors, row); | ||
| clearReview(row); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make sure that all the values from the form had some value. Even though camp already check this we want to make sure LabKey is also checking here. This will help us when we remove CAMP/DB from the equation. |
||
| } | ||
|
|
||
| function onUpdate(helper, scriptErrors, row, oldRow){ | ||
| var changed = changedFields(row, oldRow); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now, someone with edit access can change who signed off on a record without actually approving it, which may not meet GLP compliance requirements. The signature fields should only be set by the approval step and cleared when the record goes back for review. |
||
| // These need no reason of their own (the comment is free text, and a reason explains itself), | ||
| // but changing any of them is still a modification to approve | ||
| var otherChange = recordChanged(row, oldRow); | ||
|
|
||
| if (helper.isETL()){ | ||
| // A re-merge with identical values (e.g. after the write-back export) must not undo an approval | ||
| if (changed.length || otherChange) | ||
| clearReview(row); | ||
| else | ||
| row.QCStateLabel = oldRow.QCStateLabel; | ||
| return; | ||
| } | ||
|
|
||
| requireValues(scriptErrors, row); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This runs on approval too. the page needs an empty option in the dropdown so the reviewer can see a value is missing and correct it. If not, skip this check when only the QC state is changing. |
||
|
|
||
| if (changed.length || otherChange){ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the request asks for Completed and the record has changed, the trigger quietly downgrades it to Review Required and the update still succeeds. An approval should never quietly turn into “not approved”. When row.QCStateLabel === COMPLETED here, add an error instead, e.g. “This observation has changes that must be saved before it can be approved.” That way the caller finds out, whether the change is real or a bug like the one on line 58 |
||
| // An approval must never quietly become "not approved": the reviewer is told, whether the change is real or a bug | ||
| if (row.QCStateLabel === COMPLETED && oldRow.QCStateLabel !== COMPLETED){ | ||
| EHR.Server.Utils.addError(scriptErrors, 'QCStateLabel', 'This observation has changes that must be saved before it can be approved.', 'ERROR'); | ||
| return; | ||
| } | ||
|
|
||
| // Trimmed, so a reason of only spaces is no reason, the same rule the page applies | ||
| changed.forEach(function(field){ | ||
| var reason = trimmed(row[field + 'Comments']); | ||
| if (!reason) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (!reason) accepts a reason that is empty spaces - " ". The page trims before checking, but the server doesn’t, so a blank reason can still be saved through the grid edit form or the API. Trim the reason before checking it (and before the “same reason” check on line 94), so the server enforces the same rule as the page. |
||
| EHR.Server.Utils.addError(scriptErrors, field + 'Comments', 'A reason is required for each changed value', 'ERROR'); | ||
| else if (reason === trimmed(oldRow[field + 'Comments'])) | ||
| EHR.Server.Utils.addError(scriptErrors, field + 'Comments', 'Enter a new reason; this one was given for an earlier change', 'ERROR'); | ||
| else | ||
| row[field + 'Comments'] = reason; | ||
| }); | ||
|
|
||
| // The approver approves the record and any modification (9/1 decision 6) | ||
| clearReview(row); | ||
| return; | ||
| } | ||
|
|
||
| // Not approved (sent back for review by hand, or any other state): the old signature no longer applies | ||
| if (row.QCStateLabel !== COMPLETED){ | ||
| setSignature(row, null); | ||
| return; | ||
| } | ||
|
|
||
| // Already approved and still approved: the signature can't be edited | ||
| if (oldRow.QCStateLabel === COMPLETED){ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An approved record’s ID, date, location or reasons can still be changed without re-review |
||
| setSignature(row, oldRow); | ||
| return; | ||
| } | ||
|
|
||
| // Approval: the only place the signature is set | ||
|
|
||
| // The reviewer must have seen the version being approved; EHR fills in modified, so it is sent under its own name | ||
| var viewed = toMillis(row.viewedModified); | ||
| if (isNaN(viewed) || viewed !== toMillis(oldRow.modified)){ | ||
| EHR.Server.Utils.addError(scriptErrors, 'QCStateLabel', 'This observation changed after you opened it. Check the current version and approve again.', 'ERROR'); | ||
| return; | ||
| } | ||
|
|
||
| var user = LABKEY.Security.currentUser; | ||
|
|
||
| if (sameValue(user.id, oldRow.createdBy)){ | ||
| EHR.Server.Utils.addError(scriptErrors, 'QCStateLabel', 'This observation must be reviewed by someone other than its author', 'ERROR'); | ||
| return; | ||
| } | ||
|
|
||
| // Printed name for the signature record (REQ-028), read in Java so approvers don't need permission to see user details | ||
| var triggerHelper = new org.labkey.snprc_ehr.query.SNPRC_EHRTriggerHelper(user.id, LABKEY.Security.currentContainer.id); | ||
| var printedName = String(triggerHelper.getPrintedName()); | ||
|
|
||
| row.reviewedBy = user.id; | ||
| row.reviewedByName = printedName; | ||
| row.reviewedDate = new Date(); | ||
| row.reviewMeaning = 'Reviewed and approved'; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <customView xmlns="http://labkey.org/data/xml/queryCustomView" hidden="true"> | ||
| <columns> | ||
| <column name="Location"/> | ||
| <column name="Id"/> | ||
| <column name="date"/> | ||
| <column name="createdBy"/> | ||
| <column name="created"/> | ||
| </columns> | ||
| <sorts> | ||
| <sort column="Location"/> | ||
| <sort column="date"/> | ||
| <sort column="Id"/> | ||
| </sorts> | ||
| </customView> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An approved record’s ID, date, location or reasons can still be changed without re-review. Only the 13 scored fields, their carry-overs and their comment are counted as changes. Need to include Id, date, Location and the comments fields in the change check.