-
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add admin presenter overview and current-state endpoints #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
847af8d
feat: add presenter api types
toddeTV 74cc703
refactor: extract quiz result counts
toddeTV 998b2ad
feat: add presenter admin routes
toddeTV d23d0d3
docs: add presenter api docs
toddeTV 5ef3ed8
fix: reject duplicate answer option labels
toddeTV File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
| import type { PresenterCurrentState } from '~/types' | ||
|
|
||
| export default defineEventHandler(async (event): Promise<PresenterCurrentState> => { | ||
| await verifyAdmin(event) | ||
|
|
||
| return await getPresenterCurrentState() | ||
| }) |
This file contains hidden or 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,7 @@ | ||
| import type { PresenterQuestionsOverview } from '~/types' | ||
|
|
||
| export default defineEventHandler(async (event): Promise<PresenterQuestionsOverview> => { | ||
| await verifyAdmin(event) | ||
|
|
||
| return await getPresenterQuestionsOverview() | ||
| }) |
This file contains hidden or 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 hidden or 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,87 @@ | ||
| import type { | ||
| PresenterCurrentState, | ||
| PresenterQuestionsOverview, | ||
| Question, | ||
| } from '~/types' | ||
|
|
||
| function getPercent(part: number, total: number): number { | ||
| return total > 0 ? Math.round((part / total) * 100) : 0 | ||
| } | ||
|
|
||
| function findActiveQuestion(questionList: Question[]): Question | undefined { | ||
| for (let index = questionList.length - 1; index >= 0; index -= 1) { | ||
| const question = questionList[index] | ||
|
|
||
| if (question?.is_active) { | ||
| return question | ||
| } | ||
| } | ||
|
|
||
| return undefined | ||
| } | ||
|
|
||
| /** Returns stable high-level quiz metadata for presenter integrations. */ | ||
| export async function getPresenterQuestionsOverview(): Promise<PresenterQuestionsOverview> { | ||
| const questionList = await getQuestions() | ||
|
|
||
| return { | ||
| totalQuestions: questionList.length, | ||
| questions: questionList.map(question => ({ | ||
| id: question.id, | ||
| key: question.key, | ||
| question_text: question.question_text, | ||
| })), | ||
| } | ||
| } | ||
|
|
||
| /** Returns polling-friendly presenter state for the active question. */ | ||
| export async function getPresenterCurrentState(): Promise<PresenterCurrentState> { | ||
| const [ | ||
| questionList, | ||
| peers, | ||
| ] = await Promise.all([ | ||
| getQuestions(), | ||
| getPeers(), | ||
| ]) | ||
| const totalUsers = peers.length | ||
| const currentQuestion = findActiveQuestion(questionList) | ||
|
|
||
| if (!currentQuestion) { | ||
| return { | ||
| hasActiveQuestion: false, | ||
| totalUsers, | ||
| receivedAnswers: 0, | ||
| receivedAnswersPercent: 0, | ||
| currentQuestion: null, | ||
| } | ||
| } | ||
|
|
||
| const answerList = await getAnswersForQuestion(currentQuestion.id) | ||
| const receivedAnswers = answerList.length | ||
| const results = buildQuestionOptionResults(currentQuestion, answerList) | ||
| const currentQuestionIndex = questionList.findIndex(question => question.id === currentQuestion.id) + 1 | ||
|
|
||
| return { | ||
| hasActiveQuestion: true, | ||
| totalUsers, | ||
| receivedAnswers, | ||
| receivedAnswersPercent: getPercent(receivedAnswers, totalUsers), | ||
| currentQuestion: { | ||
| id: currentQuestion.id, | ||
| key: currentQuestion.key, | ||
| index: currentQuestionIndex, | ||
| totalQuestions: questionList.length, | ||
| question_text: currentQuestion.question_text, | ||
| note: currentQuestion.note, | ||
| is_active: currentQuestion.is_active ?? false, | ||
| is_locked: currentQuestion.is_locked, | ||
| createdAt: currentQuestion.createdAt, | ||
| answer_options: currentQuestion.answer_options.map(option => ({ | ||
| text: option.text, | ||
| emoji: option.emoji, | ||
| count: results[option.text.en]?.count ?? 0, | ||
| percent: getPercent(results[option.text.en]?.count ?? 0, receivedAnswers), | ||
| })), | ||
| }, | ||
| } | ||
| } |
This file contains hidden or 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,37 @@ | ||
| import type { | ||
| Answer, | ||
| Question, | ||
| Results, | ||
| } from '~/types' | ||
|
|
||
| /** Builds per-option vote counts keyed by the English option label. */ | ||
| export function buildQuestionOptionResults(question: Question, answerList: Answer[]): Results['results'] { | ||
| const results = Object.create(null) as Results['results'] | ||
| const normalizedOptionLabels = new Set<string>() | ||
|
|
||
| for (const option of question.answer_options) { | ||
| const resultKey = option.text.en | ||
| const normalizedResultKey = resultKey.toLowerCase() | ||
|
|
||
| if (normalizedOptionLabels.has(normalizedResultKey)) { | ||
| throw new Error(`Duplicate answer option label is not supported: "${resultKey}"`) | ||
| } | ||
|
|
||
| normalizedOptionLabels.add(normalizedResultKey) | ||
|
|
||
| results[resultKey] = { | ||
| count: 0, | ||
| emoji: option.emoji, | ||
| } | ||
| } | ||
|
|
||
| for (const answer of answerList) { | ||
| const selectedAnswer = answer.selected_answer.en | ||
|
|
||
| if (Object.prototype.hasOwnProperty.call(results, selectedAnswer)) { | ||
| results[selectedAnswer]!.count += 1 | ||
| } | ||
| } | ||
|
|
||
| return results | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.