Skip to content

Commit

Permalink
can query home page data (#181)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron Shiel <[email protected]>
  • Loading branch information
aaronshiel and Aaron Shiel authored Nov 25, 2024
1 parent ea250fe commit a7bce21
Show file tree
Hide file tree
Showing 8 changed files with 340 additions and 7 deletions.
136 changes: 136 additions & 0 deletions node/src/gql/query/home-page-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
This software is Copyright ©️ 2020 The University of Southern California. All Rights Reserved.
Permission to use, copy, modify, and distribute this software and its documentation for educational, research and non-profit purposes, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and subject to the full license file found in the root of this software deliverable. Permission to make commercial use of this software may be obtained by contacting: USC Stevens Center for Innovation University of Southern California 1150 S. Olive Street, Suite 2300, Los Angeles, CA 90115, USA Email: [email protected]
The full terms of this copyright and license should always be found in the root directory of this software deliverable as "license.txt" and if these terms are not found with this software, please contact the USC Stevens Center for the full license.
*/
import { GraphQLString } from 'graphql';
import { GraphQLID } from 'graphql';
import { GraphQLObjectType, GraphQLList } from 'graphql';
import {
Organization as OrganizationModel,
Mentor as MentorModel,
MentorPanel as MentorPanelModel,
Answer as AnswerModel,
Question as QuestionModel,
} from '../../models';
import { toAbsoluteUrl } from 'utils/static-urls';
import requireEnv from 'utils/require-env';
import SettingModel from '../../models/Setting';
export const HomePageMentorPanelType = new GraphQLObjectType({
name: 'HomePageMentorPanel',
fields: () => ({
_id: { type: GraphQLID },
org: { type: GraphQLID },
subject: { type: GraphQLID },
mentors: { type: GraphQLList(GraphQLID) },
title: { type: GraphQLString },
subtitle: { type: GraphQLString },
}),
});

export interface HomePageMentorPanel {
_id: string;
org: string;
subject: string;
mentors: string[];
title: string;
subtitle: string;
}

export const HomePageMentorType = new GraphQLObjectType({
name: 'HomePageMentor',
fields: () => ({
_id: { type: GraphQLID },
name: { type: GraphQLString },
title: { type: GraphQLString },
keywords: { type: GraphQLList(GraphQLString) },
transcript: { type: GraphQLString },
mentorUrl: { type: GraphQLString },
thumbnail: { type: GraphQLString },
}),
});

export interface HomePageMentor {
_id: string;
name: string;
title: string;
keywords: string[];
transcript: string;
mentorUrl: string;
thumbnail: string;
}

export const HomePageDataType = new GraphQLObjectType({
name: 'HomePageData',
fields: () => ({
panels: { type: new GraphQLList(HomePageMentorPanelType) },
mentors: { type: new GraphQLList(HomePageMentorType) },
}),
});

export interface HomePageData {
panels: HomePageMentorPanel[];
mentors: HomePageMentor[];
}

export const homePageData = {
type: HomePageDataType,
args: {
orgId: { type: GraphQLString },
},
resolve: async (
_root: GraphQLObjectType,
args: { orgId?: string }
): Promise<HomePageData> => {
const domain = requireEnv('DOMAIN');
const org = args.orgId
? await OrganizationModel.findById(args.orgId)
: null;
const config = org
? await OrganizationModel.getConfig(org)
: await SettingModel.getConfig();
const activeMentors = config.activeMentors || [];
const activeMentorPanels = config.activeMentorPanels || [];
const panels = await MentorPanelModel.find({
_id: { $in: activeMentorPanels },
});
const panelMentors = panels.flatMap((panel) => panel.mentors);
const mentors = await MentorModel.find({
_id: { $in: [...activeMentors, ...panelMentors] },
});
const introQuestion = await QuestionModel.findOne({
name: '_INTRO_',
});
if (!introQuestion) {
throw new Error('Intro question not found');
}
const mentorsIntroAnswers = await AnswerModel.find({
mentor: { $in: activeMentors },
question: introQuestion._id,
});
const homePageMentors = mentors.map((mentor) => ({
_id: mentor._id.toString(),
name: mentor.name,
title: mentor.title,
keywords: mentor.keywords,
transcript:
mentorsIntroAnswers.find(
(answer) => answer.mentor.toString() === mentor._id.toString()
)?.transcript || '',
mentorUrl: `https://${org?.name ? `${org.name}.` : ''}${domain}/chat/?mentor=${mentor._id}`,
thumbnail: mentor.thumbnail ? toAbsoluteUrl(mentor.thumbnail) : null,
}));
const homePageMentorPanels: HomePageMentorPanel[] = panels.map((panel) => ({
_id: panel._id.toString(),
org: panel.org?.toString(),
subject: panel.subject?.toString(),
mentors: panel.mentors.map((mentor) => mentor.toString()),
title: panel.title,
subtitle: panel.subtitle,
}));
return { mentors: homePageMentors, panels: homePageMentorPanels };
},
};

export default homePageData;
3 changes: 2 additions & 1 deletion node/src/gql/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import importTask from './import-task';
import trainTask from './train-task';
import answerByFieldValue from './answer-by-field';
import fetchMentorConfig from './fetch-mentor-config';

import homePageData from './home-page-data';
export default new GraphQLObjectType({
name: 'Query',
fields: {
Expand Down Expand Up @@ -83,5 +83,6 @@ export default new GraphQLObjectType({
trainTask,
answerByFieldValue,
fetchMentorConfig,
homePageData,
},
});
38 changes: 38 additions & 0 deletions node/test/fixtures/mongodb/data-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,34 @@ module.exports = {
question: 'This is an orphaned question (does not belong to a subject).',
type: 'QUESTION',
},
{
_id: new ObjectId('511111111111111111111194'),
question: 'Please provide your intro.',
name: '_INTRO_',
type: 'QUESTION',
},
],

answers: [
{
_id: new ObjectId('511111111111111111113174'),
mentor: new ObjectId('5ffdf41a1ee2c62111111119'),
question: new ObjectId('511111111111111111111194'),
hasEditedTranscript: true,
transcript: 'Here is my intro.',
video: 'https://idle/url',
status: 'COMPLETE',
webMedia: {
type: 'video',
tag: 'web',
url: 'videos/5ffdf41a1ee2c62111111111/511111111111111111111194/web.mp4',
},
mobileMedia: {
type: 'video',
tag: 'mobile',
url: 'videos/5ffdf41a1ee2c62111111111/511111111111111111111194/mobile.mp4',
},
},
{
_id: new ObjectId('511111111111111111111174'),
mentor: new ObjectId('5ffdf41a1ee2c62111111119'),
Expand Down Expand Up @@ -842,4 +867,17 @@ module.exports = {
created: '',
},
],

settings: [
{
_id: new ObjectId('511111111111111111111295'),
key: 'activeMentors',
value: ['5ffdf41a1ee2c62111111119'],
},
{
_id: new ObjectId('511111111111111111111296'),
key: 'activeMentorPanels',
value: ['5ffdf41a1ee2c62111111111'],
},
],
};
10 changes: 5 additions & 5 deletions node/test/graphql/mutation/me/config-update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe('updateConfig', () => {
});
expect(response.status).to.equal(200);
expect(response.body.data.me.updateConfig).to.eql({
activeMentors: [],
activeMentors: ['5ffdf41a1ee2c62111111119'],
featuredMentorPanels: ['5ffdf41a1ee2c62111111111'],
featuredMentors: ['5ffdf41a1ee2c62111111119'],
mentorsDefault: [],
Expand Down Expand Up @@ -206,7 +206,7 @@ describe('updateConfig', () => {
});
expect(response.status).to.equal(200);
expect(response.body.data.me.updateConfig).to.eql({
activeMentors: [],
activeMentors: ['5ffdf41a1ee2c62111111119'],
featuredMentorPanels: ['5ffdf41a1ee2c62111111111'],
featuredMentors: ['5ffdf41a1ee2c62111111119'],
mentorsDefault: [],
Expand Down Expand Up @@ -244,7 +244,7 @@ describe('updateConfig', () => {
});
expect(response.status).to.equal(200);
expect(response.body.data.me.updateConfig).to.eql({
activeMentors: [],
activeMentors: ['5ffdf41a1ee2c62111111119'],
featuredMentorPanels: [],
featuredMentors: [],
mentorsDefault: [],
Expand Down Expand Up @@ -288,8 +288,8 @@ describe('updateConfig', () => {
});
expect(response.status).to.equal(200);
expect(response.body.data.me.updateConfig).to.eql({
activeMentors: [],
activeMentorPanels: [],
activeMentors: ['5ffdf41a1ee2c62111111119'],
activeMentorPanels: ['5ffdf41a1ee2c62111111111'],
featuredMentorPanels: [],
featuredMentors: [],
mentorsDefault: [],
Expand Down
6 changes: 6 additions & 0 deletions node/test/graphql/mutation/me/mentor-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,12 @@ describe('import mentor', () => {
}`,
});
expect(response.body.data.questions.edges).to.eql([
{
node: {
mentor: null,
question: 'Please provide your intro.',
},
},
{
node: {
question:
Expand Down
2 changes: 1 addition & 1 deletion node/test/graphql/query/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('config', () => {
mentorsDefault: [],
featuredMentors: [],
featuredMentorPanels: [],
activeMentors: [],
activeMentors: ['5ffdf41a1ee2c62111111119'],
urlGraphql: '/graphql',
urlVideo: '/video',
styleHeaderLogo: '',
Expand Down
Loading

0 comments on commit a7bce21

Please sign in to comment.