diff --git a/packages/frontend/app/services/reporting.js b/packages/frontend/app/services/reporting.js index 40ef22663a..1569787d65 100644 --- a/packages/frontend/app/services/reporting.js +++ b/packages/frontend/app/services/reporting.js @@ -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'; @@ -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) { @@ -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) { @@ -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); } diff --git a/packages/frontend/tests/unit/services/reporting-test.js b/packages/frontend/tests/unit/services/reporting-test.js index 7493b38492..e0a01c4cbd 100644 --- a/packages/frontend/tests/unit/services/reporting-test.js +++ b/packages/frontend/tests/unit/services/reporting-test.js @@ -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']]); + }); });