Skip to content

Commit

Permalink
Update MCC shipping window to allow multiple IDs at once (#162)
Browse files Browse the repository at this point in the history
* Update MCC shipping window to allow multiple IDs at once
  • Loading branch information
bbimber authored Jun 14, 2024
1 parent c823024 commit 6c2f9e8
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 133 deletions.
312 changes: 182 additions & 130 deletions mcc/resources/web/mcc/window/MarkShippedWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ Ext4.define('MCC.window.MarkShippedWindow', {
return;
}

if (checked.length !== 1) {
Ext4.Msg.alert('Error', 'Currently only one ID is supported as a time');
return;
}

Ext4.create('MCC.window.MarkShippedWindow', {
dataRegionName: dataRegionName,
rowIds: checked
Expand All @@ -37,23 +32,6 @@ Ext4.define('MCC.window.MarkShippedWindow', {
html: 'This will: <br>1) Mark the selected animals as shipped from this center<br>2) Enter a new demographics record in the selected study<br>3) Preserve the MCC ID for each animal.',
border: false,
style: 'padding-bottom: 10px;'
},{
xtype: 'checkbox',
fieldLabel: 'Animal Will Use Previous Id',
itemId: 'usePreviousId',
listeners: {
scope: this,
change: function (field, val) {
var target = field.up('panel').down('#newId');
target.allowBlank = !!val;
target.setVisible(!val);
}
},
},{
xtype: 'textfield',
fieldLabel: 'New ID (blank if unchanged)',
itemId: 'newId',
allowBlank: false
},{
xtype: 'datefield',
fieldLabel: 'Effective Date',
Expand Down Expand Up @@ -105,7 +83,7 @@ Ext4.define('MCC.window.MarkShippedWindow', {
}
}
}
}],
}, this.getAnimalIdFields()],
buttons: [{
text: 'Submit',
handler: this.onSubmit,
Expand All @@ -121,11 +99,72 @@ Ext4.define('MCC.window.MarkShippedWindow', {
this.callParent(arguments);
},

getAnimalIdFields: function(){
var fields = [{
xtype: 'displayfield',
value: 'Animal ID',
width: 150
},{
xtype: 'displayfield',
value: 'Keep Existing ID?',
width: 150
},{
xtype: 'displayfield',
value: 'New ID (blank if unchanged)',
}];

Ext4.Array.forEach(this.rowIds, function(rowId){
const animalId = this.lsidToAnimalId(rowId);
fields = fields.concat([{
xtype: 'displayfield',
value: animalId,
},{
xtype: 'checkbox',
itemId: 'usePreviousId-' + animalId,
checked: false,
listeners: {
scope: this,
change: function (field, val) {
var target = field.up('panel').down('#newId-' + animalId);
target.allowBlank = !!val;
target.setDisabled(val);
}
}
},{
xtype: 'textfield',
itemId: 'newId-' + animalId,
disabled: false,
allowBlank: true
}]);
}, this);

return {
layout: {
type: 'table',
columns: 3
},
border: false,
defaults: {
style: 'padding:5px;',
border: false
},
items: fields
};
},

lsidToAnimalId: function(lsid){
lsid = lsid.split(':')[4];
lsid = lsid.split('.');
lsid.shift();

return lsid.join('.');
},

onSubmit: function(btn){
Ext4.Msg.wait('Loading...');

var win = btn.up('window');
var lsid = win.rowIds[0];
var lsids = win.rowIds;
var effectiveDate = win.down('#effectiveDate').getValue();
var centerName = win.down('#centerName').getValue();
var targetFolder = win.down('#targetFolder').getValue();
Expand All @@ -134,9 +173,19 @@ Ext4.define('MCC.window.MarkShippedWindow', {
return;
}

if (!win.down('#usePreviousId').getValue() && !win.down('#newId').getValue()) {
Ext4.Msg.hide();
Ext4.Msg.alert('Error', 'Must enter the new ID');
var hasError = false;
Ext4.Array.forEach(this.rowIds, function(rowId) {
var animalId = this.lsidToAnimalId(rowId);
var useExisting = win.down('#usePreviousId-' + animalId).getValue();
if (!useExisting && !win.down('#newId-' + animalId).getValue()) {
Ext4.Msg.hide();
Ext4.Msg.alert('Error', 'Must enter the new ID for: ' + animalId);
hasError = true;
return false;
}
}, this);

if (hasError) {
return;
}

Expand All @@ -145,122 +194,125 @@ Ext4.define('MCC.window.MarkShippedWindow', {
LABKEY.Query.selectRows({
schemaName: 'study',
queryName: 'Demographics',
filterArray: [LABKEY.Filter.create('lsid', lsid)],
filterArray: [LABKEY.Filter.create('lsid', lsids.join(';'), LABKEY.Filter.Types.IN)],
columns: 'Id,gender,colony,species,birth,death,center,Id/MostRecentDeparture/MostRecentDeparture,Id/mccAlias/externalAlias,calculated_status,dam,sire,damMccAlias/externalAlias,sireMccAlias/externalAlias',
scope: this,
failure: LDK.Utils.getErrorCallback(),
success: function(results) {
if (!results || !results.rows || !results.rows.length) {
Ext4.Msg.hide();
Ext4.Msg.alert('Error', 'No row round for LSID: ' + lsid + '. This is not expected');
return;
Ext4.Msg.alert('Error', 'No rows found for, this is not expected');
return false;
}

var row = results.rows[0];
var newId = win.down('#newId').getValue() || row.Id;
var commands = [];
Ext4.Array.forEach(results.rows, function(row){
var effectiveId = win.down('#usePreviousId-' + row.Id).getValue() ? row.Id : win.down('#newId-' + row.Id).getValue();
// This should be checked above, although perhaps case sensitivity could get involved:
LDK.Assert.assertNotEmpty('Missing effective ID after query', effectiveId);

var shouldAddDeparture = !row['Id/MostRecentDeparture/MostRecentDeparture'] || row['Id/MostRecentDeparture/MostRecentDeparture'] !== Ext4.Date.format(row.effectiveDate, 'Y-m-d') || row.Id !== newId;
if (shouldAddDeparture) {
commands.push({
command: 'insert',
schemaName: 'study',
queryName: 'Departure',
rows: [{
Id: row.Id,
date: effectiveDate,
destination: centerName,
description: row.colony ? 'Original center: ' + row.colony : null,
qcstate: null,
objectId: null,
QCStateLabel: 'Completed'
}]
});
}
var shouldAddDeparture = !row['Id/MostRecentDeparture/MostRecentDeparture'] || row['Id/MostRecentDeparture/MostRecentDeparture'] !== Ext4.Date.format(row.effectiveDate, 'Y-m-d') || row.Id !== effectiveId;
if (shouldAddDeparture) {
commands.push({
command: 'insert',
schemaName: 'study',
queryName: 'Departure',
rows: [{
Id: row.Id,
date: effectiveDate,
destination: centerName,
description: row.colony ? 'Original center: ' + row.colony : null,
qcstate: null,
objectId: null,
QCStateLabel: 'Completed'
}]
});
}

// If going to a new LK folder, we're creating a whole new record:
if (targetFolderId.toUpperCase() !== LABKEY.Security.currentContainer.id.toUpperCase() || newId !== row.Id) {
commands.push({
command: 'insert',
containerPath: targetFolder,
schemaName: 'study',
queryName: 'Demographics',
rows: [{
Id: newId,
date: effectiveDate,
alternateIds: row.Id !== newId ? row.Id : null,
gender: row.gender,
species: row.species,
birth: row.birth,
death: row.death,
dam: row.dam,
sire: row.sire,
damMccAlias: row['damMccAlias/externalAlias'],
sireMccAlias: row['sireMccAlias/externalAlias'],
colony: centerName,
source: row.colony,
calculated_status: 'Alive',
mccAlias: row['Id/mccAlias/externalAlias'],
QCState: null,
QCStateLabel: 'Completed',
objectId: null
}]
});
// If going to a new LK folder, we're creating a whole new record:
if (targetFolderId.toUpperCase() !== LABKEY.Security.currentContainer.id.toUpperCase() || effectiveId !== row.Id) {
commands.push({
command: 'insert',
containerPath: targetFolder,
schemaName: 'study',
queryName: 'Demographics',
rows: [{
Id: effectiveId,
date: effectiveDate,
alternateIds: row.Id !== effectiveId ? row.Id : null,
gender: row.gender,
species: row.species,
birth: row.birth,
death: row.death,
dam: row.dam,
sire: row.sire,
damMccAlias: row['damMccAlias/externalAlias'],
sireMccAlias: row['sireMccAlias/externalAlias'],
colony: centerName,
source: row.colony,
calculated_status: 'Alive',
mccAlias: row['Id/mccAlias/externalAlias'],
QCState: null,
QCStateLabel: 'Completed',
objectId: null
}]
});

commands.push({
command: 'update',
containerPath: null, //Use current folder
schemaName: 'study',
queryName: 'Demographics',
rows: [{
Id: row.Id, // NOTE: always change the original record
excludeFromCensus: true
}]
});
}
else {
// Otherwise update the existing:
commands.push({
command: 'update',
containerPath: targetFolder,
schemaName: 'study',
queryName: 'Demographics',
rows: [{
Id: row.Id,
date: effectiveDate,
alternateIds: null,
gender: row.gender,
species: row.species,
birth: row.birth,
death: row.death,
dam: row.dam,
sire: row.sire,
colony: centerName,
source: row.colony,
calculated_status: 'Alive',
QCState: null,
QCStateLabel: 'Completed',
objectId: null
}]
});
commands.push({
command: 'update',
containerPath: null, //Use current folder
schemaName: 'study',
queryName: 'Demographics',
rows: [{
Id: row.Id, // NOTE: always change the original record
excludeFromCensus: true
}]
});
}
else {
// Otherwise update the existing:
commands.push({
command: 'update',
containerPath: targetFolder,
schemaName: 'study',
queryName: 'Demographics',
rows: [{
Id: row.Id,
date: effectiveDate,
alternateIds: null,
gender: row.gender,
species: row.species,
birth: row.birth,
death: row.death,
dam: row.dam,
sire: row.sire,
colony: centerName,
source: row.colony,
calculated_status: 'Alive',
QCState: null,
QCStateLabel: 'Completed',
objectId: null
}]
});

// And also add an arrival record. NOTE: set the date after the departure to get status to update properly
var arrivalDate = new Date(effectiveDate).setMinutes(effectiveDate.getMinutes() + 1);
commands.push({
command: 'insert',
containerPath: targetFolder,
schemaName: 'study',
queryName: 'Arrival',
rows: [{
Id: newId,
date: arrivalDate,
source: centerName,
QCState: null,
QCStateLabel: 'Completed',
objectId: null
}]
});
}
// And also add an arrival record. NOTE: set the date after the departure to get status to update properly
var arrivalDate = new Date(effectiveDate).setMinutes(effectiveDate.getMinutes() + 1);
commands.push({
command: 'insert',
containerPath: targetFolder,
schemaName: 'study',
queryName: 'Arrival',
rows: [{
Id: effectiveId,
date: arrivalDate,
source: centerName,
QCState: null,
QCStateLabel: 'Completed',
objectId: null
}]
});
}
}, this);

LABKEY.Query.saveRows({
commands: commands,
Expand Down
Loading

0 comments on commit 6c2f9e8

Please sign in to comment.