-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Aaron Shiel <[email protected]>
- Loading branch information
1 parent
ea250fe
commit a7bce21
Showing
8 changed files
with
340 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.