Skip to content

Commit

Permalink
default topics added to effective questions
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronshiel committed Feb 7, 2024
1 parent f9226a6 commit 53723b6
Show file tree
Hide file tree
Showing 6 changed files with 425 additions and 177 deletions.
46 changes: 41 additions & 5 deletions node/src/gql/query/mentor-client-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { AnswerMedia } from '../../models/Answer';
import { Question, QuestionType } from '../../models/Question';
import SettingModel, { Config } from '../../models/Setting';
import { SubjectQuestion, Topic } from '../../models/Subject';
import { Category, SubjectQuestion, Topic } from '../../models/Subject';
import OrganizationModel, { Organization } from '../../models/Organization';
import { User } from '../../models/User';
import { isAnswerComplete, Mentor } from '../../models/Mentor';
Expand All @@ -36,6 +36,7 @@ import {
IExternalVideoIds,
externalVideoIdsDefault,
} from '../mutation/api/update-answers';
import { UseDefaultTopics } from 'gql/mutation/me/helpers';

export interface MentorClientData {
_id: string;
Expand Down Expand Up @@ -126,7 +127,8 @@ async function getQuestions(
async function getCompletedQuestions(
mentor: Mentor,
sQuestions: SubjectQuestion[],
questions: Question[]
questions: Question[],
categories: Category[]
): Promise<SubjectQuestion[]> {
let answers = await AnswerModel.find({
mentor: mentor._id,
Expand All @@ -140,7 +142,30 @@ async function getCompletedQuestions(
)
);
const questionIds = answers.map((a) => `${a.question}`);
return sQuestions.filter((sq) => questionIds.includes(`${sq.question}`));
const completedQuestions = sQuestions.filter((sq) =>
questionIds.includes(`${sq.question}`)
);
// Add category default topics
const subjectQuestionsWithUpdatedTopics: SubjectQuestion[] =
completedQuestions.map((sQuestion) => {
if (!sQuestion.category) {
return sQuestion;
}
const shouldAddDefaultTopics =
sQuestion.useDefaultTopics === UseDefaultTopics.TRUE ||
(sQuestion.useDefaultTopics === UseDefaultTopics.DEFAULT &&
sQuestion.topics.length === 0);
if (!shouldAddDefaultTopics) {
return sQuestion;
}
const category = categories.find((c) => c.id === sQuestion.category);
if (!category) {
return sQuestion;
}
sQuestion.topics = [...sQuestion.topics, ...category.defaultTopics];
return sQuestion;
});
return subjectQuestionsWithUpdatedTopics;
}

export const mentorData = {
Expand Down Expand Up @@ -191,13 +216,19 @@ export const mentorData = {
// get recorded questions in subject order
const sqs = subject.questions;
questions = await getQuestions(mentor, sqs);
sQuestions = await getCompletedQuestions(mentor, sqs, questions);
sQuestions = await getCompletedQuestions(
mentor,
sqs,
questions,
subject.categories
);
}
// no specified or default subject, use all subjects
else {
const subjects = await SubjectModel.find({
_id: { $in: mentor.subjects },
});
const allCategories = subjects.map((s) => s.categories).flat();
// get topics in alphabetical order
topics = subjects.reduce((acc, cur) => {
const newTopics = cur.topics.filter(
Expand All @@ -211,7 +242,12 @@ export const mentorData = {
// get recorded questions in alphabetical order
const sqs = subjects.reduce((acc, cur) => [...acc, ...cur.questions], []);
questions = await getQuestions(mentor, sqs);
sQuestions = await getCompletedQuestions(mentor, sqs, questions);
sQuestions = await getCompletedQuestions(
mentor,
sqs,
questions,
allCategories
);
sQuestions.sort((a, b) => {
const qa = questions.find((q) => `${q._id}` === `${a.question}`);
const qb = questions.find((q) => `${q._id}` === `${b.question}`);
Expand Down
145 changes: 145 additions & 0 deletions node/test/fixtures/mongodb/data-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ module.exports = {
mentorIds: ['5ffdf41a1ee2c62119991114'],
userRole: 'USER',
},
{
_id: ObjectId('5ffdf41a1ee2c62119991235'),
name: 'Test Default Topics User',
email: '[email protected]',
mentorIds: ['5ffdf41a1ee2c62119991234'],
userRole: 'USER',
},
],

mentors: [
Expand Down Expand Up @@ -252,6 +259,17 @@ module.exports = {
mentorConfig: ObjectId('5ffdf41a1ee2c62111111132'),
lockedToConfig: true,
},
{
_id: ObjectId('5ffdf41a1ee2c62119991234'),
name: 'Test Default Topics Mentor',
firstName: 'DefaultTopics',
isPrivate: false,
user: ObjectId('5ffdf41a1ee2c62119991235'),
subjects: [ObjectId('5ffdf41a1ee2c62119991236')],
keywords: ['Nonbinary'],
orgPermissions: [],
lockedToConfig: false,
},
],

mentorpanels: [
Expand Down Expand Up @@ -367,6 +385,57 @@ module.exports = {
questions: [{ question: ObjectId('511111111111111111111115') }],
deleted: true,
},
{
_id: ObjectId('5ffdf41a1ee2c62119991236'),
name: 'Subject Topic Question Test',
description: 'Testing subject question test',
categories: [
{
id: 'category',
name: 'Test Category',
description: 'A test category',
defaultTopics: ['5ffdf41a1ee2c62119991237'],
},
],
topics: [
{
id: ObjectId('5ffdf41a1ee2c62320b21ec3'),
name: 'Unused Category',
description: 'Unused Category',
},
{
id: ObjectId('5ffdf41a1ee2c62119991237'),
name: '(Default Topic) Test Category',
description: 'Categories default topic',
},
],
questions: [
{
question: ObjectId('511111111111111111111115'),
topics: [],
category: 'category',
useDefaultTopics: 'TRUE',
},
{
question: ObjectId('511111111111111111111113'),
topics: [],
category: 'category',
// useDefaultTopics: 'DEFAULT', should automatically be default
},
{
question: ObjectId('511111111111111111111117'),
topics: [],
category: 'category',
useDefaultTopics: 'FALSE',
},
{
question: ObjectId('511111111111111111111114'),
topics: [],
category: '',
useDefaultTopics: 'DEFAULT',
},
],
},
],

questions: [
Expand Down Expand Up @@ -538,6 +607,82 @@ module.exports = {
url: 'videos/5ffdf41a1ee2c62111111111/511111111111111111111111/mobile.mp4',
},
},
{
_id: ObjectId('511111111111111111111196'),
mentor: ObjectId('5ffdf41a1ee2c62119991234'),
question: ObjectId('511111111111111111111115'),
hasEditedTranscript: true,
transcript: 'answer transcript',
video: 'https://idle/url',
status: 'COMPLETE',
webMedia: {
type: 'video',
tag: 'web',
url: 'videos/5ffdf41a1ee2c62111111111/511111111111111111111111/web.mp4',
},
mobileMedia: {
type: 'video',
tag: 'mobile',
url: 'videos/5ffdf41a1ee2c62111111111/511111111111111111111111/mobile.mp4',
},
},
{
_id: ObjectId('511111111111111111111197'),
mentor: ObjectId('5ffdf41a1ee2c62119991234'),
question: ObjectId('511111111111111111111113'),
hasEditedTranscript: true,
transcript: 'answer transcript',
video: 'https://idle/url',
status: 'COMPLETE',
webMedia: {
type: 'video',
tag: 'web',
url: 'videos/5ffdf41a1ee2c62111111111/511111111111111111111111/web.mp4',
},
mobileMedia: {
type: 'video',
tag: 'mobile',
url: 'videos/5ffdf41a1ee2c62111111111/511111111111111111111111/mobile.mp4',
},
},
{
_id: ObjectId('511111111111111111111198'),
mentor: ObjectId('5ffdf41a1ee2c62119991234'),
question: ObjectId('511111111111111111111117'),
hasEditedTranscript: true,
transcript: 'answer transcript',
video: 'https://idle/url',
status: 'COMPLETE',
webMedia: {
type: 'video',
tag: 'web',
url: 'videos/5ffdf41a1ee2c62111111111/511111111111111111111111/web.mp4',
},
mobileMedia: {
type: 'video',
tag: 'mobile',
url: 'videos/5ffdf41a1ee2c62111111111/511111111111111111111111/mobile.mp4',
},
},
{
_id: ObjectId('511111111111111111111199'),
mentor: ObjectId('5ffdf41a1ee2c62119991234'),
question: ObjectId('511111111111111111111114'),
hasEditedTranscript: true,
transcript: 'answer transcript',
video: 'https://idle/url',
status: 'COMPLETE',
webMedia: {
type: 'video',
tag: 'web',
url: 'videos/5ffdf41a1ee2c62111111111/511111111111111111111111/web.mp4',
},
mobileMedia: {
type: 'video',
tag: 'mobile',
url: 'videos/5ffdf41a1ee2c62111111111/511111111111111111111111/mobile.mp4',
},
},
],

userquestions: [
Expand Down
Loading

0 comments on commit 53723b6

Please sign in to comment.