Skip to content

Commit adad36f

Browse files
authored
Merge pull request #8833 from michaelchadwick/frontend-4535-fix-insts-by-lm-subj-report
Subject Reports->Instructors by Learning Material now actually runs
2 parents 2daea05 + 47de2ec commit adad36f

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

packages/frontend/app/components/reports/subject/instructor.gjs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,40 @@ export default class ReportsSubjectInstructorComponent extends Component {
156156
return uniqueById(users);
157157
}
158158

159+
async getResultsForLearningMaterial(learningMaterialId, school) {
160+
let filters = [];
161+
if (school) {
162+
filters.push(`schools: [${school.id}]`);
163+
}
164+
filters.push(`learningMaterials: [${learningMaterialId}]`);
165+
166+
const attributes = ['id', 'school { title }'];
167+
const results = await this.graphql.find('courses', filters, attributes.join(', '));
168+
169+
if (!results.data.courses.length) {
170+
return [];
171+
}
172+
173+
const ids = results.data.courses.map(({ id }) => id);
174+
175+
//fetch courses 5 at a time for performance on the API
176+
//but send all the requests at once
177+
const promises = chunk(ids, 5).map((chunk) => this.getResultsForCourses(chunk));
178+
179+
const users = await (await Promise.all(promises)).flat();
180+
181+
return uniqueById(users);
182+
}
183+
159184
async getReportResults(subject, prepositionalObject, prepositionalObjectTableRowId, school) {
160185
if (subject !== 'instructor') {
161186
throw new Error(`Report for ${subject} sent to ReportsSubjectInstructorComponent`);
162187
}
163188

189+
if (prepositionalObject === 'learning material') {
190+
return this.getResultsForLearningMaterial(prepositionalObjectTableRowId, school);
191+
}
192+
164193
if (prepositionalObject === 'course') {
165194
return this.getResultsForCourses([prepositionalObjectTableRowId]);
166195
}

packages/frontend/tests/integration/components/reports/subject/instructor-test.gjs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,4 +318,72 @@ module('Integration | Component | reports/subject/instructor', function (hooks)
318318
</template>,
319319
);
320320
});
321+
322+
test('filter by learning material', async function (assert) {
323+
assert.expect(7);
324+
let counter = 0;
325+
this.server.post('api/graphql', function (schema, { requestBody }) {
326+
const { query } = JSON.parse(requestBody);
327+
counter++;
328+
const { users } = responseData.data;
329+
switch (counter) {
330+
case 1:
331+
assert.strictEqual(
332+
query,
333+
'query { courses(learningMaterials: [1]) { id, school { title } } }',
334+
'correct first query is run',
335+
);
336+
return {
337+
data: {
338+
courses: [
339+
{
340+
id: 1,
341+
},
342+
],
343+
},
344+
};
345+
case 2:
346+
assert.ok(query.includes('query { courses(ids: [1])'), 'correct second query is run');
347+
return {
348+
data: {
349+
courses: [
350+
{ sessions: [{ offerings: [{ instructors: [users[1]], instructorGroups: [] }] }] },
351+
{
352+
sessions: [
353+
{
354+
ilmSession: { instructors: [users[0]], instructorGroups: [] },
355+
offerings: [],
356+
instructorGroups: [],
357+
},
358+
],
359+
},
360+
],
361+
},
362+
};
363+
default:
364+
assert.ok(false, 'too many queries');
365+
}
366+
});
367+
const { id } = this.server.create('report', {
368+
subject: 'instructor',
369+
prepositionalObject: 'learning material',
370+
prepositionalObjectTableRowId: 1,
371+
});
372+
this.set('report', await this.owner.lookup('service:store').findRecord('report', id));
373+
await render(
374+
<template>
375+
<Instructor
376+
@subject={{this.report.subject}}
377+
@prepositionalObject={{this.report.prepositionalObject}}
378+
@prepositionalObjectTableRowId={{this.report.prepositionalObjectTableRowId}}
379+
/>
380+
</template>,
381+
);
382+
383+
assert.strictEqual(component.results.length, 2, 'result count is correct');
384+
assert.strictEqual(component.results[0].school, 'School 1:', 'result row school is correct');
385+
assert.strictEqual(component.results[0].name, 'First M. Last', 'result row name is correct');
386+
assert.strictEqual(component.results[1].school, 'School 2:', 'result row school is correct');
387+
assert.strictEqual(component.results[1].name, 'abc', 'result row name is correct');
388+
});
321389
});

0 commit comments

Comments
 (0)