Skip to content

Commit

Permalink
Merge pull request #8067 from jrjohnson/i5642-download-report
Browse files Browse the repository at this point in the history
Fix Issues with Report Downloads
  • Loading branch information
dartajax authored Aug 14, 2024
2 parents eef95af + 9df349c commit 12edb05
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 13 deletions.
50 changes: 37 additions & 13 deletions packages/frontend/app/services/reporting.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Service, { service } from '@ember/service';
import { pluralize } from 'ember-inflector';
import { camelize, dasherize } from '@ember/string';
import { camelize, capitalize, dasherize } from '@ember/string';
import striptags from 'striptags';
import { mapBy, sortBy } from 'ilios-common/utils/array-helpers';
import { map } from 'rsvp';
Expand Down Expand Up @@ -162,8 +162,24 @@ export default class ReportingService extends Service {

async instructorsArrayResults(report) {
const filters = await this.#getFilters(report);
const graphqlFilters = filters.map((filter) => {
const specialInstructed = [
'learningMaterials',
'sessionTypes',
'courses',
'sessions',
'academicYears',
];
specialInstructed.forEach((special) => {
if (filter.includes(special)) {
const cap = capitalize(special);
filter = filter.replace(special, `instructed${cap}`);
}
});
return filter;
});
const attributes = ['id', 'firstName', 'middleName', 'lastName', 'displayName'];
const result = await this.graphql.find('users', filters, attributes.join(','));
const result = await this.graphql.find('users', graphqlFilters, attributes.join(','));
const names = result.data.users
.map(({ firstName, middleName, lastName, displayName }) => {
if (displayName) {
Expand All @@ -180,7 +196,7 @@ export default class ReportingService extends Service {
})
.sort();

return [[this.intl.t('general.instructors')]].concat(names);
return [[this.intl.t('general.instructors')]].concat(names.map((name) => [name]));
}

async valueResults(endpoint, report, translationKey) {
Expand Down Expand Up @@ -229,16 +245,24 @@ export default class ReportingService extends Service {
async termsArrayResults(report) {
const filters = await this.#getFilters(report);
const result = await this.graphql.find('terms', filters, 'id');
let terms = await this.store.query('term', {
filters: {
ids: [result.data.terms.map(({ id }) => id)],
},
});
const titles = map(terms.slice(), async (term) => {
const vocabulary = await term.get('vocabulary');
const titleWithParentTitles = await term.getTitleWithParentTitles();
return vocabulary.title + ' > ' + titleWithParentTitles;
}).sort();
let terms = [];
for (let i = 0; i < result.data.terms.length; i += 100) {
const chunk = result.data.terms.slice(i, i + 100);
const loadedTerms = await this.store.query('term', {
filters: {
id: chunk.map(({ id }) => id),
},
});
terms = terms.concat(loadedTerms);
}

const titles = (
await map(terms, async (term) => {
const vocabulary = await term.vocabulary;
const titleWithParentTitles = await term.getTitleWithParentTitles();
return [vocabulary.title + ' > ' + titleWithParentTitles];
})
).sort();
return [[this.intl.t('general.vocabulary')]].concat(titles);
}

Expand Down
65 changes: 65 additions & 0 deletions packages/frontend/tests/unit/services/reporting-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,69 @@ module('Unit | Service | reporting', function (hooks) {
assert.strictEqual(props.school, 'School of Schools');
assert.strictEqual(props.subject, 'Competencies');
});

test('getArrayResults() for term report', async function (assert) {
assert.expect(2);
const school = this.server.create('school', { title: 'School of Schools' });
const report = this.server.create('report', {
subject: 'term',
school,
});
const vocabulary = this.server.create('vocabulary', { school });
const terms = this.server.createList('term', 2, { vocabulary });
this.server.post('api/graphql', function (schema, { requestBody }) {
const { query } = JSON.parse(requestBody);
assert.strictEqual(query, 'query { terms(schools: [1]) { id } }');
return {
data: {
terms: terms.reverse().map(({ id }) => ({ id })),
},
};
});

const store = this.owner.lookup('service:store');
const reportModel = await store.findRecord('report', report.id);
const props = await this.service.getArrayResults(reportModel, 1999);
assert.deepEqual(props, [['Vocabulary'], ['Vocabulary 1 > term 0'], ['Vocabulary 1 > term 1']]);
});

test('getArrayResults() for instructor report', async function (assert) {
assert.expect(2);
const school = this.server.create('school', { title: 'School of Schools' });
const course = this.server.create('course', { id: 42, school });
const session = this.server.create('session', { course });
const ilmSession = this.server.create('ilmSession', { session });
const report = this.server.create('report', {
subject: 'instructor',
school,
prepositionalObject: 'course',
prepositionalObjectTableRowId: 42,
});
const instructors = this.server.createList('user', 2, { instructorIlmSessions: [ilmSession] });
this.server.post('api/graphql', function (schema, { requestBody }) {
const { query } = JSON.parse(requestBody);
assert.strictEqual(
query,
'query { users(schools: [1], instructedCourses: [42]) { id,firstName,middleName,lastName,displayName } }',
);
return {
data: {
users: instructors
.reverse()
.map(({ id, firstName, middleName, lastName, displayName }) => ({
id,
firstName,
middleName,
lastName,
displayName,
})),
},
};
});

const store = this.owner.lookup('service:store');
const reportModel = await store.findRecord('report', report.id);
const props = await this.service.getArrayResults(reportModel, 1999);
assert.deepEqual(props, [['Instructors'], ['0 guy M. Mc0son'], ['1 guy M. Mc1son']]);
});
});

0 comments on commit 12edb05

Please sign in to comment.