RallyTechServices / user-story-ancestor-grid

Custom list that shows user stories and their ancestors
MIT License
5 stars 5 forks source link

Export Issues #12

Open stevewingert opened 3 years ago

stevewingert commented 3 years ago

Found two export issues:

  1. The Export always exports with tasks even if the user selects the "User Stories..." option.
  2. If you export a list of stories that do not contain tasks, the "filters" variable in the _exportTasks function becomes null and the waspi call for the tasks fail with no errors reported to the user.

I fixed these two issues with minimal code refactoring:

  1. Added a _standardExport handler for the "User Stories..." option
  2. In the _export function, I build an array of user story object ids before calling the _exportTasks function (used the code in the _exportTasks function)
  3. I pass the array of user story object ids into the _exportTasks function after checking the array is not zero length

    Code Updates _standardExport: function(){ this._export(false); }, _deepExport: function(){ this._export(true); }, _export: function(includeTasks) {

    var filters = this.getExportFilters();
    
    var columnCfgs = this.down('rallytreegrid').columns,
        additionalFields = _.filter(columnCfgs, function(c){ return c.text !== 'Rank' && (c.xtype === 'rallyfieldcolumn' || c.xtype === "treecolumn"); }),
        derivedFields = this.getDerivedColumns(),
        columns = Ext.Array.merge(additionalFields, derivedFields);
    
    var fetch = _.pluck(additionalFields, 'dataIndex');
    fetch.push('ObjectID');
    if (includeTasks){
        fetch.push('Tasks');
    }
    if (!Ext.Array.contains(fetch, this.getFeatureName())){
        fetch.push(this.getFeatureName());
    }
    this.setLoading('Loading data to export...');
    this.logger.log('columns', columnCfgs);
    this.fetchWsapiRecords({
        model: 'HierarchicalRequirement',
        fetch: fetch,
        filters: filters,
        limit: 'Infinity'
    }).then({
        success: this.updateExportStories,
        scope: this
    }).then({
        success: function(records){
            var oids = [];
            for (var i=0; i<records.length; i++){
                if (records[i].get('Tasks') && records[i].get('Tasks').Count){
                    oids.push(records[i].get('ObjectID'));
                }
            }
    
            // if tasks are to be exported and there are tasks to export
            if (includeTasks **&& oids.length != 0){
                this._exportTasks(records, fetch, columns, oids);
            } else {
                var csv = this.getExportCSV(records, columns);
                var filename = Ext.String.format("export-{0}.csv",Ext.Date.format(new Date(),"Y-m-d-h-i-s"));
                CArABU.technicalservices.FileUtilities.saveCSVToFile(csv, filename);
            }
        },
        failure: this.showErrorNotification,
        scope: this
    }).always(function(){ this.setLoading(false); }, this);

    }, _exportTasks: function(userStories, fetch, columns, oids){

    var filters = Ext.Array.map(oids, function(o){
        return {
            property: 'WorkProduct.ObjectID',
            value: o
        };
    });
    
    filters = Rally.data.wsapi.Filter.or(filters);
    
    fetch.push('WorkProduct');
    
    this.fetchWsapiRecords({
        model: 'Task',
        fetch: fetch,
        filters: filters,
        limit: 'Infinity',
        enablePostGet: true
    }).then({   
        success: function(tasks){
    
            this.logger.log('exportTasks', tasks.length);
            var taskHash = {};
            for (var j=0; j<tasks.length; j++){
                if (!taskHash[tasks[j].get('WorkProduct').ObjectID]){
                    taskHash[tasks[j].get('WorkProduct').ObjectID] = [];
                }
                taskHash[tasks[j].get('WorkProduct').ObjectID].push(tasks[j]);
            }
    
            var rows = [];
            for (var j=0; j<userStories.length; j++){
                rows.push(userStories[j]);
                var ts = taskHash[userStories[j].get('ObjectID')];
                if (ts && ts.length > 0){
                    rows = rows.concat(ts);
                }
            }
    
            columns.push({
                dataIndex: 'WorkProduct',
                text: 'User Story'
            });
            var csv = this.getExportCSV(rows, columns);
            var filename = Ext.String.format("export-{0}.csv",Ext.Date.format(new Date(),"Y-m-d-h-i-s"));
            CArABU.technicalservices.FileUtilities.saveCSVToFile(csv, filename);
        }
        ,
        failure: function(msg){
            var msg = "Unable to export tasks due to error:  " + msg
            this.showErrorNotification(msg);
        },
        scope: this
    });

    }